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