Help with activating LEDs after 2.5 mins and 5 mins using LDR

Hi,

I’m trying to create the program so that a LED flashes to show the system works then when it turns dark the first LED turns solid, and after 2.5 mins the second LED turns on and after 5 mins the final LED also turns on. When it becomes light again I need the system to reset to the flashing LED, ready for it to turn dark again. I’m quite new to Arduino. So far I have this:

int sensorPin = A0;
unsigned int sensorValue = 0;

void setup() {
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
pinMode(9,OUTPUT);
pinMode(8,OUTPUT);
Serial.begin(9600);
}

void loop() {
sensorValue = analogRead(sensorPin);
if(sensorValue<700) {
(digitalWrite(13,LOW));
delay(1000);
digitalWrite(13,HIGH);
delay(1000);

}
else(digitalWrite(13,HIGH));

}

Any help would be greatly appreciated! Thanks!

int sensorPin = A0;

You don’t have a generic sensor attached to this pin, do you? More likely, you have a very specific kind of sensor attached. The variable name should reflect the actual sensor attached.

It’s less obvious why this matters when you have just one sensor, but start adding a temperature sensor and a PIR sensor, and other sensors, and keeping track of what is attached to sensorPin1, sensorPin2, and sensorPin3 requires a cheat sheet. Keeping track of what is attached to tempPin, lightPin, and motionPin can generally be done without a cheat sheet.

So far I have this:

Which does something. You haven’t explained what.

You have one second delays in there. They don’t match your thread title/requirement times.

Hi,

Ahh the sensor is LDR. How do I go about changing the name of the sensor to lightPin? Sorry about that, the program currently flashes on for 1 second and off for 1 second if it is light. When the LDR is covered and it becomes dark the LED stays on during darkness. I don't know how I would go about now activating the 2nd LED after 2.5 mins of darkness and the 3rd LED after 5 mins of darkness. And then having the program reset to just the 1st LED flashing when it is light again.

Thank you!

you can try like this untested bit of code.

you pins led themselves to be part of an array, which I used along with a millis() timer:

#define DEBUG // comment this out to use real time

#ifdef DEBUG
#define FIRST_INTERVAL 10000UL //change to 10s
#define SECOND_INTERVAL 20000UL //change to 20s
#else
#define FIRST_INTERVAL 150000UL
#define SECOND_INTERVAL 300000UL
#endif

int ledPin[6] = {8,9,10,11,12};

const int sensorPin = A0;
int sensorValue = 0;
int state = 0;
unsigned long millisStartTime;

void setup() 
{
  Serial.begin(9600);
  for (int i =0; i < 6; i++)
  {
    pinMode(ledPin[i],OUTPUT);
  }

}

void loop() 
{
  sensorValue = analogRead(sensorPin);
  if(sensorValue < 700 && state == 0) 
  {
    millisStartTime = millis();
    state = 1;
  }
  if (sensorValue > 750 && state == 1) // dead band for hysteresis
  {
    state = 0;
    for (int i = 0; i < 3; i++)
    {
      digitalWrite(ledPin[i], LOW);
    }
  }
  if (state)
  {
    doTheLedThingy();
  }
}

void doTheLedThingy()
{
  if(millis() - millisStartTime < FIRST_INTERVAL)
  {
    digitalWrite(ledPin[0], HIGH);
  }
  else if (millis() - millisStartTime < SECOND_INTERVAL)
  {
    digitalWrite(ledPin[1], HIGH);
  }
  else 
  {
    (digitalWrite(ledPin[2], HIGH));
  }
}

How do I go about changing the name of the sensor to lightPin?

Highlight the name, and type a new name.

I don't know how I would go about now activating the 2nd LED after 2.5 mins of darkness

You need to record when the event of interest occurs (the light level falls below some value). Then, separately, on every pass through loop(), you see if it is dark (the light level is below some value) and if is has been dark long enough. If it has, turn the appropriate LED on.

The state change detection example will bear looking at, so you can tell "it just became dark" (so you need to record the time) vs. "it is dark" (and has been for 1 minute, 2 and half minutes, 3 hours, etc.).

The blink without delay example will bear looking at, so you can see how to do things at specific intervals after other things happened (2 and a half minutes after it got dark).

Edit: Fixed the quote that failed to copy correctly.

for (int i =0; i < 6; i++)

Oops

Oops

No, that's OK. There are 6 elements in the array (even though there are only 5 initializers).

The part that I need help with now, is logging data to Access and then showing that data on a nicely designed screen. Does that sound reasonable?

Highlight the name, and type a new name.

Copy/paste error ?

PaulS:

Oops

No, that's OK. There are 6 elements in the array (even though there are only 5 initializers).

But that means setting pin zero to be an output.

But that means setting pin zero to be an output.

I never said there wasn’t a problem. But, the array bounds issue you were alluding to (at least that’s what I assumed you were alluding to) isn’t the issue.

Copy/paste error ?

F**king windows. Ctrl-C doesn't work half the time. I need to pay attention to what I actually paste. I feel like going down the street and Ctrl-Ving someone...

AWOL:

for (int i =0; i < 6; i++)

Oops

I was tired… :blush:

another attempt :smiley:

#define DEBUG // comment this out to use real time

#ifdef DEBUG
#define FIRST_INTERVAL 10000UL //change to 10s
#define SECOND_INTERVAL 20000UL //change to 20s
#else
#define FIRST_INTERVAL 150000UL //change to 10s
#define SECOND_INTERVAL 300000UL //change to 20s
#endif

int ledPin[5] = {8,9,10,11,12};

const int sensorPin = A0;
int sensorValue = 0;
int state = 0;
unsigned long millisStartTime;

void setup() 
{
  Serial.begin(9600);
  for (int i =0; i < 5; i++)
  {
    pinMode(ledPin[i],OUTPUT);
  }

}

void loop() 
{
  sensorValue = analogRead(sensorPin);
  if(sensorValue < 700 && state == 0) 
  {
    millisStartTime = millis();
    state = 1;
  }
  if (sensorValue > 725 && state == 1)
  {
    state = 0;
    for (int i = 0; i < 3; i++)
    {
      digitalWrite(ledPin[i], LOW);
    }
  }
  if (state)
  {
    doTheLedThingy();
  }
}

void doTheLedThingy()
{
  if(millis() - millisStartTime < FIRST_INTERVAL)
  {
    digitalWrite(ledPin[0], HIGH);
  }
  else if (millis() - millisStartTime < SECOND_INTERVAL)
  {
    digitalWrite(ledPin[1], HIGH);
  }
  else 
  {
    (digitalWrite(ledPin[2], HIGH));
  }
}