First Project. Could use some help.

I am new to Arduinos. been playing but nothing of my own.

here is the code.

int LDR = A0;
int LED = 9;

int count = 0;
int LDR_VALUE = 0;
int LDR_MIN = 400;

void setup() {
  // put your setup code here, to run once:
pinMode(LDR, INPUT);
pinMode(LED, OUTPUT);
}

void loop() {
  LDR_VALUE = analogRead(LDR);

if(LDR_VALUE >= LDR_MIN)
  {
    digitalWrite(LED,LOW);
  }

if(LDR_VALUE <= LDR_MIN);
  {
    digitalWrite(LED,HIGH);
    count++;
  }
}

What I want it to do is count the number of times the LDR drops below 400. The main idea is. in order for it to add another, it would have to rise above 400 and then back down again.

I am sure you can see the problem already. When the LDR drops below 400 it counts, and counts, and counts.... etc.

I am not sure how to overcome this. Any help will help. Again I am new to this the simplest answer is the best answer for me as I would like to learn more but it takes a few to stare at things and learns it.

Thank you
Mark

You need to look at the State Change example that comes with the Arduino IDE,

Your program needs to compare the latest reading from the LDR with the previous reading and only count when the latest reading is lower than the previous one.

...R

this is one way of using a flag to carry the state of the thing

if (LDR > 500) {
if ( statisFlag = 0 ) // should be == 0 because =0 changes to zero thanks AWOL
count++ ;
statisFlag = 1 ;
}
}

if (LDR < 450 ) { // deadband to create hysteresis
statisFlag = 0;
}

if ( statisFlag = 0 )Oops.

const byte LDRPin = A0;
const byte LEDPin = 9;

int Count = 0;
boolean LDR_Was_Active = false;
const int LDR_MIN = 400;

void setup()
{
  // put your setup code here, to run once:
  // pinMode(LDR, INPUT);  // NOT for analogRead();
  pinMode(LEDPin, OUTPUT);
}

void loop()
{
  boolean LDR_Active = analogRead(LDRPin) <= LDR_MIN;

  digitalWrite(LEDPin, LDR_Active);  // Set LED to match LDR state

  if (LDR_Active && !LDR_Was_Active)
  {
    // LDR is going from Inactive to Active
    Count++;
  }

  LDR_Was_Active = LDR_Active;
}