Go Down

Topic: Turn LED on if photoresistor has detected light for 5 minutes (Read 946 times) previous topic - next topic

Brooks

Good Afternoon,

I am trying to determine how to turn an LED on after a Photo resistor has detected a value of 1 or greater for 5 minutes.

My assumption is that I need to increment a variable, if analog input is > 0 and then use delay(1000) to pause for one second and then check the analog input.  Once the Variable reaches 300 (5 minutes) than set the LED pin to high.

A couple of items that I am struggling with:

1.) How do I reset the variable back to 0 if the analog value is 0 before the variable reaches 300.
2.) Once the variable reaches 300 I want to turn the LED on and stop the sketch until the arduino is reset or the analog input is at 0.

Here is what I have so far.  Any help would be greatly appreciated.
--------------------------

const int analogPin = A0;    // Photo Resistor
const int ledPin = 13;     

int timerval = 0;


void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop() {
  // read the value of the Photo Resistor:
  int analogValue = analogRead(analogPin);

  // if the timerval = 300, turn on the LED or increment timerval by 1:
  if (analogValue > 0) {
    ++timerval;
    delay(1000);

    if (timerval = 300){
    digitalWrite(ledPin,HIGH);
    }
   
  }
  else {
    digitalWrite(ledPin,LOW);
    timerval = 0;
    delay(1);
  }

  Serial.println(analogValue);
  Serial.println(timerval); 

}

johnwasser

Code: [Select]

    static unsigned long lastTimeInputWasZero = 0;
    if (analogRead(analogPin) == 0)
        lastTimeInputWasZero = millis();

    if (millis() - lastTimeInputWasZero > 5UL*60UL*1000UL) {
        // It has been five minutes or more since the last time the input was 0
    }
    else {
        // It has been less than five minutes since the last time the input was 0
     }
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Brooks

Thank you very much for this simple solution.

I am now having an issue that when the Arudino is power cycled or turned on for the first time, the second 'if' statement runs five or six times in a row.  What are your thoughts on this?

Here is what I have now:

Quote


int analogPin = A0;    // Photo Resistor
int ledPin = 13;

int timerval = 0;


void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop()
{

 static unsigned long lastTimeInputWasZero = 400;
    if (analogRead(analogPin) == 400)
        lastTimeInputWasZero = millis();

    if (millis() - lastTimeInputWasZero > 5UL*60UL*1000UL) {
        // It has been five minutes or more since the last time the input was 0
        digitalWrite(ledPin,HIGH);
        Serial.println("LED ON");
    }
    else {
        // It has been less than five minutes since the last time the input was 0
        digitalWrite(ledPin,LOW);
        //Serial.println(analogRead(analogPin));
     }
    
  
}


Arrch

#3
Mar 20, 2013, 06:44 pm Last Edit: Mar 20, 2013, 06:46 pm by Arrch Reason: 1

I am now having an issue that when the Arudino is power cycled or turned on for the first time, the second 'if' statement runs five or six times in a row.  What are your thoughts on this?

Your initial value of lastTimeInputWasZero makes no sense.

You.also changed the analog value to 400, which means the variable name lastTimeInputWasZero doesn't accurately describe it.

GoForSmoke

First of all, the thing in the IDE that says it's for cut&paste to the forum is wrong. The code tags button is above the post edit window above the smileys and has a # sign on it.

This line ensures that only when the analogRead is exactly 400 will the lastTime be set to millis(). Is that really what you want?

Code: [Select]

   if (analogRead(analogPin) == 400)
       lastTimeInputWasZero = millis();
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Brooks

I changed the variable to accurately describe the value.

I have a threshold of about 400 or less with the photo resistor when it is "dark" .  Anything over 400 is assumed to have detected light.  It will not be exactly 400 though.  What are your thoughts?

Code: [Select]


int analogPin = A0;    // Photo Resistor
int ledPin = 13;

int timerval = 0;


void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop()
{

static unsigned long lastTimeInputWaslessThanFourHundred = 400;
    if (analogRead(analogPin) == 400)
        lastTimeInputWaslessThanFourHundred = millis();

    if (millis() - lastTimeInputWaslessThanFourHundred > 5UL*60UL*1000UL) {
        // It has been five minutes or more since the last time the input was 0
        digitalWrite(ledPin,HIGH);
        Serial.println("LED ON");
    }
    else {
        // It has been less than five minutes since the last time the input was 0
        digitalWrite(ledPin,LOW);
       Serial.println(analogRead(analogPin));
     }
   
 
}


Arrch


static unsigned long lastTimeInputWaslessThanFourHundred = 400;


I still don't understand why you are assigning 400 as the initial time at which an input was read. 400 is a sensor reading threshold value, not a time.

Quote
(analogRead(analogPin) == 400)


Quote
lastTimeInputWaslessThanFourHundred


These two things don't match...

GoForSmoke


I changed the variable to accurately describe the value.

I have a threshold of about 400 or less with the photo resistor when it is "dark" .  Anything over 400 is assumed to have detected light.  It will not be exactly 400 though.  What are your thoughts?

Code: [Select]


int analogPin = A0;    // Photo Resistor
int ledPin = 13;

int timerval = 0;


void setup()
{
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communications:
  Serial.begin(9600);
}

void loop()
{

static unsigned long lastTimeInputWaslessThanFourHundred = 400;
    if (analogRead(analogPin) == 400)
        lastTimeInputWaslessThanFourHundred = millis();

    if (millis() - lastTimeInputWaslessThanFourHundred > 5UL*60UL*1000UL) {
        // It has been five minutes or more since the last time the input was 0
        digitalWrite(ledPin,HIGH);
        Serial.println("LED ON");
    }
    else {
        // It has been less than five minutes since the last time the input was 0
        digitalWrite(ledPin,LOW);
       Serial.println(analogRead(analogPin));
     }
   
 
}




My one thought is that "400 or less" would look more like:
Code: [Select]

    if (analogRead(analogPin) <= 400)


My other thought is that you need to practice examples more before you go for a project.

Those are my thoughts.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

Go Up