if you use a limit switch that’s even better… then you don’t need to get the motor to work for 2 seconds which is probably an approximation, you know it’s closed when the switch activates.
My boolean is just a trick to memorize the state, which you forgot to initialize in the set up as I had suggested
if ((light > 512) && (!doorIsClosed)) //clair et porte ouverte
else if ((light < 512) && (doorIsClosed)) //noir et porte fermée
are useless, they do nothing but just wait… it’s not necessary… get rid of them
Why do you insist on maintaining just 512… we have explained a couple times already, and you will find out soon enough, that the value you will get from analogRead will fluctuate around 512 when it’s about time to open or close the door… if you don’t keep a gap between the 2 values, your door will constantly open/close open/close for a while until it’s real dark or real day… that’s why we suggest to close the door if <500 and open it if >530 for example. the 30 units difference won’t be tested for in your code so the door will stay wherever it has been set while the value oscillate around 512
you should trust your code, that the left and right pins will always end up LOW when the code executes, so there is no need to set the one that is not used to LOW when you start operating the relay.
You should use the const int type for the pins definition
const int left = 7; //pin of the 1st relay (motor counterclockwise)
const int right = 8; // pin of the 2nd relay (motor clockwise)
const int input = A0; //pin of the photoresistor
(and as suggested above, use better variable names)
Your LEDs stay on because you wired your relay incorrectly - aren’t you intrigued by all the cables at the output of the 2 relays… Why are you connecting both the Normally Open and the Normaly Closed terminals?
(not sure my indications are for the right relay, but this is the idea of a relay. when the command is HIGH the normally Open socket is connected to the central wire, when it’s low it’s disconnected - just like an interruptor. on the other side, the normally closed one, when the command is LOW the socket is connected to the center wire, and when it is HIGH it’s disconnected)
Only one side of the connector should be used otherwise your relay is always connecting the central wire to a wire on the side and thus your LED is ON.
So I’m unsure what is at the end of those cables, but BE SUPER CAREFUL. it’s not wired the right way and current might go through your system all the time depending on what is at the end of those cables on both sides… Don’t touch those wires (And they seem a little small for high power compared to the thickness of the one connected to the motor. What V / I are you needing for the motor??)