#include <Arduino.h>
// ============================================================================
// Hardware & Pin Configuration
// ============================================================================
constexpr uint8_t IR_SENSOR_PIN = 2; // D2 hardware interrupt pin
constexpr uint8_t PWM_OUTPUT_PIN = 3; // D3 hardware PWM output pin (Timer2)
constexpr uint8_t HOLD_BUTTON_PIN = 4; // D4 momentary push button pin
constexpr uint8_t PULSES_PER_REV = 1; // Change to blade count for multi-blade fans
// ============================================================================
// Timer1 & RPM Calculation Constants
// ============================================================================
constexpr unsigned long TIMER1_PRESCALER = 256UL;
constexpr unsigned long TICKS_PER_SECOND = F_CPU / TIMER1_PRESCALER;
constexpr float TICKS_PER_MINUTE = (float)(TICKS_PER_SECOND * 60UL);
// ============================================================================
// PWM Ramp Configuration Constants
// ============================================================================
constexpr int16_t PWM_MIN_DUTY = 0;
constexpr int16_t PWM_MAX_DUTY = 255;
constexpr int16_t PWM_STEP_SIZE = 5;
constexpr unsigned long PWM_STEP_INTERVAL_MS = 30UL;
// ============================================================================
// Serial Output & Timing Constants
// ============================================================================
constexpr unsigned long SERIAL_BAUD_RATE = 9600UL;
constexpr unsigned long SERIAL_PRINT_INTERVAL_MS = 200UL;
constexpr unsigned long DEBOUNCE_MS = 50UL;
constexpr unsigned long LOOP_DELAY_MS = 10UL;
const char* const HEADER_LIVE_TEXT = "TACK-ATTACK";
const char* const HEADER_HOLD_TEXT = "[HOLD]";
// ============================================================================
// Runtime State Variables
// ============================================================================
volatile unsigned long rpmTicks = 0;
volatile bool isTooSlow = true;
volatile bool hasNewPulse = false;
unsigned int currentRpm = 0;
bool isHeld = false;
bool buttonState = HIGH;
bool lastButtonReading = HIGH;
unsigned long lastDebounceTime = 0;
int16_t currentPwmDuty = PWM_MIN_DUTY;
int16_t pwmStepDirection = PWM_STEP_SIZE;
unsigned long lastPwmStepTime = 0;
unsigned long lastSerialTime = 0;
// ============================================================================
// Interrupt Service Routines (ISRs)
// ============================================================================
ISR(TIMER1_OVF_vect) {
isTooSlow = true;
}
void onIrPulse() {
rpmTicks = TCNT1;
TCNT1 = 0;
isTooSlow = false;
hasNewPulse = true;
}
// ============================================================================
// Helper Functions
// ============================================================================
void updatePwmRamp() {
unsigned long now = millis();
if ((now - lastPwmStepTime) >= PWM_STEP_INTERVAL_MS) {
lastPwmStepTime = now;
currentPwmDuty += pwmStepDirection;
if (currentPwmDuty >= PWM_MAX_DUTY) {
currentPwmDuty = PWM_MAX_DUTY;
pwmStepDirection = -PWM_STEP_SIZE;
} else if (currentPwmDuty <= PWM_MIN_DUTY) {
currentPwmDuty = PWM_MIN_DUTY;
pwmStepDirection = PWM_STEP_SIZE;
}
analogWrite(PWM_OUTPUT_PIN, (uint8_t)currentPwmDuty);
}
}
void printTelemetry(unsigned int rpmValue, bool heldState, int16_t pwmValue) {
Serial.print(heldState ? HEADER_HOLD_TEXT : HEADER_LIVE_TEXT);
Serial.print(" | RPM: ");
Serial.print(rpmValue);
Serial.print(" | PWM D3: ");
Serial.println(pwmValue);
}
// ============================================================================
// Setup & Main Loop
// ============================================================================
void setup() {
pinMode(IR_SENSOR_PIN, INPUT_PULLUP);
pinMode(HOLD_BUTTON_PIN, INPUT_PULLUP);
pinMode(PWM_OUTPUT_PIN, OUTPUT);
analogWrite(PWM_OUTPUT_PIN, (uint8_t)currentPwmDuty);
Serial.begin(SERIAL_BAUD_RATE);
printTelemetry(currentRpm, isHeld, currentPwmDuty);
// Configure Timer1 with 256 prescaler and overflow interrupt
noInterrupts();
TCCR1A = 0;
TCCR1B = 0;
TCNT1 = 0;
TCCR1B |= (1 << CS12);
TIMSK1 |= (1 << TOIE1);
interrupts();
attachInterrupt(digitalPinToInterrupt(IR_SENSOR_PIN), onIrPulse, FALLING);
}
void loop() {
// 1. Update non-blocking PWM ramp on Pin D3 (0 -> 255 -> 0)
updatePwmRamp();
// 2. Poll momentary HOLD button on Pin D4 with debounce
bool currentReading = digitalRead(HOLD_BUTTON_PIN);
if (currentReading != lastButtonReading) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > DEBOUNCE_MS) {
if (currentReading != buttonState) {
buttonState = currentReading;
if (buttonState == LOW) {
isHeld = !isHeld;
}
}
}
lastButtonReading = currentReading;
// 3. Calculate RPM when not in HOLD mode
if (!isHeld) {
if (isTooSlow) {
currentRpm = 0;
} else if (hasNewPulse) {
noInterrupts();
unsigned long measuredTicks = rpmTicks;
hasNewPulse = false;
interrupts();
if (measuredTicks > 0) {
float rpmFloat = TICKS_PER_MINUTE / (float)(measuredTicks * PULSES_PER_REV);
currentRpm = (unsigned int)rpmFloat;
}
}
}
// 4. Periodically print RPM, HOLD state, and PWM duty cycle to Serial Monitor
unsigned long now = millis();
if ((now - lastSerialTime) >= SERIAL_PRINT_INTERVAL_MS) {
lastSerialTime = now;
printTelemetry(currentRpm, isHeld, currentPwmDuty);
}
delay(LOOP_DELAY_MS);
}