0

Soccer Robot C - Basic lessons - Barrier

Light barrier in this robot consists of a highly directional LED and a photoresistor. The phototransistor outputs its signal and we can read it as analog value, which is smaller when a ball interrupts the light.
void RobotSoccer::loop() {
	if (analogRead(35) < 500)
		print("Ball\n\r");
	else
		print("\n\r");
}
analogRead() is Arduino function for reading analog values. Analog - it usually means numbers from 0 to 1023 or similar. Contrary to digital, which consists of just 2 values. The function has this form:
analogRead(pinNumber);
Our pin number is 35, therefore the command in the program has 35 as the argument. If the returned value is smaller than 500 (some other number may be good for Your case), that means that the light is interrupted, and the program will display "Ball".

Task: stop when ball caught.

Put a ball 15 cm in front of the robot. Write a program that will drive the robot straight ahead and stop when the ball interrupts the barrier.