/*Project LDR Truck By Thomas Longsted - Using an L293D Motor Driver IC, With 4 LDR Sensor To Control It With Light And The Vehicle will Drive Against The Light*/ #define switchPin 2 // switch input #define motorPin1 3 // L293D pin 2 enable to control direction with arduino pin 3 #define motorPin2 4 // L293D pin 7 enable to control direction with arduino pin 4 #define speedPin9 9 // L293D pin 1 enable to control the right motorspeed with arduino pin 9 #define motorPin3 6 // L293D pin 15 enable to control the speed with Arduino pin 6 #define motorPin4 7 // L293D pin 10 enable to control the speed with Arduino pin 7 #define speedPin10 10 // L293D pin 9 enable to control the left motorspeed with Arduino pin 10 int Mspeed1 = 0; // a variable to hold the current speed value int Mspeed2 = 0; // a variable to hold the current speed value int LDR_0 = 0; // a variable for ldr int LDRValue_left = 0; // a variable to hold the value for ldr int light_sensitivity_left = 100; // a variable for light_sensitivity int LDR_1 = 1; // a variable for ldr int LDRValue_right = 0; // a variable to hold the value for ldr int light_sensitivity_right = 100; // a variable for light_sensitivity int LDR_2 = 2; // a variable for ldr int LDRValue_front = 0; // a variable to hold the value for ldr int light_sensitivity_front = 100; // a variable for light_sensitivity int LDR_3 = 3; // a variable for ldr int LDRValue_back = 0; // a variable to hold the value for ldr int light_sensitivity_back = 100; // a variable tfor light_sensitivity int ledPin = 11; // a variable for ledpin void setup() { Serial.begin(9600); // begin serial monitor pinMode(ledPin, OUTPUT); // set ledpin as input pinMode(switchPin, INPUT); // set switchpin as input pinMode(motorPin1, OUTPUT); // set motorpin as output pinMode(motorPin2, OUTPUT); // set motorpin as output pinMode(speedPin9, OUTPUT); // set motorpin as output pinMode(motorPin3, OUTPUT); // set motorpin as output pinMode(motorPin4, OUTPUT); // set motorpin as output pinMode(speedPin10, OUTPUT); // set motorpin as output for (int x=0; x<3; x++) { // read code 3.times digitalWrite(ledPin, HIGH); // LED on delay(500); // wait for 500ms digitalWrite(ledPin, LOW); // LED off delay(500); // wait for 500ms digitalWrite(ledPin, HIGH); // led on } } void loop() { LDRValue_left = analogRead(LDR_0); // reads the ldr value with analog input 0 “A0″ //Serial.print(LDRValue_left); // print value to scope for ldrvalue_left LDRValue_right = analogRead(LDR_1); // reads the ldr value with analog input 1 “A01″ Serial.print(LDRValue_right); // print value to scope for ldrvalue_right LDRValue_front = analogRead(LDR_2); // reads the ldr value with analog input 2 “A2″ //Serial.print(LDRValue_front); // print value to scope for ldrvalue_front LDRValue_back = analogRead(LDR_3); // reads the ldr value with analog input 3 “A3″ //Serial.print(LDRValue_back); // print value to scope for ldrvalue_back Serial.println(); // print on new line if (LDRValue_front > light_sensitivity_front) { // run code if ldrvalue is more then light_sensitivity_front analogWrite(speedPin9, Mspeed1 = 200); // write speed to right motor analogWrite(speedPin10, Mspeed2 = 200); // write speed to left motor digitalWrite(motorPin1, HIGH); // set motorpin1 on the L293D to high - for direction on the right motor digitalWrite(motorPin2, LOW); // set motorpin2 on the L293D to low - for direction on the right motor digitalWrite(motorPin3, HIGH); // set motorpin3 on the L293D to high -for direction on the left motor digitalWrite(motorPin4, LOW); // set motorpin4 on the L293D to low - for direction on the left motor delay(1000); // wait for 1000ms analogWrite(speedPin9, Mspeed1 = 0); // write speed to right motor for stop analogWrite(speedPin10, Mspeed2 = 0); // write speed to left motor for stop } if (LDRValue_back > light_sensitivity_back) { // run code if ldrvalue is more then light_sensitivity_back analogWrite(speedPin9, Mspeed1 = 200); // write speed to right motor analogWrite(speedPin10, Mspeed2 = 200); // write speed to left motor digitalWrite(motorPin1, LOW); // set motorpin1 on the L293D to low - for direction on the right motor digitalWrite(motorPin2, HIGH); // set motorpin2 on the L293D to high - for direction on the right motor digitalWrite(motorPin3, LOW); // set motorpin3 on the L293D to low -for direction on the left motor digitalWrite(motorPin4, HIGH); // set motorpin4 on the L293D to high - for direction on the left motor delay(1000); analogWrite(speedPin9, Mspeed1 = 0); // write speed to right motor for stop analogWrite(speedPin10, Mspeed2 = 0); // write speed to left motor for stop } if (LDRValue_left > light_sensitivity_left) { // Run code if ldrvalue is more then light_sensitivity_left analogWrite(speedPin9, Mspeed1 = 200); // write speed to right motor analogWrite(speedPin10, Mspeed2 = 200); // write speed to left motor digitalWrite(motorPin1, HIGH); // set motorpin1 on the L293D to high - for direction on the right motor digitalWrite(motorPin2, LOW); // set motorpin2 on the L293D to low - for direction on the right motor digitalWrite(motorPin3, LOW); // set motorpin3 on the L293D to low -for direction on the left motor digitalWrite(motorPin4, HIGH); // set motorpin4 on the L293D to high - for direction on the left motor delay(3000); analogWrite(speedPin9, Mspeed1 = 0); // write speed to right motor for stop analogWrite(speedPin10, Mspeed2 = 0); // write speed to left motor for stop } if (LDRValue_right > light_sensitivity_right) { // Run code if ldrvalue is more then light_sensitivity_right analogWrite(speedPin9, Mspeed1 = 200); // write speed to right motor analogWrite(speedPin10, Mspeed2 = 200); // write speed to left motor digitalWrite(motorPin1, LOW); // set motorpin1 on the L293D to low - for direction on the right motor digitalWrite(motorPin2, HIGH); // set motorpin2 on the L293D to high - for direction on the right motor digitalWrite(motorPin3, HIGH); // set motorpin3 on the L293D to high -for direction on the left motor digitalWrite(motorPin4, LOW); // set motorpin4 on the L293D to low - for direction on the left motor delay(3000); analogWrite(speedPin9, Mspeed1 = 0); // write speed to right motor for stop analogWrite(speedPin10, Mspeed2 = 0); // write speed to left motor for stop } }