Go Down

Topic: help needed in this code for h-bridge ? (Read 450 times) previous topic - next topic

Jul 05, 2012, 03:00 am Last Edit: Jul 05, 2012, 03:08 am by ahmed-ragab-hamed Reason: 1
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??

Nick Gammon

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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

PaulS

Quote
**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?

silasmoeckel

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.

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy