Beginner Sensor help needed

So the sensor is plugged to pin# 2.

We are trying to make the sensor detect an “eye-mark”, and trigger the first time it sees one of the eye marks.

What is happening now is that every time the sensor hits one of the eye marks, the outputs from the arduino is as if we hit multiple ones.

Here is the code:

void loop() {

// read the sensor input pin:

buttonState = digitalRead(buttonPin);

if (buttonState == LOW)

{

sensor=0;

}

if (buttonState == HIGH)

{

sensor++;

}

if (sensor == 1)

{

Serial.println ("Eye mark seen");

}

The code above, if it was working properly, should show this:

Eye mark seen (every time the sensor sees an eye mark)

This is what we get now:

Eye mark seen
Eye mark seen
Eye mark seen
Eye mark seen
Eye mark seen

Thanks for the help.

You realize loop() runs upto a million times a second or so?

Sorry Im a neebie to this. I just need to fix my code so that when the sensor sees an "eye-mark" it triggers it once only. Even if it is continously on that "eye-mark". But if it moves away from it you get no display or atleast a display that states only once that "no-eye-mark-is-seen". Its for a sensor that will detect when something moves infront of it or stays in front of it. Thanks.

void loop()
{
  byte buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH)
  {
    Serial.println ("Eye mark seen");                           //print the text once
    while(digitalRead(buttonPin) == buttonState ){}    //do nothing untill the state turn low again, then come back to the loop
  }
}

Your original code assumed that if you added 1 repeatedly to "sensor" it would never come back to "1" again - in fact it was probably coming back to "1" many times a second, since the "int" type is 16 bit on the Arduino and cycles every 65,536 increments - machine instructions on the Arduino are executing at 20,000,000 per second so a lot can happen in a small space of time.

The suggestion above is much more sensible since it waits for the triggering condition to go away before proceeding round the loop.

I added a counter to keep track of how many eye marks the sensor sees and this is what I get after going over one eye mark.

that's strange, i use the same exact code.. however, is it possible that the state could suddenly change? what kind of sensor do you use?

I am using a BALLUFF contrast sensor; p/n: BKT 67M-001-U-S92

Do you think it is a hardware issue?

If so, what kind of sensor (preferable contrast) would you use for this type of eye mark detection application?

after a brief view of the datasheet i realized it is an analog-output device, isn't it? then you should check something
firstly, connect it to one of those analog pins, then write a simple sketch that takes the value from the pin and sends it to the serial port so that you can see how the values oscillate and change depending of a given stimulus. then post here your results :wink:

I am using the PNP/NPN output of the sensor; I connected it to the analog input and all I get is 14, does not matter if the sensor is on the eye mark or not!
Thanks a lot for your help, I am really lost!!

Firstly I would check the analog output is working (but use a 10k series resistor, it can go above 5V).

Secondly the switched output is poorly explained in the datasheet I found - the unit has a push button to set the operating mode for the switched output I believe.

The switched output is labelled "NPN/PNP" which I don't understand, it seems to be switchable between either output circuit (which means it might output the full supply voltage, which will fry the Arduino - so for now use a 22k or so resistor in series with the output - it might be open-collector in either case so try a 10k resistor from the output directly to the 5V rail.

You realize that device needs a minimum of 10V supply of course...

Anyone with a grasp of technical German will be able to check the manual pdf for more insight.

you ought to use the analog output, indeed.

Do you guys know of a cheap contrast sensor that I can use? (maybe something you have used in an application)

I have added a delay of 250 ms and still using the digital output of the sensor and it seems to work ok...
only problem is when 2 consecutive eye marks are within less than of 250 ms of each other.

I will try the analog output and let you guys know what I get.