Hello, I'm new to this forum, and actually just initiated with ARDUINO, although not new to programming. I have been teaching some people basic stuff for ARDUINO programming, aiming to do some art robots and, further on, developing animatronics. During the last (and first) workshop, not familiarized with this H-S/W students were trying to develop a proximity sensor using the cheapest sensor they could find, yep, that is the CNY70. Although it presented several problems with environmental light and noise, they were eager to use it, so, I decided to develop a lib where they could easily use it, and it turned out in this library I am posting today.
What it does is taking an environment sample (that means, it turns off the IR led and measures), and then, it is compared with the current measure. Up to there it is rather simple, what this library offers is the possibility to also filter such signal, you can choose if just sampling it as it is, or taking an average (from as many samples as you wish), or running a simple RC filter (predictive type),
So, it is an (I hope) easy to use library, which I called SensorOp. I assume some of the ppl reading this post is already familiarized with the use of libraries, but anyway I will give you some hints on its use. First you will have to un-ZIP the attached file into your ARDUINO - library folder.
you can get started with the included example,which is located on the Sensor folder.
For this example you will need to connect the sensor as follows (Mainly, the led control pin to the led's anode +, the cathode to a 220ohm resistor, the phototransistor collector to Vcc, the emitter to the analog input and to a 100K resistor, which goes to ground) :
Then, upload the code to your ARDUINO board (tested on ARDUINO UNO R3) and, open the Serial Monitor, and you will be able to see the different types of measuring methodes the library is able to perform (To do a better check just copy the data yielded at the sensor monitor and paste it into a spreadsheet (Like excel) program (or whichever you prefer), and graph it.
Now, for using it, you must include the library into your own code (preferably before all your code starts):
and then declare the object:
SensorOp MySensorOp(int ledControl, int sensorPin, int samples)
ledControl is the pin where your IR led will be connected (in the attached example is 2 (digital I/O 2)),
sensorPin, the pin where your sensor is (in example A0)
samples (the number of samples you wish to take, if any, anyway, always has to be declared)
Now, you will be able to use the same function for three different methods, all of them will result in an integer reading between 0 and 1023; deppending on your inpu,t the method will be selected, lets start with the easy one:
In this one, all the process is to take the measure from the environment an then the sensor reading, this method is really fast, but not very accurate, although the light induced noise/interference is reduced, the measure will still noisy.
this input for the method will read [sample] times and will average them, it is necessary that the input value [value] is the same output. for making it clear, if I want my result to be stored in a variable called Read, then the function input must be Read>
Read = MySensorOp.readSensor(Read);
MySensorOp.readSensor(int value,int filter)
Well this is the function I like the most, it has a simple predictive algorithm (an RC digital filter), the input is the previous reading (the same as explained in the one before), and a number which indicates the RC filter level (making it simple, the bigger the number, the less the noise, but the change on the reading will be slower).
I hope you enjoy it, any comment or suggestion just post, hope it is useful, your comments would encourage me to do more libraries like this one. Remember lot of samples is lot of time...