0

How to count +1 all the time if LED blinks with LDR sensor

Image: enter image description here

If i turn on led it must count into serial monitor 1 and if i turn off led and turn on again then it counts 2.

To turn on led I must put my hand into LDR sensor.

It also looping text in serial monitor "First LDR sensor is Dark".

int ledPin1 = 7;   
int ledPin2 = 8;  
int ledPin3 = 9;   
int ldrPin1 = A0;  
int ldrPin2 = A1;
int ledTest = 200;
int ldrStatus1 = 0;
int ldrStatus2 = 0;
int ledCheck1 = 0;
int ledCheck2 = 0;
int Count = 0;

void setup() {

  Serial.begin(9600);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);

  pinMode(ldrPin1, INPUT);
  pinMode(ldrPin2, INPUT);
}

void loop() {

  ldrStatus1 = analogRead(ldrPin1);
  ldrStatus2 = analogRead(ldrPin2);

   if (ldrStatus1 <= ledTest) {
    digitalWrite(ledPin1, HIGH);               
    Serial.println("First LDR is Dark");
    ledCheck1 = 1;
    Count++;
    Serial.println(Count);
   } else {
    digitalWrite(ledPin1, LOW);  
    ledCheck1 = 0;
   }

   if (ldrStatus2 <= ledTest) {
    digitalWrite(ledPin3, HIGH);               
    Serial.println("Second LDR is Dark");
    ledCheck2 = 1;
    Count++;
    Serial.println(Count);
   } else {
    digitalWrite(ledPin3, LOW);
    ledCheck2 = 0;
   }

   if ((ledCheck1 == 1) && (ledCheck2 == 1)) {
    digitalWrite(ledPin2, HIGH);
    Count++;
    Serial.println(Count);
   } else {
    digitalWrite(ledPin2, LOW);
   }

}

At the moment it counts when it is turned on, like seconds.. How to fix or make it work correctly?

closed as unclear what you're asking by Michel Keijzers, MatsK, user31481, per1234, gre_gor Jan 30 '18 at 16:02

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • I really don't know what you want to ask... what means +1 all the time? Should it count the number of transitions (of one or both leds?), maybe you have short values which are incorrect (cheap LDRs) and you have to remove short 'spikes' / false postives. – Michel Keijzers Jan 30 '18 at 13:18
  • @MichelKeijzers When I put my hand on LDR sensor it must turn light on and tell in serial monitor 1 time turned on and then if i take my hand off led is turned off. and if I repeat it count to 2 and again then 3... – Joe David Jan 30 '18 at 13:39
  • Did you check what values you get when you put your hand over it? And analyzed them? Print out every number (can be many) and check if your program behaves the way you expect when using the values you received. You might have to filter some, or use an average or other way to remove some values that you didn't expect (but the sensor still sends). – Michel Keijzers Jan 30 '18 at 13:58
  • @MichelKeijzers Sensor printing "First LDR is Dark" and looping it. How I can make it no loop and counts +1 ? – Joe David Jan 30 '18 at 14:01
  • That means that if (ldrStatus1 <= ledTest) is always true ... check the value to see if it is what you expect. – Michel Keijzers Jan 30 '18 at 14:15
1

Use state change detection and hysteresis for counting pulses in an analog signal.

const int lowerThreshold = 150;
const int upperThreshold = 200;

const uint8_t analogPin = A0;
const uint8_t ledPin = LED_BUILTIN;

void setup() {
  Serial.begin(115200);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  static bool state = LOW;
  static unsigned int counter = 0;

  int analogValue = analogRead(analogPin);
  if (state == HIGH) {
    if (analogValue < lowerThreshold) {
      state = LOW;
      counter++;
      Serial.println(counter);
      digitalWrite(ledPin, LOW);
    }
  } else { // state == LOW
    if (analogValue > upperThreshold) {
      state = HIGH;
      digitalWrite(ledPin, HIGH);
    }
  }
}

This sketch basically applies a software Schmitt trigger to the analog input signal, and increments the counter on each falling edge. (Rising edge of light intensity, because your LDR configuration inverts the signal.) Schmitt Trigger Waveforms

The red waveform is the analog input from the LDR. The gray rectangular wave is the state (and the state of the LED). The counter is incremented at each black, vertical dashed line.

  • LED is always ON.. – Joe David Jan 30 '18 at 13:38
  • Then change the thresholds. Do you understand how it works? That's the first step. – tttapa Jan 30 '18 at 14:03
  • I changed low treshold to 0 and led still turned ON. Changed ledPin to 7 where is my led and still led turned on. – Joe David Jan 30 '18 at 14:14
  • That's to be expected, if you set the low threshold to 0, it is impossible to turn off the LED. Go through the sketch, try to understand the logic. Try it with a potentiometer first (left terminal to ground, center terminal to A0, right terminal to 5V) and set the low threshold to 256, and the high threshold to 767. Turn the potentiometer and see what happens to the LED. Then use the analogReadSerial example to see what values you get from the LDR. Adjust the thresholds accordingly. – tttapa Jan 30 '18 at 14:20
  • Edited my question. – Joe David Jan 30 '18 at 14:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.