The bot moves forward when it is "dark" (as defined by a threshold compared to the value from a photoresistor), moves in reverse while turning for approximately one second when one of two buttons has been pressed (if the bot has bumped into something in the forward direction) to avoid obstructions and stops moving once it detects light. The DC motor control was achieved by creating an h-bridge circuit out of mosfets using the schematic found at the bottom of this page. The mosfets act as a series of switches that when activated in the correct order will allow current to flow in one of two directions, effectively flipping power and ground to allow the motor to move both forwards and backwards. The h-bridge ciruit was isolated almost entirely, aside from it’s common ground wire with the Arduino. The motor control board also contains the circuit for the two buttons and the photoresistor circuits both of which are connected via a ribbon cable that I created and some additional leads from the 5v lead from the Arduino board and the analog input to the Arduino from the photoresistor. The code is fairly straightforward in that I tried to create a series of instructions that could be executed sequentially to avoid timing issues. My solution was to use a series of flags to keep track of the state of the bot and clear them after a specified duration using the timing function featured in the Arduino Tutorials here. One issue that I’ve noticed is that the millis() function is not particularly accurate. While testing I’ve noticed that when I specify a one-second delay it seems to vary seemingly randomly between almost no delay to a delay slightly over one second. The Arduino reference claims that it’s accurate enough that it won’t overflow for 50 days, but I’m somewhat sketptical. go in the dark bot behavior outline:
- If it is dark:
- If the bot has not bumped something, move forward
- If the bot has bumped something, move backward (and turn) for one second
- If it is light:
- stop
int photoPin = 0;
int photoVal = 0;
int photoSensitivity = 150;
int buttonInPin = 9;
int buttonVal = 0;
int buttonFlag = 0;
int a = 13;
int b = 12;
int c = 11;
int d = 10;
long previousMillis = 0;
long interval = 1000;
void stop(){
digitalWrite(a, HIGH);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
}
void forward(){
digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
}
void reverse(){
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
}
void setup(){
beginSerial(9600);
pinMode(photoPin, INPUT);
pinMode(buttonInPin, INPUT);
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
}
void loop(){
buttonVal = digitalRead(buttonInPin);
if(buttonVal == HIGH){
buttonFlag = 1;
}
if (millis() - previousMillis > interval){
previousMillis = millis();
buttonFlag = 0;
}
//printInteger(buttonFlag);
//printNewline();
photoVal = analogRead(photoPin);
//printInteger(photoVal);
//printNewline();
if(photoVal <= photoSensitivity){
if(buttonFlag == 0){
printString("forward");
printNewline();
forward();
}
if(buttonFlag == 1){
printString("backward");
printNewline();
reverse();
}
}
if(photoVal > photoSensitivity){
printString("stop");
printNewline();
stop();
}
}



