Simultanious Motor Loops and Button (Motorshield)

Hey Forum - Im kinda stuck on my arduino project.
I am new to arduino btw.

So i have this problem with my arduino project.

I am running 3 DC motors of an arduino uno with and Arduino Motorshield ( with it´s own independent power supply)

The Motorshield leaves me with no digital pins but with 6 analog pins availiable.

I want to connect 6 momentary-buttons to the arduino - they have to be programmed into toggle buttons.

3 Buttons turn each individual motor on or off

The 3 other buttons activates a loop that varies the speed of the individual motors motorspeed in a specific patterns.

B1 = on/off M1

B2 = on/off M2

B3 = on/off M3

B4 = M1 loop

B5 = M2 loop

B6 = M3 loop

These loops should also be able to be turned off in the middle of the loops by clicking the buttons. (right now they can only be turned off in the end of the loop)

for example: (im using delay here which i know is not an option cuz of the code blocking)

motor1.setSpeed(255)

delay(200)

motor1.setSpeed(150)

delay(1200)

motor1.setSpeed(180)

delay(800)

motor1.setSpeed(120)

delay(800)

motor1.setSpeed(150)

delay(1200)

motor1.setSpeed(170)

delay(800)

motor1.setSpeed(130)

delay(800)

motor1.setSpeed(150)

delay(1200)

motor1.setSpeed(160)

delay(800)

motor1.setSpeed(140)

delay(800)

motor1.setSpeed(150)

delay(1200)

Codingwise i can´t get it to work beyond one button, how do i use millis to make make these loops work independently from each other - but the loops using "delays". What kind of direction and knowledge do i need to aquire to make this possible?

All best

Here´s what i got so far:


#include <AFMotor.h>
int Motor1State = 0;
int button1Pin = A0;
int button1New;
int button1Old = 1;
int Motor12State = 0;
int button2Pin = A1;
int button2New;
int button2Old = 1;
int dt=20;
AF_DCMotor motor1(1, MOTOR12_8KHZ);
AF_DCMotor motor2(2, MOTOR12_8KHZ);
void setup() {
  // put your setup code here, to run once:
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
button1New=digitalRead(button1Pin);
button2New=digitalRead(button2Pin);
while(button1Old==0 && button1New==1){
  if(Motor1State==0){
    motor1.setSpeed(255);
    motor1.run(FORWARD); 
    Motor1State=1;
  }
  else{
    motor1.run(RELEASE);
    Motor1State=0;
  }
  }
}
button1Old=button1New;
delay(dt);
// button no.2.
while(button2Old==0 && button2New==1){
  if(Motor12State==0){
    motor1.setSpeed(255);
    motor1.run(FORWARD);
    delay(200);
    motor1.setSpeed(150);
    delay(1200); 
    Motor12State=1;
  }
  else{
    motor1.run(RELEASE);
    Motor12State=0;
  }
  }
}
button2Old=button2New;
delay(dt);

}

Your code doesn't compile - too many closing braces in the loop function.

More seriously you have two potentially infinite while loops. This:

  while (button1Old == 0 && button1New == 1)

Is broken because if the condition is true, the loop will never end. This because you don't read the button in the loop.

1 Like

Hello
You may design a button handler. This buttonhandler takes care about debouncing and handles the subroutines to be called either when the button has been pressed or released.
Have a nice day and enjoy coding in C++.

1 Like

Hello @oranjekiz,

While loops and delay() pretty much guarantee that you cannot do anything simultaneously, or more correctly: concurrently, you need to write non-blocking code.

These tutorials will get you started:

Using millis for timing
Demonstration for several things at the same time
Finite state machine tutorial

1 Like

Hi PerryBebbington
Thank you so much! i´ll start immediately!