Using a light sensor to avoid light

< Tutorials‎ | Arduino Projects/Mobile Robotics/BoeBot
Revision as of 15:43, 16 October 2012 by Jwatson (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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()
    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.

This will reverse the motor values so instead of moving towards and 'chasing' the light, the robot will now 'run away' from the light.