Motor direction by buttonswitch

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 :slight_smile:

The first thing that I would do is to get rid of all of the delay()s because they block the program from running freely which it needs to do in order to react to inputs in a timely manner. In any case why have you got two consecutive delay()s ?

delay(0700);
  delay(0410);

Removing the delay()s may seem like a big deal but it will pay dividends in the long run. The first thing to establish is why they are there. Are the delay()s of 1 millisecond really needed, for instance ?

In restructuring your code I would use a state machine structure. It sounds scary, but its not. Write down each state that the program can be in and assign a unique number to that state. Now in loop() use switch/case based on the current state number to execute the code for the current state. When the current state changes, perhaps due to user input or time passing, change the state number as appropriate and the code for the new state will run.

DO NOT use delay() for timing. Use millis() instead as in the BlinkWithoutDelay example. In the code for a state that is waiting for a period of time to pass check each time through loop() whether the period has elapsed and if so, change to the new state.

So, how do you use a pushbutton to change the timing or stop the motors immediately ? Well, because you are not blocking loop() from running at full speed you can read the button each time through loop() and act on it by changing a timing variable such as the period variable you are using to test whether the period has elapsed or change the state number to alter what the motors are doing.

The demo several things at a time is an extended example of the BWoD technique.

...R

Many thanks for the information. I have, like many others, not enough hours on a day, so I haven't got the time for my project in a few days.
Why I have two relays in a row, I can't recall. I thought it was because it only could go to 1000, but that's not correct.

I will go forward, and write the states down and see if it gets more clear how to make it work.

Again thanks!!