/*
Arduino | hardware-help
Dc Motor Circuit Problems
Nick — 6/25 at 5:29 PM
A test of the RPM counter for speed feedback.
The Pulse Generator simulates the output of a Hall sensor.
*/
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int HALL_PIN = 2;
const unsigned long INTERVAL = 1000;
volatile unsigned int hallPulses = 0; // ISR variables need to be declared as volatile
unsigned long prevMillis = 0;
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, adjust if necessary
void pulseCounter() {
hallPulses++;
}
void showRPM() {
char buffer[16];
char fBuffer[8];
cli(); // disable interrupts while getting data
double displayRPM = (hallPulses * 60.0) / 1000.0;
hallPulses = 0; // reset count
sei(); // re-enable interrupts
dtostrf(displayRPM, 3, 2, fBuffer); // convert float to char array
snprintf(buffer, 16, "RPM: %s x1000", fBuffer); // format display string
Serial.println(buffer);
lcd.setCursor(0, 0);
lcd.print(buffer);
}
void setup() {
Serial.begin(115200);
lcd.init();
lcd.backlight();
pinMode(HALL_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(HALL_PIN), pulseCounter, FALLING);
// splash screen
lcd.setCursor(4, 0);
lcd.print("RPM Test");
delay(2000);
lcd.clear();
}
void loop() {
if (millis() - prevMillis >= INTERVAL) {
prevMillis = millis();
showRPM();
}
}