58 lines
1.1 KiB
Arduino
58 lines
1.1 KiB
Arduino
#define PIN_RPM 9
|
|
#define PIN_PWM 10
|
|
|
|
volatile unsigned int pulseCount = 0;
|
|
const unsigned int pulsesPerRevolution = 2;
|
|
|
|
unsigned long previousMillis = 0;
|
|
unsigned int rpm = 0;
|
|
|
|
void rpmISR() {
|
|
pulseCount++;
|
|
}
|
|
|
|
int getSpeedForTemperature(){
|
|
static int i = 0;
|
|
switch(i++ % 5){
|
|
case 0: return 100;
|
|
case 1: return 140;
|
|
case 2: return 180;
|
|
case 3: return 220;
|
|
case 4: return 255;
|
|
}
|
|
return 255;
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
|
|
pinMode(PIN_RPM, INPUT_PULLUP);
|
|
pinMode(PIN_PWM, OUTPUT);
|
|
|
|
attachInterrupt(digitalPinToInterrupt(PIN_RPM), rpmISR, FALLING);
|
|
digitalWrite(PIN_PWM, HIGH);
|
|
}
|
|
|
|
void loop() {
|
|
uint8_t speed = getSpeedForTemperature();
|
|
unsigned long currentMillis = millis();
|
|
if(currentMillis - previousMillis >= 1000){
|
|
noInterrupts();
|
|
unsigned int count = pulseCount;
|
|
pulseCount = 0;
|
|
interrupts();
|
|
|
|
rpm = (count * 60) / pulsesPerRevolution;
|
|
|
|
Serial.println("Current Voltage: " + String(speed));
|
|
Serial.println("Current RPM: " + String(rpm));
|
|
Serial.println();
|
|
|
|
previousMillis = currentMillis;
|
|
}
|
|
|
|
analogWrite(PIN_PWM, speed);
|
|
|
|
delay(1000);
|
|
}
|