Ok. For my first Arduino project, I needed to create a tachometer/RPM counter. I used my google-fu and found several of the optical tachometer projects, and decided that would be the best solution.
So, I grabbed the Radio Shack IR LED Emitter and Detector pair.
Alas, that was probably not the best choice. I suggest that if anyone is interested in the same solution I used, look at the reviews for this product and compare to other IR emitters/detectors (sold as separate items) before making an informed decision.
But, I was bound to dance with the one I brung. So, I looked to other projects to test whether the emitter/detector pair even worked. In the course of doing that, it became a good exercise in learning about Arduino i/o and the programming generally.
So, in an effort to "pay it forward" for others getting into this stuff, and for feedback from people who know alot more than me, I now present my IR LED test sketch.
What this does it puts the emitter and detector on different pins, and measures the analog readings of the detector. It then keeps track of a minimum and maximum value. Using these, I could set a threshold in my tach sketch - above a certain value meant the "beam" had been "tripped" and to count that towards the RPM. Also, note that for my purposes, the emitter and detector are mounted side-by-side, so the LACK of IR means that it is between rotations. When the IR is reflected back to the detector, it means that the arm traversed across the beam. (Oh, I also put an LED on pin 13 because I'd just finished the blink tutorials.)
Diagrams:
Sketch:
/*---------------------------
IR LED and phototransistor beam test and calibration
debug stuff:
min/max val = whatever the value is when it changes
LED is always on
----------------------------*/
// defines for pins
#define IRoutputPin 2 // digital output pin
#define IRinputAnalogPin 0 // analog input pin
#define LEDsignalPin 13 // LED signal pin
// int and long values
int val = 0; // variable to store the value read from input pin
int minVal = 1000;
int maxVal = 0;
int oldVal= 0;
// strings
String outputString;
String intro = "JTD IR test and calibration init";
boolean needIntro = true;
String signalBase = "Signal Strength: ";
String minBase = "Min: ";
String maxBase = "Max: ";
void setup()
{
pinMode(IRoutputPin,OUTPUT);
digitalWrite(IRoutputPin,HIGH);
pinMode(LEDsignalPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
// print header
if (needIntro == true)
{Serial.println(intro);
needIntro = false;}
digitalWrite(LEDsignalPin, HIGH); // turn signal LED on
// read value of analog input
val = analogRead(IRinputAnalogPin); // read the input pin
//Serial.println(val); // debug value
if (val != oldVal) // if the new value is different than the old value, then do stuff
{
String outputString = signalBase + val; // concatenates output for signal strength
Serial.println(outputString);
outputString = minBase + minVal; // concatenates output for min
Serial.println(outputString);
outputString = maxBase + maxVal; // concatenates output for max
Serial.println(outputString);
oldVal = val;
delay(500); // wait half a second
}
if (val < minVal) // conditional if val is less than prior minVal
{ //String outputString = minBase + val; // concatenates output
Serial.println(outputString);
minVal = val;}
if (val > maxVal) // conditional if val is greater than prior maxVal
{ String outputString = maxBase + val; // concatenates output
//Serial.println(outputString);
maxVal = max(maxVal, val);}
}
Again - this is by no means authoritative. All feedback is welcome. Also, I plan on trying to incorporate this into a self-calibration routine in the actual tachometer. Someone can spin whatever it is they want to get the RPM of, the routine will map the min and max to a value range, and anything above the middle is a "count." Basically, it would self-determine what the ambient is, and what the reflected value is.
And thanks to all those who share their knowledge in forums like this. It makes it much easier for n00bs like me.