Pages: [1]   Go Down
Author Topic: I give up on debouncing light  (Read 580 times)
0 Members and 1 Guest are viewing this topic.
South East USA
Offline Offline
God Member
*****
Karma: 5
Posts: 658
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I just can't quite get my brain around this photocell debouncer.  All I need is a boolean that is true if it's light, and false if it's dark.  Light and dark being set by MySettings.Photocell_Level.  I don't want the boolean to oscillate when the light level is close to the threshold, though, so I thought I'd add a 5 second debouncer to it.  I've struggled too long on this one...
This is a mess, but:
Code:
void Photocell(){
  boolean newLightLevel;
  boolean prevLightLevel;
  long lightDebounceTime;
  int senseVal = analogRead(photocellPin);  //get the pin reading
  Serial.print("photocell reading: ");
  Serial.println(senseVal);
  if (senseVal > 62)  newLightLevel = true;  //light level is higher than threshold  MySettings.Photocell_Level
  else  newLightLevel = false;  //it's darker than threshold

  if (newLightLevel != prevLightLevel) {    // lighting has changed
    lightDebounceTime  = millis();    // reset the debouncing timer
    prevLightLevel = newLightLevel; //the previous light is now the current reading
  }
    if (millis() - lightDebounceTime > 5000) { //It's stayed either light or dark for 5 seconds
      Serial.print("Level changed to :");
      lightLevel = newLightLevel;  //True for light, False for dark
    }     
      if(lightLevel) Serial.println("LIGHT");
      else Serial.println("DARK");

}

By all means, if there's a better way to go about this, please do tell!
Logged

Lancashire, UK
Offline Offline
Edison Member
*
Karma: 9
Posts: 1991
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

light1 is of type float;

Light sensors don't bounce, so its called smoothing;

Code:
light1 = light1 * 0.9 + float(analogRead(light1Pin)) * 0.1;

Adjust the 0.9 & 0.1 to suit but together they should add up to 1.

« Last Edit: April 20, 2011, 12:22:23 am by pluggy » Logged


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

Perhaps you should just set different thresholds for OFF and ON:
Code:
void Photocell()
  {
  boolean prevLightLevel = lightLevel;

  int senseVal = analogRead(photocellPin);  //get the pin reading
  if (senseVal > 62)
      lightLevel = true;  //light level is higher than light threshold  MySettings.Photocell_Level
  else
  if (senseval < 54)
      lightLevel = false;  //it's darker than dark threshold

  if (lightLevel != prevLightLevel)
   {    // lighting has changed
   if(lightLevel)
      Serial.println("LIGHT");
    else
      Serial.println("DARK");
   prevLightLevel = lightLevel;
   }
}
Logged

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

South East USA
Offline Offline
God Member
*****
Karma: 5
Posts: 658
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps you should just set different thresholds for OFF and ON:
Code:
void Photocell()
  {
  boolean prevLightLevel = lightLevel;

  int senseVal = analogRead(photocellPin);  //get the pin reading
  if (senseVal > 62)
      lightLevel = true;  //light level is higher than light threshold  MySettings.Photocell_Level
  else
  if (senseval < 54)
      lightLevel = false;  //it's darker than dark threshold

  if (lightLevel != prevLightLevel)
   {    // lighting has changed
   if(lightLevel)
      Serial.println("LIGHT");
    else
      Serial.println("DARK");
   prevLightLevel = lightLevel;
   }
}
thanks, something like this worked good.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 76
Posts: 7305
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Just in case you decide to later use a digital photo-sensor, like a photo-transistor with a comparator, all you need is the button debouncing code if the output is bouncy.
Logged


Pages: [1]   Go Up
Jump to: