# Midterm: Light-Loving/Light-Fearing Vehicle

My initial plan for my midterm project was to produce a vehicle that would behave differently depending on how far it was from an object that is in its way. However, Aaron gave me a brilliant idea, which was to have a look at Braitenberg Vehicles, vehicles that would behave differently depending on its distance from light. This solved one of the biggest problems with my initial idea, because the Braitenberg vehicle would allow for greater interaction with users, allowing people to use flashlights on their phone  to navigate the vehicle.

My initial prototype was made of cardboard, however, this was structurally very weak and the right wheel would constantly fall off after prolonged movement. The vehicle was also lopsided, with the front being much lower to the ground than the back of the vehicle. This was not aesthetically pleasing and it resulted in some performance issues.

So I instead decided to use wood for the vehicle, which provides a more solid foundation, and solved the issue of the tilted vehicle. For the program itself, I ran an analogRead on the photoresistors positioned at the front of the vehicle, which would determine the speed of the wheels when exposed to varying light levels. This was effective at first, and my vehicle ran smoothly, however, for some reason, one photoresistor was getting much higher read values than the other. Despite trying to debug this and rewire the circuit, I decided to somewhat hardcode the machine. This actually turned out to be more effective, allowing the vehicle to be more attracted to light when the light in the environment was higher. I am sure that my code could have been more efficient.

Here is a video of the vehicle running in a dark room:

I also created a light-fearing version of the vehicle by changing the code by flipping the values for the wheel movement speeds. The vehicle does not work as well as the light-loving version, especially in environments that are moderately lit.

Here is the code for the light-loving version of the vehicle

const int pwma = 11 ;
const int in_1 = 13 ;
const int in_2 = 12 ;
//first wheel ^
const int pwmb = 10 ;
const int in_3= 8 ;
const int in_4 = 9 ;
//first wheel ^

const int photo1 = A4;
const int photo2 = A5;

int direct = 0;
int direct2 = 0;
void setup() {
// put your setup code here, to run once:
pinMode(pwma,OUTPUT) ; //we have to set PWM pin as output
pinMode(pwmb,OUTPUT) ; //we have to set PWM pin as output
pinMode(in_1,OUTPUT) ; //Logic pins are also set as output
pinMode(in_2,OUTPUT) ;
pinMode(in_3,OUTPUT) ; //Logic pins are also set as output
pinMode(in_4,OUTPUT) ;
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
direct = random(0, 1);
direct2 = (direct + 1) % 2;
int photoOneValue = map(analogRead(A4), 0, 1023, 0, 255);
int photoTwoValue = map(analogRead(A5), 0, 1023, 0, 255);
if (photoTwoValue > 105) {
digitalWrite(in_1, direct2) ;
digitalWrite(in_2, direct) ;
digitalWrite(in_3, direct2) ;
digitalWrite(in_4, direct) ;
analogWrite(pwma, 70);
analogWrite(pwmb, 250);
Serial.println("1");
} // speed in opposite direction to light
//previous value was 85 ^

else if (photoOneValue > 160) {
digitalWrite(in_1, direct2) ;
digitalWrite(in_2, direct) ;
digitalWrite(in_3, direct2) ;
digitalWrite(in_4, direct) ;
analogWrite(pwma, 250);
analogWrite(pwmb, 70);
Serial.println("2");
}
//previous value was 160 ^
else if (photoTwoValue < 20 && photoOneValue < 80) {
digitalWrite(in_1, direct2) ;
digitalWrite(in_2, direct) ;
digitalWrite(in_3, direct2) ;
digitalWrite(in_4, direct) ;
analogWrite(pwma, 110);
analogWrite(pwmb, 110);
Serial.println("3");
//constant slow speed when room is dim
}
else if (photoTwoValue > 20 && photoOneValue > 80 && photoOneValue < 160 && photoTwoValue < 105) {
digitalWrite(in_1, direct2) ;
digitalWrite(in_2, direct) ;
digitalWrite(in_3, direct2) ;
digitalWrite(in_4, direct) ;
analogWrite(pwma, photoOneValue + 20);
analogWrite(pwmb, photoTwoValue + 60);
Serial.println("4");
//environment not dim, but light not sensitive enough to activate the two if statements on top, allows vehicle to move faster towards light/
}
}

And below is the code for the “light-fearing” version of the vehicle:

const int pwma = 11 ;
const int in_1 = 13 ;
const int in_2 = 12 ;

const int pwmb = 10 ;
const int in_3= 8 ;
const int in_4 = 9 ;

const int photo1 = A4;
const int photo2 = A5;

int direct = 0;
int direct2 = 0;
void setup() {
// put your setup code here, to run once:
pinMode(pwma,OUTPUT) ; //we have to set PWM pin as output
pinMode(pwmb,OUTPUT) ; //we have to set PWM pin as output

pinMode(in_1,OUTPUT) ; //Logic pins are also set as output
pinMode(in_2,OUTPUT) ;
pinMode(in_3,OUTPUT) ; //Logic pins are also set as output
pinMode(in_4,OUTPUT) ;
Serial.begin(9600);
}

void loop() {
// put your main code here, to run repeatedly:
direct = random(0, 1);
direct2 = (direct + 1) % 2;
int photoOneValue = map(analogRead(A4), 0, 1023, 0, 255);
int photoTwoValue = map(analogRead(A5), 0, 1023, 0, 255);
if (photoTwoValue > 105) {
digitalWrite(in_1, direct2) ;
digitalWrite(in_2, direct) ;
digitalWrite(in_3, direct2) ;
digitalWrite(in_4, direct) ;
analogWrite(pwma, 250);
analogWrite(pwmb, 70);
Serial.println("1");
}
//previous value was 85 ^

else if (photoOneValue > 160) {
digitalWrite(in_1, direct2) ;
digitalWrite(in_2, direct) ;
digitalWrite(in_3, direct2) ;
digitalWrite(in_4, direct) ;
analogWrite(pwma, 70);
analogWrite(pwmb, 250);
Serial.println("2");
}
//previous value was 160 ^
else if (photoTwoValue < 20 && photoOneValue < 80) {
digitalWrite(in_1, direct2) ;
digitalWrite(in_2, direct) ;
digitalWrite(in_3, direct2) ;
digitalWrite(in_4, direct) ;
analogWrite(pwma, 110);
analogWrite(pwmb, 110);
Serial.println("3");

}
else if (photoTwoValue > 20 && photoOneValue > 80 && photoOneValue < 160 && photoTwoValue < 105) {
digitalWrite(in_1, direct2) ;
digitalWrite(in_2, direct) ;
digitalWrite(in_3, direct2) ;
digitalWrite(in_4, direct) ;
analogWrite(pwma, photoTwoValue + 60);
analogWrite(pwmb, photoOneValue + 20);
Serial.println("4");
}
}