relay control board

I have built a relay control board that is able to control 8 relays to make two motors go forward and reverse. I want to hit one button and have one motor go forward until a physical limit switch is tripped, then for the other motor to move forward until a third switch is tripped. Any suggestions on code structure? I am having trouble making a do or do while command work at all. if I want a relay to turn on while a certain limit switch is not tripped why should it be so hard?

void loop()
   // "I want to hit one button and..."
    while (button_not_pressed)
         /* Just wait here. */;

    // "... have one motor go forward until a physical limit switch is tripped, ...
   while (limit_switch_1_not_reached)
         /* Just wait here. */;

    // ... then for the other motor to move forward until a third switch is tripped. 
   while (limit_switch_2_not_reached)
         /* Just wait here. */;

    while(1);  //  End of program.

so then things like motor2forward() would be written out later as the exact digitalWrites i want to move the correct motor but I am still wondering what the motor2stop() will look like. I guess it would be the opposite commands from that which made the motor go forward.

void loop() { startState = digitalRead(start); prog1State = digitalRead(prog1); prog2State = digitalRead(prog2); prog3State = digitalRead(prog3); prog4State = digitalRead(prog4); xHomeState = digitalRead(xHome); xLimitState = digitalRead(xLimit); yPos1State = digitalRead(yPos1); yPos2State = digitalRead(yPos2); yPos3State = digitalRead(yPos3); yPos4State = digitalRead(yPos4);

while(prog1State !=HIGH) xMotorForward();

while (xLimitState !=HIGH) xMotorStop();


while (yPos2State != HIGH) yMotorStop();


void xMotorForward() { digitalWrite(2, HIGH); }

void xMotorStop()

{ digitalWrite(2, LOW); }

void yMotorForward() { digitalWrite(3,HIGH); }

void yMotorStop() { digitalWrite(3,HIGH); }

when I load this sketch I get "digitalWrite(2,HIGH); as a result and no matter what switches i hit that does not change.

You have the relays set up like a H-bridge so you can go back & forth? For example: K1/K2 on go one direction, K3/K4 go the other direction digitalWrite the outputs high to move, and low when you want to stop. If the motor tends to coast along, then add a burst of opposite direction to make it stop.

yes I have the two motors set up like an H-Bridge. In the code I wrote for the sake of simplicity I am only digitalWriting one pin. In the real sketck there will be more digitalWrites going on but I am still having problems getting pin2 to shut off and at that moment light up pin3 which represents the second motor moving forward. Your code looks like it should work but nothing changes no mater what switch I press after the initial pin2 HIGH situation.

Part of the problem is you only tell pin3 to go high:

void yMotorForward()

void yMotorStop()

How are your switches wired up? Are they inputs with internal pullups:

pinMode(xHome, INPUT);
digitalWrite(xHome, HIGH);

that you then connect to ground to make it read as a 0 when pressed?

const int start = 33; const int prog1 = 35; const int prog2 = 37; const int prog3 = 39; const int prog4 = 41; const int xHome = 43; const int xLimit = 45; const int yPos1 = 47; const int yPos2 = 49; const int yPos3 = 51; const int yPos4 = 53;

int startState = 0; int prog1State = 0; int prog2State = 0; int prog3State = 0; int prog4State = 0; int xHomeState = 0; int xLimitState = 0; int yPos1State = 0; int yPos2State = 0; int yPos3State = 0; int yPos4State = 0;

void setup() { pinMode(start, INPUT); pinMode(prog1, INPUT); pinMode(prog2, INPUT); pinMode(prog3, INPUT); pinMode(prog4, INPUT); pinMode(xHome, INPUT); pinMode(xLimit, INPUT); pinMode(yPos1, INPUT); pinMode(yPos2, INPUT); pinMode(yPos3, INPUT); pinMode(yPos4, INPUT);

pinMode(2, OUTPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); }

no they read as HIGH when pressed. They are grounded to external 10k resistors

and I fixed

void xMotorForward() { digitalWrite(2, HIGH); }

void xMotorStop()

{ digitalWrite(2, LOW); }

void yMotorForward() { digitalWrite(3,HIGH); }

void yMotorStop() { digitalWrite(3,LOW); }

it still doesn't work

prog1State = digitalRead(prog1);
while(prog1State != HIGH)

is NOT the same as:

while(digitalRead(prog1) != HIGH)

In your code you are reading the button ONCE and acting on the reading repeatedly. If you want the button to work you have to check the button repeatedly.

thanks, that was honestly the next thing I was going to try. I'll let you know how it goes.

ok guys I got it working. Thanks a million. This is my first project so I was pretty well stuck in the sand before you guys helped out so much. This project is for a 2 axis glue machine where the glue dispensing head moves over a table with the object to be glued underneath. It makes up to 4 passes because the glue dispenser is 12 inches wide and the maximum width of the thing to be glued would be 48 inches. The only question I have left is how to add an emergency stop function in the code. I'm sure if I tinker long enough I'll figure it out but I figured I'd ask lol. I'm guessing I can use the allStop() function I made for that but I'm unsure where to add it. BTW here is a segment of my working sketch for a full four passes of the glue head

void loop() { if (digitalRead(prog4) == HIGH) {program4();} }

void program4() { while (digitalRead(xLimit) != HIGH) xPass1();

while (digitalRead(yPos2) != HIGH) yMove1();

while (digitalRead(xHome) != HIGH) xPass2();

while (digitalRead(yPos3) != HIGH) yMove2();

while (digitalRead(xLimit) != HIGH) xPass1();

while (digitalRead(yPos4) != HIGH) yMove3();

while (digitalRead(xHome) != HIGH) xPass2();

while (digitalRead(xHome) != LOW) allStop(); }

void xPass1() { digitalWrite(4, LOW); delay(50); digitalWrite(2,HIGH); }

void xPass2() { digitalWrite(4,LOW); delay(50); digitalWrite(3,HIGH); }

void yMove1() { digitalWrite(2,LOW); delay(50); digitalWrite(4,HIGH); }

void yMove2() { digitalWrite(3,LOW); delay(50); digitalWrite(4,HIGH); }

void yMove3() { digitalWrite(2,LOW); delay(50); digitalWrite(4,HIGH); }

void allStop() { digitalWrite(2,LOW); digitalWrite(3,LOW); digitalWrite(4,LOW); digitalWrite(5,LOW); digitalWrite(6,LOW); digitalWrite(7,LOW); digitalWrite(8,LOW); }

You have a free interrupt pin? Run void allstop() when your emergency button is pressed.