I'm building a photovore bot and would like to implement a start/stop/lurching behavior, where it runs for a second, then stops and takes a light reading. After the reading it should decide if it should continue straight because the values were too low or turn (spin one wheel) or turn off completely b/c the values are good. However, I'm having trouble achieving this (I have no coding experience and everything I have learned has been through tutorials online). I assume I don't know where to place the 'delay' function within my sketch. Any help would be appreciated.
Using Arduino Duemilanove with Arduino 18. One half (single wheel/motor) of my attempted sketch is below.
//Photovore
int LDR = 2; // select the input pin for the LDR
int val = 0; // variable to store the value coming from the sensor
int LDR_right = 3; // select the input pin for the LDR
int val_right = 0; // variable to store the value coming from the sensor
int motor1Pin = 5; // H-bridge leg 1 (pin 2, 1A)
int motor2Pin = 4; // H-bridge leg 2 (pin 7, 2A)
int enablePin = 3; // H-bridge enable pin
int motor1Pin_right = 13; // H-bridge leg 1 (pin 2, 1A)
int motor2Pin_right = 12; // H-bridge leg 2 (pin 7, 2A)
int enablePin_right = 11; // H-bridge enable pin
void setup() {
pinMode(LDR, INPUT); // declare the LDR as an INPUT
pinMode(LDR_right, INPUT);
// set all the other pins you're using as outputs:
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(motor1Pin_right, OUTPUT);
pinMode(motor2Pin_right, OUTPUT);
pinMode(enablePin_right, OUTPUT);
// set enablePin high so that motor can turn on:
// digitalWrite(enablePin, HIGH); NEEDED?
// digitalWrite(enablePin_right, HIGH); NEEDED?
}
void loop()
{
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH);
delay (1000);
val = analogRead(LDR); // read the value from the sensor
if (val < 150) {digitalWrite(enablePin, HIGH); // if LDR reads a value higher lower than 150, ON
}
else digitalWrite(enablePin, LOW); // if LDR reads a value lower than 150, OFF