I'm currently trying to set up an Arduino Uno and a Hall Effect Sensor (A3144) to measure the RPM of a motor. I have tested the sensor, and it reads "0" when sensing the magnet, and "1" when the magnet is removed. I have also written code that I thought would work, but when I test my program, nothing appears on the Serial Monitor. If anyone has any ideas of how I can change my coding to get it to work, I would greatly appreciate some advice!
Here is my code:
int sensorPin = 2; //Hall Effect Sensor at Digital Pin 2
float hall_thresh = 100.0; //Set number of hall trips for RPM reading
void setup() {
Serial.begin (115200); // setup serial - diagnostics - port
pinMode (sensorPin, INPUT); // setup pins
}
void loop() {
// preallocate values for tachometer
float hall_count = 1.0;
float start = micros();
bool on_state = false;
// counting the number of times the hall sensor is tripped
while (true) {
if (digitalRead(sensorPin) == 0) {
if (on_state == false) {
on_state = true;
hall_count += 1.0;
}
} else {
on_state = false;
}
if (hall_count >= hall_thresh) {
break;
}
}
// print information about Time and RPM
float end_time = micros();
float time_passed = ((end_time-start)/1000000.0);
Serial.print("Time Passed: ");
Serial.print(time_passed);
Serial.println("s");
float rpm_val = (hall_count / time_passed) * 60.0;
Serial.print(rpm_val);
Serial.println(" RPM");
delay(1); // set delay in between reads for stability
}
float
s for counters? Terrible idea. – TomServoSerial.print()
call inside thewhile
loop to see what it's doing? – Pranav Hosangadi