Introduction to IR Sensors

Objectives

  1. Continue developing an understanding of basic Java programming using the IntelliBrain bots.
  2. Obtain IR sensor values and display them to the LCD.
  3. Use IR sensors to help the robot move.

Important Words

  • analog
  • IR (infrared)


Before you start

Know how to create a project in a Java IDE and RoboJDE (Getting Started Tutorial link)

Have basic familiarity with the IntelliBrain bot

Complete the Introduction to Input/Output tutorial

Complete the Introduction to Motors tutorial



Obtaining values from the IR sensors

Previously you've only used the thumbwheel as an input for the IntelliBrain bot. We are now going to start exploring some of the other inputs that the robot has available. Infrared sensors (IR sensors) are used to determine if there is an object a short distance away. The following code will obtain the value from either the left or the right IR sensor (you get to pick which one) and display it on the LCD. This code is designed for the robot to have two IR sensors, with one angled towards the front right and the other towards the front left (see above image). If your IntelliBrain is set up differently, with a third IR sensor facing forward, the code can be adjusted accordingly. Let's begin by making a new program named DisplayIRValue and importing the libraries we need to get a value from the IR sensors and display them on the LCD display.


The IR sensors are only sensitive at close ranges. Above a certain distance, objects may not be detected. When you're working through the questions at the end of this section, try to figure out the range for the IR sensors.



	import com.ridgesoft.intellibrain.IntelliBrain; 

	import com.ridgesoft.io.Display;

	import com.ridgesoft.robotics.AnalogInput; //library we need to import to be able to use the IR sensors

	

	public class DisplayIRValue { 

		public static void main(String[] args) { 

		

                //you code will go here



			}

		}

	}


Now that we have the library imports we need, we need to create a display for the program to use and also create an IR sensor for the program to sample.


	import com.ridgesoft.intellibrain.IntelliBrain; 

	import com.ridgesoft.io.Display;

	import com.ridgesoft.robotics.AnalogInput; //library we need to import to be able to use the IR sensors

	

	public class DisplayIRValue { 

		public static void main(String[] args) { 

		

		   Display display = IntelliBrain.getLcdDisplay();

		   AnalogInput leftIR = Intellibrain.getAnalogInput(1); 

			

			}

		}

	}

Just like with the thumbwheel, we have to use a method located in the library we imported to get a value from the IR sensor and we also have to have a place to store the value that we get. The constructor also requires us to tell it which port the sensor is plugged into, in this case it is in port 1.


	import com.ridgesoft.intellibrain.IntelliBrain; 

	import com.ridgesoft.io.Display;

	import com.ridgesoft.robotics.AnalogInput; //library we need to import to be able to use the IR sensors

	

	public class DisplayIRValue { 

		public static void main(String[] args) { 

		

		   Display display = IntelliBrain.getLcdDisplay();

		   AnalogInput leftIR = Intellibrain.getAnalogInput(1); 

			

		              

			      int result = leftIR.sample();  //method to obtain a value from the IR sensor located in port 1



			

		}

	}

Now that we have a sensor value, we want to display that value on the LCD. Let's put "IR Sensor Value" on the first line and the value on the second line.


	import com.ridgesoft.intellibrain.IntelliBrain; 

	import com.ridgesoft.io.Display;

	import com.ridgesoft.robotics.AnalogInput; //library we need to import to be able to use the IR sensors

	

	public class DisplayIRValue { 

		public static void main(String[] args) { 

		

		   Display display = IntelliBrain.getLcdDisplay();

		   AnalogInput leftIR = Intellibrain.getAnalogInput(1); 

			

		            

			      int result = leftIR.sample();

			      display.print(0, "IR Sensor Value");

			      display.print(1, Integer.toString(result)); 

			

		}

	}

We have one last thing we need in our program, do you remember what it is? If you guessed, tell the program how long to display the result on the LCD display, then you are correct! Let's use a while(true) loop like we have previously.


	import com.ridgesoft.intellibrain.IntelliBrain; 

	import com.ridgesoft.io.Display;

	import com.ridgesoft.robotics.AnalogInput; //library we need to import to be able to use the IR sensors

	

	public class DisplayIRValue { 

		public static void main(String[] args) { 

		

		   Display display = IntelliBrain.getLcdDisplay();

		   AnalogInput leftIR = Intellibrain.getAnalogInput(1); 

			

			while(true) {                

			      int result = leftIR.sample();

			      display.print(0, "IR Sensor Value");

			      display.print(1, Integer.toString(result)); 

			}

		}

	}

The variable result is an integer (it is of the integer type). We can't display integers directly, we can only display strings (variables of the string type). So in order to do this we need to convert the integer type to a string type. We accomplished this here by using the Integer.toString(result) method.




Words to know


analog
- this represents a continuous form of data input, think of old clocks with hour, minute, and second hands that are always in motion
IR
- stands for infrared red, it is a type of sensor


More Obtaining values from the IR Sensors


  1. How would you get the value from the other IR sensor? Try it!

  2. What happens when you put a piece of paper in front of the sensor? Try it!

  3. What happens to the value displayed as you move the piece of paper closer and/or further away?

  4. What are the minimum and maximum values from the IR sensor for your robot?

  5. What happens if you swap the places of the 0 and 1 on lines 13 and 14?