Using a dc motor with a photoresistor

Hi I'm still new to Arduino coding and i am trying to make a dc motor turn for 4 seconds when the photoresistor reaches the threshold then stop until the the light level goes under the threshold the turn the other direction for the same time any help would be appreciated

int photoresistor = A0;
const int AIN1 = 13;
const int AIN2 = 12;
const int PWMA = 11;


#define mosfetPin 9
#define LDRPin     A0
int value1 = 900;
int value2 = 1023;

void setup() {
  Serial.begin(9600);
  pinMode(LDRPin, INPUT);
  pinMode(mosfetPin, OUTPUT);
  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT) ;
  pinMode(PWMA, OUTPUT) ;
  
  

}

void loop(){
  int threshold = 900;
  photoresistor = analogRead(A0);
  if (photoresistor > threshold) {
    digitalWrite(AIN1, HIGH) ;
    digitalWrite(AIN2, LOW) ;
    analogWrite(PWMA, 255) ;
    delay(4000);
    digitalWrite(AIN1, LOW) ;
    digitalWrite(AIN2, LOW) ;
    analogWrite(PWMA, 0);
    
  }
  else {
    digitalWrite(AIN1, LOW) ;
    digitalWrite(AIN2, HIGH) ;
    analogWrite(PWMA, 255) ;
    delay(4000);
    digitalWrite(AIN1, LOW) ;
    digitalWrite(AIN2, LOW) ;
    analogWrite(PWMA, 0);
  }
  }

Please follow the advice given in the link below when posting code , use code tags and post the code here to make it easier to read and copy for examination

Meet a bug-in-waiting.

Please remember to use code tags when posting code

So what actually happens when you run the code?
Also what has the word tangent in the title got to do with this?

so sorry first time posting but for now when I upload this to the Arduino the dc motor will spin clockwise without stopping until I cover the photoresistor the it will spin the opposite direction without stopping all i would want it to do would be to spin for 4 seconds when it is light enough then stop but when it gets dark enough it will spin the opposite direction for 4 seconds then stop

code needs to recognize two states, one where it turns forward and after that has happened, changes state and considers the other direction

  if (state) {
    if (photoresistor > threshold) {
        ...
        state = ! state;
    }
  }
  else  {
    if (photoresistor < threshold) {
        ...
        state = ! state;
    }
  }

Well it spins all the time because your loop function repeats continuously. So once it has spun in one direction after four second it stops. But then almost immediately it loops round again and spins for another four seconds. That is why you never see the motor stop.
To see it stop for four seconds then add a delay after you turn it off. It will still loop round and start the motor after that.

The simplest way to do what you want is to add a while loop after the spin that waits until the light reading drops below the threshold. And a similar loop that waits for the reading to rise above the threshold after the spin in the other direction.

Hi I have been trying to learn and use the states like you suggested and it the first part of the loop works but dose not spin the other direction once it gets dark and I can't figure out what I'm doing wrong any help would be appreciated

int threshold = 900;
int underthreshold = 900;

void loop() {
  if (threshold > 900) {
    photoresistor = analogRead(A0);
    if (photoresistor > threshold) {
      digitalWrite(AIN1, HIGH) ;
      digitalWrite(AIN2, LOW) ;
      analogWrite(PWMA, 255) ;
      delay(4000);
      digitalWrite(AIN1, LOW) ;
      digitalWrite(AIN2, LOW) ;
      analogWrite(PWMA, 0);
      underthreshold = ! threshold;
    }
  }
  else {
    if (photoresistor < threshold) {
      digitalWrite(AIN1, LOW) ;
      digitalWrite(AIN2, HIGH) ;
      analogWrite(PWMA, 255) ;
      delay(4000);
      digitalWrite(AIN1, LOW) ;
      digitalWrite(AIN2, LOW) ;
      analogWrite(PWMA, 0);
      threshold = ! underthreshold;
    }
  }
}

is not

What does that mean?

Note, your else clause does not read the photoresistor

You are not doing what I said in the last paragraph of reply #7.
Where is the while statement?
You don’t have one, or in fact the two you need.

AND still no delay after you turn the motors off!

consider

const int AIN1 = 13;
const int AIN2 = 12;
const int PWMA = 11;
const int Photo = A0;

int  photoresistor = Photo;
int  threshold = 900;
bool state;

void loop() {
    photoresistor = analogRead(Photo);

    if (state)  {
        if (photoresistor > threshold) {
            digitalWrite(AIN1, HIGH) ;
            digitalWrite(AIN2, LOW) ;
            analogWrite(PWMA, 255) ;
            delay(4000);

            digitalWrite(AIN1, LOW) ;
            digitalWrite(AIN2, LOW) ;
            analogWrite(PWMA, 0);

            state = ! state;
            Serial.println (state);
        }
    }
    else {
        if (photoresistor < threshold) {
            digitalWrite(AIN1, LOW) ;
            digitalWrite(AIN2, HIGH) ;
            analogWrite(PWMA, 255) ;
            delay(4000);

            digitalWrite(AIN1, LOW) ;
            digitalWrite(AIN2, LOW) ;
            analogWrite(PWMA, 0);

            state = ! state;
            Serial.println (state);
        }
    }
}

void setup() {
    Serial.begin(9600);

    pinMode(AIN1, OUTPUT);
    pinMode(AIN2, OUTPUT);
    pinMode(PWMA, OUTPUT);

    Serial.println (analogRead (Photo));
}

Why?

Photo is the pin

But photoresistor is not the pin number, its the logic level.

Anyway you don't need a global variable for photoresistor (you don't even need any variable for it, you can just go:

   if (digitalRead (Photo) > threshold)
     ....

right.

is an unnecessary assignment

Thanks for all you guys patience and help it works great now

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.