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 void setup(); 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   void setup(); , 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   void loop(); 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:

  • Serial.print(); to print one value consecutively similar to how sentences are created.
  • Serial.printnln(); 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 );

delay()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 delay() should be 3000.

Final Code