Go Down

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

ahmed-ragab-hamed

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.
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
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview