Hi All
I am having a head scratching moment here with some code. I have an analog hall sensor with Arduino UNO board that is sensing a magnet on a shaft that is rotating at 4-10 rpm. What I am trying to accomplish is slowing the refresh rate down enough to get a good signal and only printing the result every 60 seconds. I know this code has some issues and its not super clean, this is my first full blown attempt at writing code in 20 years.
My issue is when I use 60 sec for the interval, the Serial.print(count) does not show up, if i drop it to 31 sec, I get the RPM = and the correct RPM. What I am concerned about is that if it is 3 or so RPM, that is only 1 - 2 readings to determine the result and it will be a bit wonky. Not a huge issue for this project but I would like to know why this is happening with the interval above 31 sec. Any help is very much appreciated.
/*
ReadAnalogVoltage
Reads an analog input on pin 0, counts changes in A0 pin and counts for 60 seconds to convert into RPM for low speed shafts (1-59)
this will be used for 12-20 hrs at a time in between resets
*/
int threshold;
int thresholdread;
int sensorValueread;
int sensorValue;
int count = 0;
int voltage;
unsigned long previousmillis = 0;
const int interval = 60000;
// the setup routine runs once when you press reset:
void setup() {
int thresholdread = 0;
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop()
{
unsigned long currentMillis = millis();
int thresholdread = analogRead(A0); // read sensor for baseline comparison
int threshold = (((thresholdread+49)/50)*50); //round sensor reading to nearest 50 to smooth out readings
delay(1000); // read sensor every 1 second
int sensorValueread = analogRead(A0); //read sensor
int sensorValue = (((sensorValueread+49)/50)*50); //round sensor reading to nearest 50 to smooth out readings
int voltage = sensorValue * (5.0 / 1023.0); //convert sensor reading back into voltage
if (sensorValue > threshold) //test to see if sensor is higher than threshold
{
count++;} //counts if true
if (sensorValue < threshold) //test to see if sensor is less than threshold
{ count++; //counts if true
}
Serial.println(voltage); //just for testing purposes
Serial.println(sensorValue); //just for testing purposes
Serial.println(threshold); //just for testing purposes
Serial.println(currentMillis); //just for testing purposes
if ((unsigned long)(currentMillis - previousmillis) >= interval){ //test to see if 30 sec has passed
Serial.print("RPM = ");
Serial.println(count*2,DEC); //counts per minute is equal to RPM
previousmillis = currentMillis; //starts a new 30 sec countdown
count = 0; //resets count to 0 for new countdown
}
}