RPM code I've been using, which is great for reading RPM. Every time I attempt to switch an output based on the variable 'rpm' I get no activity on 'motorPin'. But if I manually apply voltage to the motor I get a perfect rpm reading. If I digitalWrite motorPin HIGH in loop it will turn on the pin and still give correct rpm.
- Code: Select all
int rpmcount = 0;
int rpm = 0;
int timeold = 0;
int motorPin = 8;
int ledPin = 13;
void setup()
{
pinMode(motorPin, OUTPUT);
pinMode(ledPin, OUTPUT);
// digitalWrite(motorPin, HIGH);
digitalWrite(ledPin, HIGH);
Serial.begin(57600);
attachInterrupt(0, rpm_fun, RISING);
}
void loop()
{
if (rpmcount >= 20) {
//Update RPM every 20 counts, increase this for better RPM resolution,
//decrease for faster update
rpm = 30*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
Serial.println(rpm,DEC);
if (rpm < 3400){
digitalWrite(motorPin, HIGH);
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(motorPin, LOW);
digitalWrite(ledPin, LOW);
}
}
}
void rpm_fun()
{
rpmcount++;
//Each rotation, this interrupt function is run twice
}

