Trying to get a DC motor to change direction based on photocell reading

I am a noob but really trying to figure it all out. I can get both things working in isolation, but not sure what to do next.

int enablePin = 11;
int in1Pin = 10;
int in2Pin = 9;
int switchPin = 7;
int potPin = 0;

//photocell

int photocellPin = 1; // the cell and 10K pulldown are connected to a0
int photocellReading = 50; // the analog reading from the sensor divider

void setup()
{
Serial.begin(9600);
pinMode(in1Pin, OUTPUT);
pinMode(in2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
}

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

analogWrite(enablePin, speed);
if (photocellReading > 100){
digitalWrite(in1Pin, ! reverse);
delay (4000);
analogWrite(enablePin, (0));
}
if (photocellReading < 80){
digitalWrite(in2Pin, reverse);
delay (4000);}
}

I can get both things working in isolation, but not sure what to do next.

What are the "both things"?

What is the problem with the code you posted? What does it actually do? How does that differ from what you want?

Sorry, new to forums too :slight_smile:

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.

So, what will the switch pin be doing?

Setting the direction based on whether the reading is over 100 or less than 80 is possible, but what should the direction be if the reading is 90?

Are you powering the Motor directly off the Pins?
I think it would be best if you provided a schematic of how things are hooked up.

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

}