My error correction doesn't seem to work?

I'm building a counter that uses a electric eye. Basically I tell the program to look for a change then increment my counters, simple enough. If I have the system running every so often I get a false positive, so I tried to write some error correction code ( my duel "if" statements). But it doesn't seem to work? Can you take a peak at my code and help my smooth it out
Thanks!

int sensorpin = 0;                 // analog pin used to connect the sharp sensor
int val = 0;                 // variable to store the values from sensor(initially zero)
int count = 0;          //variable to store count
int val2 =0;            //variable to store current sensor reading
int countout1 = 7;      // dispaly #1 output
int countout2 = 8;      // display #2 output

void setup()
{
 Serial.begin(9600);       // starts the serial monitor
 val = analogRead(sensorpin);    // set a base sensorr reading
 pinMode(countout1, OUTPUT);    // set pin 7 as output
 pinMode(countout2, OUTPUT);    // set pin 6 as output
 digitalWrite(countout1, LOW);   // turn on pull down resistor
 digitalWrite(countout2, LOW);  // turn on pull down resistor
}

void loop()
{ 
  Serial.print("val = ");     //print initial sensor value for debugging
  Serial.println (val);       //debugging
 int val3 = val + 100;  
 Serial.print("val2 = ");     //debugging
 Serial.println(val2);     //print current sensor reading for debugging  
 val2 = analogRead(sensorpin);       // reads the value of the sharp sensor
 if (val2 > val3 )
 {
  delay(200);                     //wait icase of false positive reading
  if(val2 > val3)                //check sensor again
  {
   count = (count++);
   Serial.println(count);
   digitalWrite (countout1, HIGH);
   digitalWrite(countout2, HIGH);
   delay(500);
 }
 }
 else
 {

   delay(100);                      //give the displays time to catch up
   digitalWrite (countout1, LOW);
   digitalWrite (countout2, LOW);
 }                      


}
if (val2 > val3 )
 {
  delay(200);                     //wait icase of false positive reading
  if(val2 > val3)                //check sensor again
  {

That does nothing because you never read the values again so what was true 200 mS ago is true now, it can not fail.

Hi Bradshaw106

 if (val2 > val3 )
 {
  delay(200);                     //wait icase of false positive reading
  if(val2 > val3)                //check sensor again

While the delay() is executing, nothing else can happen in the program. So your comment on the last line is incorrect - the sensor has not been checked again, and the values of val2 and val3 will be the same as they were before.

You need to repeat this statement before the second if statement:

val2 = analogRead(sensorpin);

However, I think you may then hit a new problem. The next time round the loop (after the 500ms + 200ms delay) the counter will increment again if val2 is still greater than val3. Maybe with your hardware this situation can't happen.

Regards

Ray

First, it would make your code more readable if you used Ctrl-T before you posted it.

Second, you set val in setup(), but never read or update it again. val2 is updated in loop() and compared to val3, but val3 is always based upon the value read in setup(). It would seem to me that if you are counting something, what that count is when the program starts doesn't matter much later on.

Third, your Serial.print() calls are placed in such a way they don't seem to do much good. Since val never changes in loop(), why print it out? Also, since val3 is just val + 100, it never varies either, which means val3 is a constant. If so, why not just set it as a const and be done with it?

Fourth, this code fragment:

  if (val2 > val3 )
  {
    delay(200);                     //wait icase of false positive reading
    if (val2 > val3)               //check sensor again
    {

is never going to change state. The delay() call has no way to change either val2 (since analogRead() isn't called but is the only thing that can change val2) or val3 (which is a constant).

Am I missing something?

Thanks! For the fast responses. You have giving me a direction to go in, I will experiment. As far as seeing "weird" things in my code, well that's because I'm very new at this. I will try to clean it up a bit