Using a light sensor to avoid light
From ROBOTC API Guide
Arduino → Arduino Tutorials and Guided Projects → Parallax BoeBot + Arduino Shield, Mobile Robotics Platform → Using a light sensor to avoid light
Say you wanted your robot to avoid light - perhaps your robot's photosensory is photosensitive, or you want to hunt vampires. We can do this by a small modification of the previous program, and get drastically different results. This is one of the powers of programming - once you have a solid code base, you can easily manipulate it to get a variety of interesting results.
All we need to do is change the code so that shadeDiff is subtracted from the leftServo, and added to the rightServo.
#pragma config(CircuitBoardType, typeCktBoardUNO) #pragma config(PluginCircuitBoard, typeShieldParallaxBoeBot) #pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0) #pragma config(Sensor, anlg0, leftLight, sensorReflection) #pragma config(Sensor, anlg1, rightLight, sensorReflection) #pragma config(Motor, servo_10, leftServo, tmotorServoContinuousRotation, openLoop, IOPins, dgtl10, None) #pragma config(Motor, servo_11, rightServo, tmotorServoContinuousRotation, openLoop, reversed, IOPins, dgtl11, None) //*!!Code automatically generated by 'ROBOTC' configuration wizard !!*// task main() { while(true) { int factor = 120; int shadeDiff = ((SensorValue[rightLight] * factor) / (SensorValue[leftLight] + SensorValue[rightLight]) - (factor / 2)) * 2; motor[leftServo] = 20 - shadeDiff; motor[rightServo] = 20 + shadeDiff; //If the loop cycles too quickly, we get calculation errors. Therefore we regulate the speed by waiting a bit. wait1Msec(10); } } |
This will reverse the motor values so instead of moving towards and 'chasing' the light, the robot will now 'run away' from the light.