Hey, I'm doing a project in school where I will do a game where a LDR is needed to turn on and off some LEDs. Basically; When you put your hand over the LDR the value lowers, and I have made an if-statement and then a for-loop telling the LEDs to turn on when the LDR value is lower than the treashold. I want it to delay randomly between some seconds before the LEDs are turned on, that is all done. But my problem now is, when i remove my hand from the LDR it keeps delaying the same amout of time that the LEDs were turned on.
So, how can I make it so that when I take away my hand from the LDR the LEDs turns off again immediatley?
I use the Serial.begin(); and then i use the delay the intire serial monitor is delayed.
Here is my code:
int sens = 0;
int redPin[] = {2, 3};
int place = 2;
boolean set = HIGH;
void setup()
{
Serial.begin(9600);
for(int i = 0; i<place; i++)
{
pinMode(redPin[i], OUTPUT);
}
}
void loop()
{
int val = analogRead(sens);
Serial.println(val);
delay(450);
if(val<240)
{
delay(random(1000, 5000));
for(int i = 0; i<place; i++)
{
digitalWrite(redPin[i], set);
}
}
else
{
for(int i = 0; i<place; i++)
{
digitalWrite(redPin[i], !set);
}
}
}