Arduino multiple if statements

Hi,

Thank you in advance for any feedback for my question.

I made a XY plotter machine that I want to control with a joystick. The joystick has 4 momentary pushbuttons. One for up, down, left, right.

I want the up and down to control one motor (or x axis), forward and backwards, and the left and right to control the second motor (y axis), forwards and backwards.

I am using an Arduino UNO with Motor shield. I have all of the wiring part sorted out, but I am having trouble with my code.

The problem is when I push forwards, the motor keeps going forward even when I stop pushing the button. I would like it to stop when I stop pushing the button. I am not too familiar with programming.

Here is the code I am using:


//Channel A or X Axis

int upPinX = 2;
int downPinX = 4;
int forwardX = 0;
int reverseX = 0;

//Channel B or Y Axis

int upPinY = 5;
int downPinY = 6;
int forwardY = 0;
int reverseY = 0;

void setup() {
 
 
//Setup Channel A or X Axis
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
pinMode(upPinX, INPUT); //Motor A forward pushbutton
pinMode(downPinX, INPUT); //Motor A backward pushbutton

//Setup Channel B or Y Axis
pinMode(13, OUTPUT); //Initiates Motor Channel A pin
pinMode(8, OUTPUT); //Initiates Brake Channel A pin
pinMode(upPinY, INPUT); //Motor A forward pushbutton
pinMode(downPinY, INPUT); //Motor A backward pushbutton

}


void loop(){

forwardX = digitalRead(upPinX);
reverseX = digitalRead(downPinX);
forwardY = digitalRead(upPinY);
reverseY = digitalRead(downPinY);

if (forwardX == LOW) {

digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 1023); //Spins the motor on Channel A at full speed

} if (reverseX == LOW) {
 
digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 1023); //Spins the motor on Channel A at full speed

} if (forwardY == LOW) {

digitalWrite(13, HIGH); //Establishes forward direction of Channel A
digitalWrite(8, LOW); //Disengage the Brake for Channel A
analogWrite(11, 1023); //Spins the motor on Channel A at full speed

} if (reverseY == LOW) {
 
digitalWrite(13, LOW); //Establishes forward direction of Channel A
digitalWrite(8, LOW); //Disengage the Brake for Channel A
analogWrite(11, 1023); //Spins the motor on Channel A at full speed

} else {
 
digitalWrite(9, HIGH); //Eengage the Brake for Channel A
digitalWrite(8, HIGH); //Engage the break for Channel B
}

}


- Also perhaps someone could let me know how I can insert code in one of those code boxes?

Thanks,





sort by: active | newest | oldest
MattiV1 year ago

i have proplem how write, if button high do relay high and if value >= X relay low,

alltime read value at sensor and if button high led/relay hig, if value >= X(limited value) do relay low. else led/relay low.

how write 2 if and not do relay high ,only if bush button relay keep high and value limti relay off, value read alltime and relay NOT must go high only if bush button relay keep high and wait when value have limti, then relay low.i think longtime and test many but no understand how write ?

now i has test if button high do led high and relay high, if x >= value do led,relay low, else all low. but not working whit 2 if ? why ?

toedip (author) 4 years ago
Hi,

Thanks for the comments steveastrouk.

My hardware will prevent two inputs being done at once. The joystick only allows one direction at a time. So that saves me a bit more coding. I did eventually figure out a solution this afternoon. I'm still not clear why this method worked, but it basically fiddling around with the if / else statements. I ended up creating one if / else group for motor A, and another for motor B. See the code below.

And would anyone know how I can post code in here without it being just as text? I've seen other posts where they have a new window within the post for code that you can scroll down. (it cleans up the post from looking too sloppy).

Thanks again!

//Channel A or X Axis

int upPinX = 2;
int downPinX = 4;
int forwardX = 0;
int reverseX = 0;

//Channel B or Y Axis

int upPinY = 5;
int downPinY = 6;
int forwardY = 0;
int reverseY = 0;

void setup() {


//Setup Channel A or X Axis
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
pinMode(upPinX, INPUT); //Motor A forward pushbutton
pinMode(downPinX, INPUT); //Motor A backward pushbutton

//Setup Channel B or Y Axis
pinMode(13, OUTPUT); //Initiates Motor Channel A pin
pinMode(8, OUTPUT); //Initiates Brake Channel A pin
pinMode(upPinY, INPUT); //Motor A forward pushbutton
pinMode(downPinY, INPUT); //Motor A backward pushbutton

}


void loop(){

forwardX = digitalRead(upPinX);
reverseX = digitalRead(downPinX);
forwardY = digitalRead(upPinY);
reverseY = digitalRead(downPinY);

if (forwardX == LOW) {

digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 1023); //Spins the motor on Channel A at full speed

} else if (reverseX == LOW) {

digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 1023); //Spins the motor on Channel A at full speed

} else {

digitalWrite(9, HIGH);

}


if (forwardY == LOW) {

digitalWrite(13, HIGH); //Establishes forward direction of Channel A
digitalWrite(8, LOW); //Disengage the Brake for Channel A
analogWrite(11, 1023); //Spins the motor on Channel A at full speed

} else if (reverseY == LOW) {

digitalWrite(13, LOW); //Establishes forward direction of Channel A
digitalWrite(8, LOW); //Disengage the Brake for Channel A
analogWrite(11, 1023); //Spins the motor on Channel A at full speed

} else {


digitalWrite(8, HIGH); //Engage the break for Channel B
}

}
Go to your Arduino page. Copy all the source as HTML
Come here
Select "Rich Editor"
Select "source"
Paste copied source as HTML into this box
Done
/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.
 
  This example code is in the public domain.
 */
 
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

This could be more hardware than software. Can you post your circuit ?

If a motor is already on, what turns it OFF ? You don't show any code that can do that. If your direction is set forwards, what happens if you simultaneously try and set it backwards ?
This looks more hardware than software. Can you post your circuit ?
toedip (author) 4 years ago
Hi,

I tried adding that else statement after every if. It doesn't like it. My up direction works for motor A. But the down direction doesn't work. I can hear a faint waining like it's trying... my guess is it's trying to go while the brake is engaged. The second bit of code I added works for me with one motor. It doesn't have the "else break" statement until the end.
toedip (author) 4 years ago
Hey,

Thanks for the input. I have this code also, which was my first building block. (dealing with one motor, and it worked fine.) I did switch up and remove the elses as part of my troubleshooting methods. I read somewhere that I do not need them in certain situations. And the code functions ok, except for it keeps the digitalWrite HIGH after my input forwardX goes HIGH again.

I thought maybe the problem is I'm daisy chaining too many if statements. I'm basically trying to say, if button a goes low, do action a, if button b goes low, do action b, ... until if button d goes low, do action b.

This code works as I want it to, so my job now is to decipher what is different between my first posted code and this one.


int upPin = 2;
int downPin = 4;
int forward = 0;
int reverse = 0;

void setup() {


//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
pinMode(upPin, INPUT); //Motor A forward pushbutton
pinMode(downPin, INPUT); //Motor A backward pushbutton

}


void loop(){


forward = digitalRead(upPin);
reverse = digitalRead(downPin);
if (forward == LOW) {

digitalWrite(12, HIGH); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 1023); //Spins the motor on Channel A at full speed

} else {


if (reverse == LOW) {

digitalWrite(12, LOW); //Establishes forward direction of Channel A
digitalWrite(9, LOW); //Disengage the Brake for Channel A
analogWrite(3, 1023); //Spins the motor on Channel A at full speed

} else {

digitalWrite(9, HIGH); //Eengage the Brake for Channel A

}

}

}
mh76dk4 years ago
i dont know arduinos specific stuff, but you basically need to add an 'else' to each of the 'if' clauses to engage the break for that direction, it seems the last one (reverseY == LOW) already does that, but all the preceding ones do not.

(i hope this short version helps, i would not like to have to write the actual code because i am sure i will mess up the writes to the different pins - but if you force me, i might be able to mock up some semi-workable untested code :) )
mh76dk mh76dk4 years ago
on closer examination it seems you can just copy-paste the last 'else' to the previous 'if' clauses... i think... ;-)

It seems it engages the breaks both motors, so it might be all you need to do (for each 'if' clause).