Pages: [1]   Go Down
Author Topic: Turn LED on if photoresistor has detected light for 5 minutes  (Read 867 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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); 
 
}
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 212
Posts: 8974
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
    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
     }
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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));
     }
    
  
}

Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3446
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: March 20, 2013, 12:46:14 pm by Arrch » Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4835
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
   if (analogRead(analogPin) == 400)
        lastTimeInputWasZero = millis();
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 12
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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));
     }
   
 
}

Logged

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3446
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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...
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4835
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:

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:
    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.
Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Pages: [1]   Go Up
Jump to: