Hello,
I am coding a DC motor to spin at a certain speed for a certain time. I am having trouble getting it to run. I want to first press the speed option (button 10) and then have it wait for me to press the time options (button 12 and 13). Below is my code, according to my print statements the code is running the while loop continuously but not running the if statements when I press buttons 12 or 13. Thanks for the help.
int buttonPin8 = 8;
int buttonPin9 = 9;
int buttonPin10 = 10;
int buttonPin12 = 12;
int buttonPin13 = 13;
int motorPin = 3;
bool pressed = false;
int B10_var = 0;
void setup(){
pinMode(motorPin, OUTPUT);
pinMode(buttonPin10, INPUT_PULLUP);
pinMode(buttonPin12, INPUT_PULLUP);
pinMode(buttonPin13, INPUT_PULLUP);
Serial.begin(9600);
while (! Serial);
}
void loop(){
bool currentState10 = digitalRead(buttonPin10);
bool currentState12 = digitalRead(buttonPin12);
bool currentState13 = digitalRead(buttonPin13);
if (currentState10 == pressed){
B10_var = 1 ;
do{
Serial.println("step2") ;
if (currentState12 == pressed){
analogWrite(motorPin, 255);
delay(100);
analogWrite(motorPin, 25);
delay(2000);
analogWrite(motorPin, 0);
B10_var = 0 ;
Serial.println("step3") ;
}
else if (currentState13==pressed){
analogWrite(motorPin, 255);
delay(100);
analogWrite(motorPin, 25);
delay(2000);
analogWrite(motorPin, 0);
B10_var = 0;
Serial.println("step4") ;
}
}
while (B10_var == 1);
}