I have an anemometer which uses a reed switch. Depending on the RPM of the anemometer a servo motor should move at a certain speed and amount to correspond with the wind turning the anemometer (the servo motor moves a swinging sign depending on the windspeed outside).
I started the code with an RPM calculation. In the loop there are "If" statements; I have set them to turn an LED high as well as running the servo sequence depending on the RPM. The LED's are working with the corresponding RPM but I cannot get the servo to move.
I am not sure but I assume the interrupt is stopping the servo loop from running?
I have tried "detachInterrupt(0);" but I could not get this to work, I added it after each "If" statement followed by an "attachInterrupt(0, rpm_fun, RISING);" you can see it in the code.
A precise RPM is not essential but a general wind speed of High, Medium or Low is needed.
I am not sure if I have overcomplicated this and any ideas are welcome.
volatile byte rpmcount;
unsigned int rpm;
unsigned long timeold;
int ledPin1 = 13;
int ledPin2 = 12;
int ledPin3 = 11;
int ledPin4 = 10;
#include <Servo.h>
Servo myservo;
int delayTimeF = 17; //delay between servo movements to control speed
int delayTimeM = 25;
int delayTimeS = 30;
int delayTimeVS = 40;
void setup()
{
Serial.begin(9600);
attachInterrupt(0, rpm_fun, RISING);
rpmcount = 0;
rpm = 0;
timeold = 0;
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
digitalWrite(2, HIGH); //anemometer pin set high
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop()
{
if (rpmcount >= 5) {
//Update RPM every 5 counts,
rpm = 30*1000/(millis() - timeold)*rpmcount;
timeold = millis();
rpmcount = 0;
Serial.println(rpm,DEC);
//delay(1000); use as an alternative to rpmcount >5
}
}
void rpm_fun()
{
rpmcount++;
//Each rotation, this interrupt function is run twice
if((rpm >= 0) && (rpm < 79)){
//detachInterrupt(0);
digitalWrite( ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
//attachInterrupt(0, rpm_fun, RISING);
}
if((rpm >= 80) && (rpm < 150)){
//detachInterrupt(0);
digitalWrite( ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
myservo.write(100);
delay(delayTimeVS);
myservo.write(101); //full sequence removed for forum posting
delay(delayTimeVS);
myservo.write(100);
delay(delayTimeVS);
//attachInterrupt(0, rpm_fun, RISING);
}
if((rpm >= 151) && (rpm < 199)){
//detachInterrupt(0);
digitalWrite( ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, LOW);
myservo.write(100);
delay(delayTimeS);
myservo.write(101);
delay(delayTimeS);
myservo.write(102);
delay(delayTimeS); //full sequence removed for forum posting
myservo.write(101);
delay(delayTimeS);
myservo.write(100);
delay(delayTimeS);
//attachInterrupt(0, rpm_fun, RISING);
}
if((rpm >= 200) && (rpm < 399)){
//detachInterrupt(0);
digitalWrite( ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, LOW);
myservo.write(100);
delay(delayTimeM);
myservo.write(101); //full sequence removed for forum posting
delay(delayTimeM);
myservo.write(100);
delay(delayTimeM);
//attachInterrupt(0, rpm_fun, RISING);
}
if((rpm >= 400) && (rpm < 550)){
//detachInterrupt(0);
digitalWrite( ledPin1, LOW);
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
digitalWrite(ledPin4, HIGH);
myservo.write(100);
delay(delayTimeF);
myservo.write(101);
delay(delayTimeF);
myservo.write(102); //full sequence removed for forum posting
delay(delayTimeF);
myservo.write(101);
delay(delayTimeF);
myservo.write(100);
delay(delayTimeF);
//attachInterrupt(0, rpm_fun, RISING);
}
}