70 lines
1.9 KiB
Arduino
70 lines
1.9 KiB
Arduino
// Board: Adafruit Feather M0
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <Adafruit_GFX.h>
|
|
#include "Adafruit_LEDBackpack.h" // "Adafruit LED Backpack Library" v1.5.0
|
|
|
|
#define MEASUREMENT_PERIOD_MS 100
|
|
#define PULSES_PER_REVOLUTION 8
|
|
|
|
Adafruit_7segment matrix = Adafruit_7segment();
|
|
|
|
// --- Tachometer variables ---
|
|
const int TACH_INTERRUPT_PIN = A0; // Or any other digital pin capable of interrupts
|
|
volatile unsigned long pulse_count = 0;
|
|
unsigned long last_measurement_time = 0;
|
|
|
|
// --- Interrupt Service Routine (ISR) ---
|
|
void count_pulse() {
|
|
pulse_count++;
|
|
}
|
|
|
|
void show(int num) {
|
|
matrix.print(num, DEC);
|
|
matrix.writeDisplay();
|
|
}
|
|
|
|
void setup() {
|
|
matrix.begin(0x70);
|
|
|
|
Serial.begin(115200);
|
|
// Serial.setDebugOutput(true);
|
|
|
|
// --- Setup Tachometer Interrupt ---
|
|
pinMode(TACH_INTERRUPT_PIN, INPUT_PULLUP); // Set pin as input with internal pull-up resistor
|
|
attachInterrupt(digitalPinToInterrupt(TACH_INTERRUPT_PIN), count_pulse, RISING);
|
|
|
|
delay(1000);
|
|
|
|
Serial.println();
|
|
Serial.println();
|
|
Serial.println("================");
|
|
Serial.println("BOOT UP");
|
|
|
|
show(99999);
|
|
|
|
}
|
|
|
|
void loop() {
|
|
unsigned long current_time = millis();
|
|
|
|
if (current_time - last_measurement_time >= MEASUREMENT_PERIOD_MS) {
|
|
//detachInterrupt(digitalPinToInterrupt(TACH_INTERRUPT_PIN));
|
|
unsigned long collected_pulses = pulse_count;
|
|
pulse_count = 0;
|
|
//attachInterrupt(digitalPinToInterrupt(TACH_INTERRUPT_PIN), count_pulse, RISING);
|
|
|
|
// Calculate RPM
|
|
// RPM = (pulses / pulses_per_revolution) / (measurement_period_ms / 1000 / 60)
|
|
// RPM = (pulses / pulses_per_revolution) * (60000 / measurement_period_ms)
|
|
// Ensure floating point division for accuracy before converting to int for display
|
|
float rpm_float = ( (float)collected_pulses / PULSES_PER_REVOLUTION ) * (60000.0 / MEASUREMENT_PERIOD_MS);
|
|
int rpm = (int)rpm_float;
|
|
|
|
show(rpm);
|
|
|
|
last_measurement_time = current_time;
|
|
}
|
|
}
|