I'm working on a counter with an ultrasonic sensor, SRF05... the concept is the same as button counter, everytime an object is detected in range it will add +1 to the counter... I've made the code but it doesn't work... here's my code:
int maximumRange = 500;
int minimumRange = 50;
int currentState = 0;
int previousState = 0;
int counter = 0;
const int TrigPin = 12;
const int EchoPin = 11;
void setup() {
Serial.begin(9600);
}void loop() {
long duration, distance;
pinMode(TrigPin, OUTPUT);
digitalWrite(TrigPin, LOW);
delayMicroseconds(2);
digitalWrite(TrigPin, HIGH);
delayMicroseconds(5);
digitalWrite(TrigPin, LOW);
pinMode(EchoPin, INPUT);
duration = pulseIn(EchoPin, HIGH);
distance = duration/58.2;
if (distance <= minimumRange){
currentState = 1;
}
else {
currentState = 0;
}
if(currentState != previousState){
if(currentState == 1){
counter++;
previousState = currentState;
Serial.println(counter);
}
}
}
i'm not sure where i did wrong, if anyone could help, please help..