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.
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
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.
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.