Pages: [1]   Go Down
Author Topic: Counting with photoresistor (searched)  (Read 305 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,
Making a very simple circuit to count when objects move over it. The problem I'm having is the incrementing variable is counting too many times per object.

I want to ++ everytime there is 1 object moving over the sensor but it seems that the code is ++ the all the while the sensor is blocked.

The threshold for the light value is 15, so anything less than that would be considered an object.

// car counting program
int highPin = 52;
int lightVal;
int sNBT = 15;
long NBT = 0;
long previousMillis = 0;
long interval = 3000;
long bin = 1;
int x = 1;

void setup(){
  pinMode(highPin, OUTPUT);
  pinMode(sNBT, INPUT);

void loop(){
  digitalWrite(highPin, HIGH);
  //read the sensors light value
  lightVal = analogRead(sNBT);
  //sense if car is on sensor

if(lightVal <15){
  lightVal = analogRead(sNBT);
  while(lightVal < 15){
    lightVal = analogRead(sNBT);
  unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval){
    Serial.print("NBT Cars :");
    previousMillis = currentMillis;
    NBT = 0;
    Serial.print("Current Light Level:");
    Serial.println (lightVal);

Thanks for any advice!
« Last Edit: February 15, 2013, 08:18:34 pm by Therefor3 » Logged

Offline Offline
Full Member
Karma: 5
Posts: 156
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It looks like you're reading light levels all of the time and whenever it is lower than 15 you increase the NBT variable. Try moving the "if(lightval < 15)" block of code inside the "if(currentMillis - previousMillis > interval)" block.

Offline Offline
Shannon Member
Karma: 223
Posts: 12630
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I see that it's supposed to stay in that while loop until the level rises above 15, and presumably that's expected to happen when the object has cleared the sensor. Have you tried logging the values you actually get in practice? It may be that the readings don't consistently stay below 15 while the object is there.

Is there any other filtering you could apply to prevent the multiple readings? For example, is there a minimum delay between genuine triggers of the sensor? If so you could use that to 'debounce' the sensor readings.

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: