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-66068cfde92d2257889671-i/] method.
[crayon-66068cfde92da231425413/]

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

Learn More

Next, inside  [crayon-66068cfde92de523669378-i/] , enter the following to initialize the use of the serial monitor .
[crayon-66068cfde92df896410019/]
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-66068cfde92e0299030890-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-66068cfde92e2013650965/]

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

delay( )

delay( time in milliseconds );

[crayon-66068cfde92eb800269595-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-66068cfde92ec451253123-i/] should be 3000.
[crayon-66068cfde92ee946392393/]

Final Code

[crayon-66068cfde92ef960578658/]