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