Hello. I'm totally new to arduino and now I have this problem.
I have an arduino uno, with motor shield.
I have a pushbutton, to start a loop for to motors. And works just fine. Now I want to change the time the motors run with a button instead and this button need to change the direction to.
Please see the code below where I have marked the problem with red!
const int buttonPin = 2; // the number of the pushbutton pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// put your setup code here, to run once:
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel B pin
pinMode(8, OUTPUT); //Initiates Brake Channel B pin
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(7, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(7, HIGH);
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
//Motor A forward @ full speed
digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at full speed
delay(1000);
digitalWrite(9, HIGH); //Engage the Brake for Channel A
delay(0001);
//Motor B forward @ full speed
digitalWrite(13, HIGH); //Establishes forward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed
delay(0735);
delay(0425); Those delay times need to by change with a button, so its the button thats stops the motor and not the time (and its the motor thats runs something that sould hit the button, and then change direction to run the other way.
digitalWrite(8, HIGH); //Engage the Brake for Channel B
delay(0500);
//Motor B backward @ full speed
digitalWrite(13, LOW); //Establishes backward direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, 255); //Spins the motor on Channel B at full speed
delay(0700);
delay(0410); Those delay times need to by change with a button, so its the button thats stops the motor and not the time. The motor should stop and stay there on till the pushbutton are pressed again.
digitalWrite(8, HIGH); //Engage the Brake for Channel B
delay(0001);
//Motor A backward @ full speed
digitalWrite(12, LOW); //Establishes backward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 255); //Spins the motor on Channel A at half speed
delay(1000);
digitalWrite(9, HIGH); //Engage the Brake for Channel A
delay(0001);
}
else {digitalWrite(7, HIGH); }
}
Looking forward to some help for this matter.
Best regards, a beginner to Arduino ![]()