What I would like is for the motor to turn in one direction, for 5 seconds, with a photocell reading of say less than 80.
Turn the other way, also for 5 seconds, with a reading over 100.
I can get reading from the photocell, and using an h-bridge reverse a dc motor, but not sure how to combine the two things.
The code above is more or less a splice of the two and turns in only one direction as if only using only the first time the photocell is sampled.
Thanks Paul,
I took the switch pin part out. The ultimate goal is a door that opens in the am light and closes in the pm dark. Eventually I will have take 5 mins or so of data and average it (that is easy enough). If it's at 90 it can just sit there.
I think with the h-bridge you need to write one pin HIGH and the other LOW to make the motor turn, you are only writing to one pin at a time. The value you are writing is based on the switchPin which seems to have no meaning.
I advise you to put meaningful comments in your code, that will help you see if the logic matches what you are trying to do. Below is my interpretation of what you are trying to do, it may not be correct but might give you some ideas on how to proceed.
void loop()
{
photocellReading = analogRead(photocellPin);
Serial.print("Analog reading = ");
Serial.println(photocellReading);
Serial.print("Speed = ");
int speed = 240;
Serial.println(speed);// the raw analog reading
boolean reverse = digitalRead(switchPin);
// if reading is not between 80 and 100
if( (photocellReading < 80) || (photocellReading > 100) )
{
// reverse if less than 80
reverse = (photocellReading < 80) ? LOW : HIGH;
// enable motor
analogWrite(enablePin, speed);
// run motor
digitalWrite(in1Pin, !reverse);
digitalWrite(in2Pin, reverse);
delay (4000);
// stop motor
analogWrite(enablePin, 0);
digitalWrite(in1Pin, LOW);
digitalWrite(in2Pin, LOW);
} // if
/*
analogWrite(enablePin, speed);
if (photocellReading > 100){
digitalWrite(in1Pin, ! reverse);
delay (4000);
analogWrite(enablePin, (0));
}
if (photocellReading < 80){
digitalWrite(in2Pin, reverse);
delay (4000);
}
*/
}