Photovore - Trouble with delays

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

Oh, here is my original sketch (before playing with delays). Needed to move towards delays b/c the bot would move so fast I couldn't control it.

//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);
}

void loop() {
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
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH); // set leg 2 of the H-bridge high
}
else digitalWrite(enablePin, LOW); // if LDR reads a value lower than 150, OFF

val_right = analogRead(LDR_right); // read the value from the sensor
if (val_right < 150) {digitalWrite(enablePin_right, HIGH); // if LDR reads a value higher lower than 150, ON
digitalWrite(motor1Pin_right, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin_right, HIGH); // set leg 2 of the H-bridge high
}
else digitalWrite(enablePin_right, LOW);

}

Hey guys,
Fooled around with delay and simplified my sketch to just have the motors run for 1 second the pause for 1 second. Still not sure how to implement the delays with my if/else statements.

void loop()
{
digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, HIGH);
delay (1000);

digitalWrite(motor1Pin, LOW); // set leg 1 of the H-bridge low
digitalWrite(motor2Pin, LOW);
delay (1000);

Really simple but I at least I think I'm on the right track now!

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

This code (and much that follows) would make more sense if LDR and val were renamed LDR_left and val_left (presuming that they really correspond to the other side of the robot).

You read from one sensor, and turn, if that reading is low. Then, you read from the other sensor, and turn again, if that reading is low.

Wouldn't it be better to read both sensors, and then turn based on the relative values of the two readings?

I agree, that would be a better way of attacking this problem. However, I actually have no clue how to do this.

The course I'm taking doesn't dive much further into code than the blinking LED tutorial in the Arduino Playground. So everything I have posted/developed has been trial and error. Thus the reason I'm trying to create a solution with what I already sort of know.

You know how to read the sensors. Just move the 2nd read right after the first one.

You know how to turn left or right, if a sensor is below a threshold. Change that to something like this:

if(val_left < val_right)
{
   // Turn one way
}
else if (val_left > val_right)
{
   // Turn the other way
}
else
{
   // Readings are equal. Go straight
}

Going to play with this right now. Thanks a lot. :slight_smile: