Control motor from Relay using button

Hi everyone,

I'm completely new to the world of Arduino and just got my first Arduino and 8 relay.
For the past couple of days I've been setting everything up but just now ran into my first problem with writing actual code to run a motor.

I have gotten so far that my code runs the motor CW for the time I want, then stops, then runs the motor CCW and stops.

Basically I have a couple of button / limswitches (normaly open and normaly closed) laying around which I want to use to stop the motor whenever the switch is pressed.

So in my opinion the code should do this:

  1. check if bottomLimitSwitch is pressed

if false: give power to Relay1 so the motor runs CCW until it presses the bottomLimitSwitch, then it should stop.

if true: give power to Relay1 so the motor runs CW until it presses the topLimitSwitch, then it should pause for 5 seconds, then run CCW until it hits bottomLimitSwitch.

I have come this far and could really use some help:

// These data pins link to 4 Relay board pins IN1, IN2, IN3, IN4


#define RELAY1  12                       

#define RELAY2  13

#define limSwitchBottom

#define limSwitchTop


void setup()

{    

// Initialise the Arduino data pins for OUTPUT

 pinMode(RELAY1, OUTPUT);       

 pinMode(RELAY2, OUTPUT);

 pinMode(limSwitchBottom, INPUT);

 pinMode(limSwitchTop, INPUT);

}

int buttonState = 0;                        // variable for reading the pushbutton status

void loop(){

const int limSwitchBottom = 1;            // the number of the    bottom   Limit switch pin
const int limSwitchTop = 2;              // the number of the    top      Limit switch pin



 buttonState = digitalRead(limSwitchBottom);
  digitalWrite(RELAY1,LOW);                                             // Turns ON Relay number 1
    delay(4000);                                                       // Let motor run for 4 seconds
     if(buttonState == LOW);                                          // Wait for limit switch to un-trigger
      //while (digitalRead(limSwitchBottom) == LOW);                 // Wait for limit switch to un-trigger
        digitalWrite(RELAY1,HIGH);                                  // Turns Relay number 1 Off


 buttonState = digitalRead(limSwitchTop);
  digitalWrite(RELAY2,LOW);                                        // Turns ON Relay number 1
    delay(4000);                                                  // Let motor run for 4 seconds
      if(buttonState == LOW);                                    // Wait for limit switch to un-trigger
        digitalWrite(RELAY2,HIGH);                              // Turns Relay number 2 Off

}

OK, on lines 8 and 10, you haven't specified pin numbers for the switches.
23 and 25, you have the unknown pins as INPUTs, are the switches connected to 5V or ground?
33 and 35, you are defining pin numbers with the same names as the previous defines ( you can't use pin 1, it's the serial TX pin), use the first defines and delete lines 33 and 35.
Fix those and see what happens and report back.

Hey there!

thanks for your reply!

I made the adjustments and the code does not throw any errors now.
I did not connect the limit switches as of yet, but I was planning to connect them to ground.

If you read the code in my loop(), should it do what I want it to do(?), which is:

1. check if bottomLimitSwitch is pressed
if false: give power to Relay1 so the motor runs CCW until it presses the bottomLimitSwitch, then it should stop.
if true: give power to Relay1 so the motor runs CW until it presses the topLimitSwitch, then it should pause for 5 seconds, then run CCW until it hits bottomLimitSwitch.

// These data pins link to 4 Relay board pins IN1, IN2, IN3, IN4


#define RELAY1  12                       
#define RELAY2  13
#define limSwitchBottom  1
#define limSwitchTop  2

void setup(){    

// Initialise the Arduino data pins for OUTPUT

 pinMode(RELAY1, OUTPUT);       
 pinMode(RELAY2, OUTPUT);
 pinMode(limSwitchBottom, INPUT);
 pinMode(limSwitchTop, INPUT);

}

void loop(){
 
int buttonState = 0;                        // variable for reading the limSwitch status

 buttonState = digitalRead(limSwitchBottom);
  digitalWrite(RELAY1,LOW);                                             // Turns ON Relay number 1
    delay(4000);                                                       // Let motor run for 4 seconds
     if(buttonState == LOW);                                          // Wait for limit switch to un-trigger
      //while (digitalRead(limSwitchBottom) == LOW);                 // Wait for limit switch to un-trigger
        digitalWrite(RELAY1,HIGH);                                  // Turns Relay number 1 Off


 buttonState = digitalRead(limSwitchTop);
  digitalWrite(RELAY2,LOW);                                        // Turns ON Relay number 1
    delay(4000);                                                  // Let motor run for 4 seconds
      if(buttonState == LOW);                                    // Wait for limit switch to un-trigger
        digitalWrite(RELAY2,HIGH);                              // Turns Relay number 2 Off

}

rem what outsider said do not use pin number 1 for any inputs or outputs as it is used for serial tx. otherwise the code seems ok. change the 1 to a 3 or something besides 0 or 1.

rogertee:
rem what outsider said do not use pin number 1 for any inputs or outputs as it is used for serial tx. otherwise the code seems ok. change the 1 to a 3 or something besides 0 or 1.

changed it, completely forgot about that, my bad.

thanks for pointing that out!

Made some adjustments, does this look OK or is there another/ better way to do what I want?
I created notes with every line so it is easy to read and understand, hopefully..
Also I wasnt sure to use boolean or int.

#define RELAY1  12                       
#define RELAY2  13
#define limSwitchBottom  3
#define limSwitchTop  2

void setup(){    

// Initialise the Arduino data pins for OUTPUT

pinMode(RELAY1, OUTPUT);      
pinMode(RELAY2, OUTPUT);
pinMode(limSwitchBottom, INPUT);
pinMode(limSwitchTop, INPUT);

}


void loop(){

boolean bottomButtonState;             // variable for reading the bottom limit Switch status
boolean topButtonState;                          // variable for reading the top limit Switch status

/* Maybe try int if bool doesnt work.
 int bottomButtonState = 0;             // variable for reading the bottom limit Switch status
 int topButtonState = 0;                          // variable for reading the top limit Switch status
 */

 bottomButtonState = digitalRead(limSwitchBottom);
 topButtonState = digitalRead(limSwitchTop);
 
 
if(bottomButtonState == LOW)                                           // Check for (pushed) state bottom limit switch
 {
 digitalWrite(RELAY1,LOW);                                            // If bottom limit switch is pressed, turn ON Relay number 1 to run motor CW
 while(digitalRead(topButtonState) == LOW);                          // Wait for top limit switch to trigger
 digitalWrite(RELAY1,HIGH);                                         // Stop motor from running CW
 delay(4000);                                                      // Pause 4 seconds
 digitalWrite(RELAY2,LOW);                                        // run motor CCW back to bottom position
 while(digitalRead(bottomButtonState) == LOW);                   // Wait for bottom limit switch to trigger
 digitalWrite(RELAY2,HIGH);                                     // Stop motor from running CWW.
 }
 
else if(topButtonState == LOW)                                // Check for (pushed) state top limit switch
 {
 digitalWrite(RELAY2,LOW);                                   // If top limit switch is pressed, turn ON Relay number 2 to run motor CCW
 while(digitalRead(bottomButtonState) == LOW);              // Wait for bottom limit switch to trigger
 digitalWrite(RELAY2,HIGH);                                // Stop motor from running CCW
 delay(4000);                                             // Pause 4 seconds
 } 

else if(topButtonState == HIGH && bottomButtonState == HIGH)              // Check for unpushed state both top and bottom limit switch
 {
 digitalWrite(RELAY2,LOW);                                               // if TRUE Turns ON Relay number 2 to run motor CCW
 while(digitalRead(bottomButtonState) == LOW);                          // Wait for bottom limit switch to trigger
 digitalWrite(RELAY2,HIGH);                                            // Stop motor from running CCW
 delay(2000);                                                         // Pause 2 seconds
 digitalWrite(RELAY1,LOW);                                           // Turns ON Relay number 1 to run motor CW
 while(digitalRead(topButtonState) == LOW);                         // Wait for top limit switch to trigger
 digitalWrite(RELAY1,HIGH);                                        // Stop motor from running CW
 delay(4000);                                                     // Pause 4 seconds
 digitalWrite(RELAY2,LOW);                                       // if TRUE Turns ON Relay number 2 to run motor CCW
 while(digitalRead(bottomButtonState) == LOW);                  // Wait for bottom limit switch to trigger
 digitalWrite(RELAY2,HIGH);                                    // Stop motor from running CCW
 }
 }