DC motor control via relay shield, CODE help

Hello All,
I am fairly new to this programming world and have a few questions, please don't make too much fun of me. :wink: Below is my code I am having an issue with. I am controlling two DC motors with a relay shield and a three position latching switch. I can get position B and off to work just fine, but cannot get any response with position A. I am to the point where I'm pretty sure it's something with my code as I've set up a serial monitor and tried swapping inputs and switches. Down the road I would like to add a trimming, for more adjustment, but that will be after I get the three position working first.

Thanks for the help!!


#define   POS_A_ON                digitalWrite(POS_A_PIN, HIGH)
#define   POS_A_OFF               digitalWrite(POS_A_PIN, LOW)
#define   POS_B_ON                digitalWrite(POS_B_PIN, HIGH)
#define   POS_B_OFF               digitalWrite(POS_B_PIN, LOW)

#define   SWITCH_POSITION_A_PIN   12

// variables
byte switchState = 0;             // to store switch reading 0 = off 1 = posA 2 = posB
byte lastSwitchState = 0;         // to check for change

int relay0 = 8;                 // Relay 0 connected to pin 8 (left out)
int relay1 = 7;                 // Relay 1 connected to pin 7 (left in)
int relay2 = 2;                 // Relay 2 connected to pin 2 (right out)
int relay3 = 4;                 // Relay 3 connected to pin 4 (right in)
int switchPin1 = 6;             // Trim switch in
int switchPin2 = 9;             // Trim switch out

void setup() {
  pinMode(relay0, OUTPUT);      // sets the digital pin as output
  pinMode(relay1, OUTPUT);
  pinMode(relay2, OUTPUT);
  pinMode(relay3, OUTPUT);
  pinMode(switchPin1, INPUT);
  pinMode(switchPin2, INPUT);

void loop(){
  // Read the switch and put the result is switchState
  if (READ_SWITCH_POSITION_A == HIGH) switchState = 1;
  if (READ_SWITCH_POSITION_B == HIGH) switchState = 2;
  else switchState = 0;

  // Only do anything if the state has changed
  if (switchState != lastSwitchState){
    lastSwitchState = switchState;
    case 1:
       digitalWrite(relay0, HIGH);   // sets relay 0 on (left out)
  delay(5000);      // waits for 5 second
  digitalWrite(relay0, LOW);    // sets relay 0 off
    case 2:
      digitalWrite(relay2, HIGH);   // sets relay 2 on (right out)
  delay(5000);      // waits for 5 second
  digitalWrite(relay2, LOW);    // sets relay 0 off
    case 0:      
      digitalWrite(relay1, HIGH);   // sets relay 1 on  (left in)
      digitalWrite(relay3, HIGH);   // sets relay 3 on  (right in)
  delay(5000);      // waits for 5 second
  digitalWrite(relay1, LOW);    // sets relay 1 off
  digitalWrite(relay3, LOW);    // sets relay 3 off
      Serial.println("Switch Off");

First of all Welcome to Debugging programming and electronics at the same time all with no debugger, also please put the code in the code brackets it makes it easier to read.
And, Clean up your code, delete unused variables and defines.
If you are new to this, start a little simpler with how you write your code it will be easier for you to read and debug. And break it down in pieces, is it your wiring or is it your code, make that distinction by creating a sketch just to make that one piece work do not try to tackle it all at once.
Hook up your switch and use the code below, MAKE sure you change the buttonPin variable to whatever you are using for position B. If the led light on the board by pin 13 lights up when the switch is on and is not lit when the switch is off then your wiring is correct. Without running your code it looks fine. Check all the wiring, are you using pull down resistors on the switches?

int buttonB = 6;

void setup() {

void loop() {

Thanks for your help Germ. I tried your button tester with both of my buttons/switches. They both work and light up the onboard LED. I am fairly proficient with wiring, and both on my inputs have pulldown resistors on them. The wiring is mine and the code is mostly mine. I got some help with the switch state stuff, that was confusing for me and that was where I thought my problem would lie. I even set up the serial print so I could see what part of the code wasn't outputting. I cannot get any response from case1, no matter which input or the switch I use. Any other ideas and help is appreciated.


  if (READ_SWITCH_POSITION_A == HIGH) switchState = 1;
  if (READ_SWITCH_POSITION_B == HIGH) switchState = 2;
  else switchState = 0;

You will never end up with switchState equal 1. It will always be 2 or 0.

The second if looks like it should be an else if statement. That way, you would end up with 0, 1, or 2.

Thank you thank you PaulS. That fixed it, your awesome!! I never would have caught that, crazy how something that simple can throw everything off.