IR Sensors as Input

Using the IR sensors as Input for the Robot

Previously, you learned how to use the IR sensors on the IntelliBrain and to display the value on the LCD. The value that we can get from the sensors is useful as it can provide us with an approximate distance of how far an obstacle is away from the robot. Now, we are going to take it a step further and use the IR sensors as an input for the motors. This is useful when the robot is moving to help avoid crashing into something!


Let's start making a new program IR_Avoid now and import the libraries we need to use the LCD display, motors, and IR sensors.



	import com.ridgesoft.intellibrain.IntelliBrain; 

	import com.ridgesoft.io.Display;

	import com.ridgesoft.robotics.AnalogInput; 

	import com.ridgesoft.robotics.Motor; 

	import com.ridgesoft.robotics.ContinuousRotationServo; 

	

	public class IR_Avoid {

		public static void main(String[] args) { 

		



		} 

	}



So how do we do this? First we need to decide what is an acceptable safe distance for an object to be from the robot and see what the corresponding sensor values are for that distance. If that sounds similar to what you just did, you're right! In the following code we are going to use a sensor value of 300, however, your number will vary depending on your sensors and the distance you decide is right for the robot! Next want the robot to avoid any obstacles the object detects. We will do this using an if/else if/else statement to control the motors. If you need refreshing on how if/else if/ else statements work or how to use the motors to make the robot move, head on back to the Introduction to Input/Output tutorial (link) and/or the Introduction to Motors tutorial (link).


As always, try writing your code first using the template from above, then compare your code to the complete code below. The following code allows us to use the IR sensors to help the robot move, and in this case, avoid coming too close to an object that is either on the left or the right. If an object is too close on either side, the code will cause the robot to turn in the opposite direction until the object is far enough away.


Helpful Hint: Remember to create the parts of the robot you want to use in your program. Just because you can see it attached to the robot, doesn't mean that the program knows where it is or how to use it!


	import com.ridgesoft.intellibrain.IntelliBrain; 

	import com.ridgesoft.io.Display;

	import com.ridgesoft.robotics.AnalogInput; 

	import com.ridgesoft.robotics.Motor; 

	import com.ridgesoft.robotics.ContinuousRotationServo; 

	

	public class IR_Avoid {

		public static void main(String[] args) { 

		

			Motor leftMotor = new ContinuousRotationServo(IntelliBrain.getServo(1), false, 10); 

			Motor rightMotor = new ContinuousRotationServo(IntelliBrain.getServo(2), true, 10); 

			Display display = IntelliBrain.getLcdDisplay();

			AnalogInput leftIR = IntelliBrain.getAnalogInput(1); 

            AnalogInput rightIR = IntelliBrain.getAnalogInput(2);

			

			while(true) {                

				int leftSensor = leftIR.sample()                

				int rightSensor = rightIR.sample();

				 

				if(leftSensor > 300){                         

					leftMotor.setPower(0);                        

					rightMotor.setPower(8); 

				}else if(rightSensor > 300) {                     

					leftMotor.setPower(8);                     

					rightMotor.setPower(0); 

				} else {                     

					display.print(0, “IRs Below 300”);                      

					display.print(1, “Moving Forward”);                     

					leftMotor.setPower(8);                      

					rightMotor.setPower(8); 

				} 

			} 

		} 

	}



Remember how loops work (see Introduction to Loops tutorial link)? As the robot is moving in the while(true) loop, if it encounters a condition where either the leftSensor or rightSensor are greater than 300, it changes the power to the motors causing the robot to turn. It will stay in that block until the sensor is no longer above 300.





More Using the IR sensors as input for the robot


  1. What happens if you use a different value other than 300? Try it!

  2. Suppose you wanted the robot to respond to an actual value, say 6 inches, instead of just a number. How would you go about this?