How to Program the TOP IR Sensors on the Inventor Board

This lesson will go over the Top IR Sensor Pins found on PINS(19~21)

First, begin the code by assigning Global Variable names to the TOP IR Sensor Pins(19-21). Enter the following before the [crayon-670b113c1a366459554227-i/] method.
[crayon-670b113c1a36d325734287/]

Learn More: What’s a Variable?

Variables are a way to store information in a program. The easiest way to think of them is as if they were boxes. In these boxes we can store stuff and we can write a name on the box to help us know which box is the one we want. One box can be named kitchen and has plates in it, another could be named books and has books in it, of course another could be named blankets and has lamps in it. With variables we can do the same thing.
[crayon-670b113c1a36e335050345/]
In this example a variable is created named “LeftSensor” and puts the number 19 into it.

Learn More

Next, inside  [crayon-670b113c1a370151665126-i/] , enter the following to initialize the use of the serial monitor .
[crayon-670b113c1a371655128509/]
This function will establish a “Serial” connection between the computer and the board. The number 9600 is known as the BAUD rate and refers to the processing speed, in this case 9600 bytes/second. This value can change depending on the application, just make sure that when you run your program that this number matches with the number located on the bottom right of the Serial Monitor.

Next, inside  [crayon-670b113c1a373597673825-i/] you will need to create global variable names for the value that each IR Sensor is providing. This value represents the distance an object is from the each value that the sensor is reading.
[crayon-670b113c1a374823658554/]

analogRead( )

analogRead( pin number);

  • This function will allow the board to receive an analog signal from a designated pin
  • For this example the pin number is replaced with the Variable that was assigned in the beginning. The pin numbers are replaced with the variable names ‘LeftSensor’, ‘CenterSensor’, and ‘RightSensor’

Next, display the values by using the following:

  • [crayon-670b113c1a375422522639-i/] to print one value consecutively similar to how sentences are created.
  • [crayon-670b113c1a378860476010-i/] to print in blocks similar to how paragraphs are created.

This example will use both by indicating which value to print and display on the serial monitor. We will be using it to display all the values relayed by the sensor. Text is displayed by placing quotations ” ” before and after the desired word to display. Do the following for all three sensor values.
[crayon-670b113c1a379104098611/]
Lastly, include a delay at the end so that it will give some space in between the sensor values
[crayon-670b113c1a37a757314176/]

delay( )

delay( time in milliseconds );

[crayon-670b113c1a37b242182522-i/]is a function that will define how long an action should be performed. There are 1000 milliseconds in 1 second. This means that if the action should run for 3 seconds, the time entered in the [crayon-670b113c1a37c139013663-i/] should be 3000.
[crayon-670b113c1a37d842068725/]

Final Code

[crayon-670b113c1a37e989592182/]