Hello all,
I'm having difficulty comparing voltage readings over time using an Arduino UNO Rev 3.
Using analogRead on a analog pin to measure RPM with a rotating disc and optical sensor. When a solid part of the disc passes through the optical sensor, the voltage from the sensor should read 0. Otherwise, it should read a positive value based upon a voltage draw (I got 7mV before my computer said it was drawing too much current). Whenever the voltage rises, "counts" should increment by 1. Unfortunately, the counts vary wildly with time (sometimes it was in the hundreds), so I decided to try to perform counts in intervals, but I was getting random counts when nothing was in between the emitter and sensor on the optical sensor.
I suspected that the voltage readings were very sensitive to small voltage changes, thus, it would fluctuate crazily and give me "counts" as every rising value. Using Serial.print(analogRead(analogPin)) returns a integer value (based upon 5V/1023 increments as I understand it). But when I store the analogRead(analogPin) as a variable, it gives me all the crazy counts. I'm suspecting that Serial.print(analogRead(analogPin)) does not give me the real variable value that's stored... am I correct? And if so, what is the best method of getting an RPM reading.
In addition, I tried using attachInterrupt with the "RISING" parameter, but it wasn't sensitive enough since the voltage changed by a small number of milli-volts.
I attached a basic circuit diagram and below is my code (I'm sorry if it's really amateurish; I just started playing with Arduinos and barely know the syntax):
//This program uses a optical sensor and rotating disc with
//gear shapes cut out to measure RPM as the disc rotates.
//Variable to measure time
unsigned long time1 = 0;
unsigned long time2 = 0;
//Variable to store RPM
unsigned long rpm = 0;
//Variable for number of rises in voltage for a given time
int counts = 0;
//Set variable for pin for voltage reading
int analogPin = 0;
//Initialize variable to store value of voltage
unsigned long v = 0;
unsigned long voltage = 0;
unsigned long voltage1 = 0;
unsigned long voltage2 = 0;
//Set variable for time between RPM calculations
unsigned long RPMperiod = 500000;
unsigned long samplePeriod = 100000;
//Number of slots cut into the disc
int slots = 8;
void setup() {
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
time1 = micros();
voltage1 = analogRead(analogPin);
if ((voltage1 - voltage2) > 4.5 && (voltage1 >= 0) && (voltage2 >= 0)){
//Serial.println(voltage1);
//Serial.println(voltage2);
counts++;
voltage2 = voltage1;
}
else if (voltage1 < voltage2){
voltage2 = voltage1;
}
if ((time1-time2) > RPMperiod){
time2 = time1;
Serial.print("Voltage:");
Serial.print("\t");
voltage = analogRead(analogPin);
Serial.print(voltage);
Serial.print("\t");
Serial.print("Count:");
Serial.print("\t");
Serial.print(counts);
Serial.print("\t");
Serial.print("RPM:");
Serial.print("\t");
rpm = counts*60000000/slots/RPMperiod;
Serial.print(rpm);
Serial.print("\n");
counts = 0;
}
}