Pages: [1]   Go Down
Author Topic: help needed in this code for h-bridge ?  (Read 307 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 - downloaded 3 times.)
* switch case.txt (1.68 KB - downloaded 1 times.)
« Last Edit: July 04, 2012, 08:08:44 pm by ahmed-ragab-hamed » Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 115
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: