want to check this code for h-bridge of 2 motors ??

hi . I Started to write this code to change the rotation of 2 motors using 4 relays like that :

**the code:

const int forward = 2;
const int backward = 3;
const int right = 4;
const int left = 5;
const int relay1 = 6;
const int relay2 = 7;
const int relay3 = 8;
const int relay4 = 9;
int state = 0;

void setup()
{
pinMode(forward, INPUT);
pinMode(backward, INPUT);
pinMode(right, INPUT);
pinMode(left, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
}

void loop()
{
state = digitalRead(forward);
if (state == HIGH) {
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, HIGH);
}

state = digitalRead(backward); --------------------(33)
else if (state == HIGH) {
digitalWrite (relay1, LOW);
digitalWrite (relay2, HIGH);
digitalWrite (relay3, HIGH);
digitalWrite (relay4, LOW);
}

state = digitalRead(right); -----------------------(41)
else if (state == HIGH) {
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, HIGH);
digitalWrite (relay4, LOW);
}

state = digitalRead(left); --------------------(49)
else if (state == HIGH) {
digitalWrite (relay1, LOW);
digitalWrite (relay2, HIGH);
digitalWrite (relay3, LOW);
digitalWrite (relay4, HIGH);
}
}

**the problem :

**there is an error appears after the verifying :

**the error :

t_1.cpp: In function 'void loop()':
t_1:33: error: 'else' without a previous 'if'
t_1:41: error: 'else' without a previous 'if'
t_1:49: error: 'else' without a previous 'if'

what is the solve ??
thx :slight_smile:

t_1.ino (1.08 KB)

The syntax problem is right there every time:

...digitalWrite (relay4, HIGH);
}
state = digitalRead(backward);
else if ...

when an if ends with }, the else is expected right away
if { } else { }
or if { } else if { }
so you'll have to move that

state = digitalRead(backward);

to some other place in your code :wink:

so the problem in this :

state = digitalRead(forward);
state = digitalRead(backward);
state = digitalRead(right);
state = digitalRead(left);

don't use this?

Looks to me like you can just make each else if a simple if.

Only one is invoked at a time, so each time thru the loop it drops thru the ifs, reads your buttons and goes ahead, back, left or right.

Edit... just noticed they're in setup().... probably want to move them to loop() else it'll only read your buttons once.

More edit.... might want to put one set of writes with no button tests in setup(), maybe making them all low, so you have a known configuration when you fire up.

Jim

So from what i understand it's an H bridge for 2 DC motors and not a dual H bridge for 2 stepper motors right...?
Yes the total solution is what JimboZA recommended, since you read the inputs of 4 buttons, you can really remove the "else" and just use 4 single ifs one after another

void loop()
{
state = digitalRead(forward);
if (state == HIGH) {
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, HIGH);
}

state = digitalRead(backward);
if (state == HIGH) {
digitalWrite (relay1, LOW);
digitalWrite (relay2, HIGH);
digitalWrite (relay3, HIGH);
digitalWrite (relay4, LOW);
}

state = digitalRead(right);
if (state == HIGH) {
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, HIGH);
digitalWrite (relay4, LOW);
}

state = digitalRead(left);           --------------------(49)
if (state == HIGH) {
digitalWrite (relay1, LOW);
digitalWrite (relay2, HIGH);
digitalWrite (relay3, LOW);
digitalWrite (relay4, HIGH);
} 
}

thank you for your help : JimboZA & vagos21 i appreciate your Interesting to my problem
that's work :smiley:

yes that's 2 dc motors and i thought that "else if" must be written but the only "if" work with me but when i use "else if" ??
thx :slight_smile:

there is an other problem need your help plz :slight_smile:
i want to make a state == low at the code to make the relays to stop after each state == high
when i press the "forward" button "relay 1" and "relay 2" go high and when i release the button the relays keep high but i need it to be low ...
what should i do to achieve the low state ???

**the code:

const int forward = 2;
const int backward = 3;
const int right = 4;
const int left = 5;
const int relay1 = 6;
const int relay2 = 7;
const int relay3 = 8;
const int relay4 = 9;
int state = 0;

void setup()
{
pinMode(forward, INPUT);
pinMode(backward, INPUT);
pinMode(right, INPUT);
pinMode(left, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
}

void loop(){
state = digitalRead(forward);
if (state == HIGH) {
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, HIGH);
}

state = digitalRead(backward);
if (state == HIGH) {
digitalWrite (relay1, LOW);
digitalWrite (relay2, HIGH);
digitalWrite (relay3, HIGH);
digitalWrite (relay4, LOW);
}

state = digitalRead(right);
if (state == HIGH) {
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, HIGH);
digitalWrite (relay4, LOW);
}

state = digitalRead(left);
if (state == HIGH) {
digitalWrite (relay1, LOW);
digitalWrite (relay2, HIGH);
digitalWrite (relay3, LOW);
digitalWrite (relay4, HIGH);
}

}

How about:

Have a pull-down resistor on that button so that when it's released it goes low. Then add a block of code looking for the button to be low and set the relays the way you want them next time it goes thru the loop.

Jim

i actually use the pull-down resistor . the problem in the code. i tried a lot of codes but one of them work with me. but by adding an external button to the leg pin no "10" and use it if the button "high" all relays "low" , and if the button "low" all relays "high" and it's work right as shown :

** the code :
const int forward = 2;
const int backward = 3;
const int right = 4;
const int left = 5;
const int relay1 = 6;
const int relay2 = 7;
const int relay3 = 8;
const int relay4 = 9;
const int hold = 10; -------------------------> the button witch stop the relays
int state = 0;

void setup()
{
pinMode(forward, INPUT);
pinMode(backward, INPUT);
pinMode(right, INPUT);
pinMode(left, INPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
}

void loop(){
state = digitalRead(forward);
if (state == HIGH) {
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, HIGH);
}

state = digitalRead(backward);
if (state == HIGH) {
digitalWrite (relay1, LOW);
digitalWrite (relay2, HIGH);
digitalWrite (relay3, HIGH);
digitalWrite (relay4, LOW);
}

state = digitalRead(right);
if (state == HIGH) {
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, HIGH);
digitalWrite (relay4, LOW);
}

state = digitalRead(left);
if (state == HIGH) {
digitalWrite (relay1, LOW);
digitalWrite (relay2, HIGH);
digitalWrite (relay3, LOW);
digitalWrite (relay4, HIGH);
}

state = digitalRead(hold); ----------------------------- "the condition"
if (state == HIGH) {
digitalWrite (relay1, LOW);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, LOW);
}

}
this code work with me . but is there any code to solve this problem without adding this external button??

thank you ... i appreciate your help :slight_smile: