Go Down

Topic: Vehicle counter using IR sensor (Read 527 times) previous topic - next topic

Latik

Hi..
   I have trouble while counting obstacle in front of the IR sensor, I have used the state "HIGH" and "LOW" for the detection of the obstacle. But the program  gives two counting for the single obstacle. Here I m attaching my program detail.. plz guide me
Code: [Select]

#include <Wire.h>
#include <time.h>
#include <LiquidCrystal.h>
#define sensor x = A0
#define colx 8

int sensor;
LiquidCrystal lcd(0,1,8,9,10,11);

void setup()
{
  pinMode(sensor, INPUT);
  lcd.begin(16,2);
  lcd.setCursor(0,0);
  lcd.print("Count :");
}

int v;
int state;
int laststate = LOW;

void loop()
{
  int senread = digitalRead(sensor);
  char count[10];
  time_t endwait;
  time_t start = time(NULL);
  time_t seconds = 10;
 
  int state = senread;
 
  if(state != laststate)
  {
    v = v+1;
    delay(1000);
    lcd.setCursor(colx,0);
    sprintf(count,"%d", v);
    lcd.print(count);
  }

  delay(1);
  laststate = state;
  return(0);
}

TomGeorge

#1
Mar 14, 2018, 07:50 am Last Edit: Mar 14, 2018, 07:51 am by TomGeorge
Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html

You code is detecting a change in state, that is LOW to HIGH  and HIGH to LOW, which occurs as your object moves past the sensor.

You need to detect the LOW to HIGH only and ignore the HIGH to LOW.

You need to check if your new state is HIGH that the last state was LOW.

Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

GoForSmoke

A one full second delay because you detected a change? What will you miss, the next change? And will the current read match the last one and not report?

You should review what, when and how you expect to happen, model that out and make the code fit that. The delay does not fit.
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

6v6gt

Are you counting the transition HIGH to LOW and also LOW to HIGH ?

Code: [Select]

if(state != laststate)


Maybe

Code: [Select]

if(state == HIGH && laststate == LOW)

GoForSmoke

Look at where the delay(1000) is. Detect vehicles, catch a transition and wait 1 second.. will the following transition be missed? And if it is then how does that fit with further code execution?
1) http://gammon.com.au/blink  <-- tasking Arduino 1-2-3
2) http://gammon.com.au/serial <-- techniques howto
3) http://gammon.com.au/interrupts
Your sketch can sense ongoing process events in time.
Your sketch can make events to control it over time.

6v6gt

OK. I see my answer is essentially what @TomGeorge wrote. I missed the detail because it looked too much like his standard 'newbie' template. The delay in the if block is certainly unhealthy but may be a crude attempt to eliminate sensor "bounce" and is equivalent to a 4 meter vehicle passing at about 15km/h, but naturally, also dependent on the characteristics of the sensor.

TomGeorge

OK. I see my answer is essentially what @TomGeorge wrote. I missed the detail because it looked too much like his standard 'newbie' template. 
Thats the only people it needs to get the attention of.
I should have put the rest in its own post.... :o :o


Tom... :)
Everything runs on smoke, let the smoke out, it stops running....

lastchancename

@6v6gt, i'm glad you mentioned 'sensor bounce' because all inputs are susceptible to mis triggering - not just 'switches'.
I was going to raise it but saw your comment above.

In the case of optical inputs, it could be rough edges, reflections etc.
Cheers
Experienced responders have a nose for laziness, (they were beginners once)... expecting the poster to contribute to the learning experience.

Go Up