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-666c9da171e19728744618-i/] method.

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.
In this example a variable is created named “LeftSensor” and puts the number 19 into it.

Learn More

Next, inside  [crayon-666c9da171e24801188401-i/] , enter the following to initialize the use of the serial monitor .
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-666c9da171e26756250247-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.

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-666c9da171e29108670515-i/] to print one value consecutively similar to how sentences are created.
  • [crayon-666c9da171e2c295915551-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.
Lastly, include a delay at the end so that it will give some space in between the sensor values

delay( )

delay( time in milliseconds );

[crayon-666c9da171e2f868650719-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-666c9da171e30085030544-i/] should be 3000.

Final Code