I am currently working on a project that has an automatic support wheel for bicycle.
The mechanism of this project is when the speed sensor is detecting the RPM of the rear bicycle wheel it will triggered the support wheel. The support wheel is oriented by the motor power. Basically, when the bicycle reaches a certain speed limit, it will activate the support wheel whether to move upwards or downwards. I have done some coding for this.
int sensor = 11;
unsigned long start_time = 0;
unsigned long end_time = 0;
int steps=0;
float steps_old=0;
float temp=0;
float rpm=0;
int motorPin = 9;
void setup()
{
{
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(motorPin, OUTPUT);
Serial.begin(9600);
pinMode(sensor,INPUT_PULLUP);
}
}
void loop()
{
start_time=millis();
end_time=start_time+1000;
while(millis()<end_time)
{
if(digitalRead(sensor))
{
steps=steps+1;
while(digitalRead(sensor));
}
}
temp=steps-steps_old;
steps_old=steps;
rpm=(temp/5)/60;
Serial.print("RPM = ");
Serial.println(rpm,DEC);
delay (1000);
//Restart the interrupt processing
if (rpm==0)
{
digitalWrite(8, HIGH);
digitalWrite(7,LOW);
analogWrite(motorPin, 255);
delay(1000);
}
if (rpm>0.02)
{
digitalWrite(8, HIGH);
digitalWrite(7,LOW);
analogWrite(motorPin, 255);
delay(1000);
digitalWrite(8, LOW);
digitalWrite(7,LOW);
analogWrite(motorPin, 255);
}
}
The problem with this code is that I do not know how to stop the motor when the rpm>0
until the sensor read rpm=0
. I want to make the motor rotate backwards for a few seconds and stop completely as long as the rpm is above zero (my coding will make the motor rotate forward when rpm>0
again and again, the same goes for when rpm=0
) and it will only rotate forward for few seconds when the rpm=0
and stop completely until the sensor read rpm>0
. The process continues looping.
Please help me, I'm new to Arduino and sorry for my English as I'm not really fluent in English.