So I have created a circuit so that when an ldr is in the sunlight, LED’s light up and the motor is operating for X amount of seconds. After this, the motor then switches off. However, the loop will just keep playing. I want to stop this loop until the ldr is in the shade and another loop will be executed and vice versa.
I currently do not know how to do this ( apologies I’m releatively new to programming). I’ve added while(1); but this means that the loop will be executed once and then stop. Even if I change the input of the ldr, the whole program stops.
Here is my program:
int input1=3;
int input2=4;
const int led1=5;
const int led2=7;
const int ldr1=0;
void setup()
{
pinMode(input1, OUTPUT);
pinMode(input2, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(ldr1, INPUT);
}
void loop()
{
int ldrstatus=analogRead(ldr1);
if (ldrstatus<=300)
{
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(input1, LOW);
digitalWrite(input2, HIGH);
delay(500);
digitalWrite(input2,LOW);
delay(500);
while(1);
}
else if (ldrstatus>300)
{
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(input1, HIGH);
digitalWrite(input2, LOW);
delay(500);
digitalWrite(input1, LOW);
delay(500);
while(1);
}
}