/* @Author: alexongarato.com Description: The main objective of this experiment is to create a behavior that allows it to run away from the light. This experiment utilizes two LDRs to measure light and, accordingly to this, take control of 2 dc motors through a h-bridge. Date: 23/06/2011 */ // ---------------- CONSTANTS #define SENSOR_LEFT A1 //pin where the left LDR is attached #define SENSOR_RIGHT A0 //pin where the right LDR is attached #define LED 13 #define MAX_VEL 255 #define DELAY 50 #define BACKWARD 0 #define FORWARD 1 #define LEFT 2 #define RIGHT 3 //Arduino predefined pins #define LEFT_FORW 12 #define LEFT_BACK 11 #define RIGHT_FORW 7 #define RIGHT_BACK 8 // ---------------- VARIABLES int _leftSensorDif = 0; int _rightSensorDif = 0; int _leftSensorReading = 0; int _rightSensorReading = 0; // ---------------- MAIN PROGRAM void setup() { //set the pin mode of all the pins that will be used. pinMode(LED, OUTPUT); pinMode(SENSOR_LEFT, INPUT); pinMode(SENSOR_RIGHT, INPUT); calibrateSensors(); } void loop() { //read the actual LDRs values and substract with the first sensor read, it must be done to determine an initial state that will be //used as a reference point for all behaviors. _leftSensorReading = analogRead(SENSOR_LEFT) - _leftSensorDif; _rightSensorReading = analogRead(SENSOR_RIGHT) - _rightSensorDif; delay(100); //if the values are higher than the servos velocity, then call calibrateSensors() method. if(_leftSensorReading> MAX_VEL*3 || _leftSensorReading< MAX_VEL*-3) calibrateSensors(); if(_rightSensorReading> MAX_VEL*3 || _rightSensorReading< MAX_VEL*-3) calibrateSensors(); int leftForward = (_rightSensorReading>0 ? ((_rightSensorReading>MAX_VEL) ? MAX_VEL : _rightSensorReading) : 0); int leftBackward = (_rightSensorReading<0 ? (((_rightSensorReading*-1)>MAX_VEL) ? MAX_VEL : (_rightSensorReading*-1)) : 0); int rightForward = (_leftSensorReading>0 ? ((_leftSensorReading>MAX_VEL) ? MAX_VEL : _leftSensorReading) : 0); int rightBackward = (_leftSensorReading<0 ? (((_leftSensorReading*-1)>MAX_VEL) ? MAX_VEL : (_leftSensorReading*-1)) : 0); go(LEFT_FORW, leftForward); go(LEFT_BACK, leftBackward); go(RIGHT_FORW, rightForward); go(RIGHT_BACK, rightBackward); } // ---------------- ACTIONS void calibrateSensors() { stopAll(); //read the actual LDR values and set it as the default values. _leftSensorDif = analogRead(SENSOR_LEFT); _rightSensorDif = analogRead(SENSOR_RIGHT); } void go(int DIRECTION, int strength) { strength = (strength>MAX_VEL) ? MAX_VEL : strength; if(DIRECTION == BACKWARD){ analogWrite(LEFT_FORW, LOW); analogWrite(LEFT_BACK, strength); analogWrite(RIGHT_FORW, LOW); analogWrite(RIGHT_BACK, strength); } else if(DIRECTION == FORWARD){ analogWrite(LEFT_FORW, strength); analogWrite(LEFT_BACK, LOW); analogWrite(RIGHT_FORW, strength); analogWrite(RIGHT_BACK, LOW); } else if(DIRECTION == LEFT){ analogWrite(LEFT_FORW, strength); analogWrite(LEFT_BACK, LOW); analogWrite(RIGHT_FORW, LOW); analogWrite(RIGHT_BACK, MAX_VEL); } else if(DIRECTION == RIGHT){ analogWrite(LEFT_FORW, LOW); analogWrite(LEFT_BACK, MAX_VEL); analogWrite(RIGHT_FORW, strength); analogWrite(RIGHT_BACK, LOW); } } void stopAll() { go(LEFT, 0); go(RIGHT, 0); go(FORWARD, 0); go(BACKWARD, 0); }