help needed in this code for h-bridge ?

i tried this code to change the motion of 2 dc motors :
**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);
switch (state){
case 1:
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, HIGH);
break;
case 0:
digitalWrite (relay1, LOW);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, LOW);
break;
}

state = digitalRead(backward);
switch (state){
case 1:
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, HIGH);
break;
case 0:
digitalWrite (relay1, LOW);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, LOW);
break;
}

state = digitalRead(right);
switch (state){
case 1:
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, HIGH);
break;
case 0:
digitalWrite (relay1, LOW);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, LOW);
break;
}

state = digitalRead(left);
switch (state){
case 1:
digitalWrite (relay1, HIGH);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, HIGH);
break;
case 0:
digitalWrite (relay1, LOW);
digitalWrite (relay2, LOW);
digitalWrite (relay3, LOW);
digitalWrite (relay4, LOW);
break;
}

}

**the relays does't work what is the problem??

switch case.txt (1.68 KB)

switch case.txt (1.68 KB)

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

**the relays does't work what is the problem??

Do they work in a sketch that JUST toggles the relays? Are we looking at a software problem or a hardware problem?

Well assuming the hardware is fine that code does not look like it will work you have 4 separate switch cases each setting the relays (and all of them set relay 1-4 to 1001). So if you were pressing forward it would set relay 1 and 4 high then the next on would set them low again when it notices that backwards was not pressed. The only way that code works is if all 4 buttons are pressed simultaneously and if they are are very clean as you did not debounce them. Now the fix is dependent on the hardware, can it be going forward/backwards and turning at the same time? I assume you want to do more things once you get this bit done so while loops are not an option.