I am trying to have my code where when the pushbutton is pressed, it stays high until the button is releaed. When I push the button, it switches from high to low every time. (As seen in serial port). I don't know why it's doing this. I;m using a motor shield with this, I'm just using the serial port to help debug the problem.
Here is my code
int pinI1=8;//define I1 interface
int pinI2=11;//define I2 interface
int motor_on_pin=9;//enable motor A
int variable_speed_pin=A0;//analog input from potentiameter
int forwardButton_pin=4;
int reverseButton_pin=2;
float sensorValue=0;
float outputValue=0;
int forwardButtonstate=0;
int reverseButtonstate=0;
void setup()
{
Serial.begin(9600);
pinMode(pinI1,OUTPUT);
pinMode(pinI2,OUTPUT);
pinMode(motor_on_pin,OUTPUT);
pinMode(forwardButton_pin,INPUT_PULLUP);
pinMode(reverseButton_pin,INPUT_PULLUP);
}
void loop()
{
Serial.println("THERES MOTHER F'N SNAKES ON THIS MOTHER F'N PLANE");
forwardButtonstate=digitalRead(forwardButton_pin);
reverseButtonstate=digitalRead(reverseButton_pin);
while(forwardButtonstate==LOW && reverseButtonstate==HIGH)
{
analogWrite(motor_on_pin,potvalue());//input a simulation value to set the speed
digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
digitalWrite(pinI1,HIGH);
Serial.println("forward button is pushed");
break;
}
while(reverseButtonstate==LOW)
{
backward();
}
}
void forward()
{
analogWrite(motor_on_pin,potvalue());//input a simulation value to set the speed
digitalWrite(pinI2,LOW);//turn DC Motor A move anticlockwise
digitalWrite(pinI1,HIGH);
Serial.println("forward button is pushed");
}
void backward()//
{
analogWrite(motor_on_pin,potvalue());//input a simulation value to set the speed
digitalWrite(pinI2,HIGH);//turn DC Motor A move clockwise
digitalWrite(pinI1,LOW);
Serial.println("reverse button pushed");
}
void stop()//
{
digitalWrite(motor_on_pin,LOW);// Unenble the pin, to stop the motor. this should be done to avid damaging the motor.
delay(1000);
}
float potvalue()
{
sensorValue= analogRead(variable_speed_pin);
outputValue = sensorValue;
return outputValue;
}
For example the output will say:
Theres snakes on the plan
button is pushed
theres snakes on the plane
button is pushed
theres snakes on the plane
button is pushed
....etc