Help with coding DPDT switch

Hi guys,

I am a complete noob when it comes to anything Arduino and i'm working with a buddy to get our program working. Our goal is literally to just drive two stepper motors up and down on the switch command. We are using the following:

DPDT Switch: (Baomain Momentary 6 Pin DPDT Button On/Off/On Rocker Switch AC 250V/10A 125V/15A)

Stepper Motors: (STEPPERONLINE CNC Stepper Motor Nema 23)

Controllers: (SainSmart CNC Micro-Stepping Stepper Motor Driver)

Arduino DUE

The current code that we have allows the steppers to rotate CCW when we press down on our switch but nothing for the up direction. Could anyone help point us in the right direction? Current concerns are internal pull-up resistors (logic switch), pulse inversion, or the switch itself using 3.3 volts as high.

Here is the code:

// ARDUINO DUE Conrol of 2x ST-M5045 Stepper Motor Controllers
 
const int interval = 700;   // Pulse inteval. Controls RPM
                            // 500 likely max speed       // Full wave in 700 mircosconds
                            // 1000 is med speed
                            // 1400 is slow speed
//const int flatline = 10000000;   // forced low or zero logic value for testing
boolean pulse = LOW;

void setup() {  
 Serial.begin(9600);      // start serial connection
  pinMode(8, OUTPUT);      // Dir - Pin                     MOTOR1
  pinMode(9, OUTPUT);      // Pul - Pin                     MOTOR1
                                      //digitalWrite(7 & 9, HIGH);    // Shunt?
  pinMode(3, OUTPUT);      // Dir - Pin                     MOTOR2
  pinMode(5, OUTPUT);      // Pul - Pin                     MOTOR2

  pinMode(A1, INPUT_PULLUP);     // WHT. Switch Pin 1 (HOT)        DPDT             flipped logic setup
  pinMode(A2, INPUT_PULLUP);     // GRN. Switch Pin 5 (HOT)        DPDT
  pinMode(A4, INPUT_PULLUP);     // BLK  Switch Pin 2    (GND)     DPDT
  pinMode(A5, INPUT_PULLUP);     // BLU  Switch Pin 6    (GND)     DPDT
  pinMode(13, OUTPUT);    // Onboard LED for testing
  //pinMode(, INPUT_PULLUP);       // Enables onboard 10k_ohm resistor

  digitalWrite(8, LOW);     // Low CCW / High CW         MOTOR 1
  digitalWrite(9, LOW);     // Pulse                     MOTOR 1      // throwing early?
  digitalWrite(3, LOW);     // Low CCW / High CW      MOTOR 2
  digitalWrite(5, LOW);     // Pulse                  MOTOR 2         // throwing early?
}

void loop() {
int sensorA1 = digitalRead(A1);   //read pin A11 WHT (HOT)
int sensorA2 = digitalRead(A2);   // Inverted logoc UP in DPDT switch (top off hot, prong 3)
int sensorA5 = digitalRead(A5);   // BLU (GND)
int sensorA4 = digitalRead(A4);   // GREN LED
Serial.println(sensorA1);      //print out the value of the pushbutton (sceen if connected)

if (sensorA5 == HIGH) {     // UP DIR Button DPDT, Blue LED      ////////UP/////////
  //digitalWrite(13, LOW);  
  digitalWrite(8, LOW);     // Low CCW / High CW         MOTOR 1
  digitalWrite(9, pulse);     // Pulse                     MOTOR 1                        ***************
  digitalWrite(3, LOW);      // Low CCW / High CW      MOTOR 2
  digitalWrite(5, LOW);     // Pulse                  MOTOR 2                           ******************
  pulse = !pulse;               // inverts pulse value
  //digitalWrite(9, pulse);       // Writes new value to port   MOTOR 1
  //digitalWrite(5, pulse);       // Writes new value to port   MOTOR 2
  delayMicroseconds(interval);
}
if (sensorA1 == HIGH) {   // DOWN Dir Button DPDT, LED Pin 13 onboardLED     ///DOWN////    ***works rn. toggles action
  digitalWrite(13, LOW);
  digitalWrite(8, HIGH);     // Low CCW / High CW         MOTOR 1
  digitalWrite(9, LOW);     // Pulse                     MOTOR 1
  digitalWrite(3, HIGH);     // Low CCW / High CW      MOTOR 2
  digitalWrite(5, LOW);     // Pulse                  MOTOR 2
  pulse = !pulse;               // inverts pulse value
  //digitalWrite(9, pulse);       // Writes new value to port   MOTOR 1
  //digitalWrite(5, pulse);       // Writes new value to port   MOTOR 2
  delayMicroseconds(interval);
}
else {                                                                  // Neutral- No Manual DPDT Switch Input ***works rn.
   //pulse = !pulse;               // inverts pulse value
   //digitalWrite(9, pulse);       // Writes new value to port   MOTOR 1
  // digitalWrite(5, pulse);       // Writes new value to port   MOTOR 2
   //delayMicroseconds(flatline);
   digitalWrite(13, HIGH);   // NO LED 13
  }
 }  

//////////// Revised Lines of Code- Ref.  -  ////////////////////

 // readingA5_0 = analogRead(A5);
 //Serial.println(sensorA5);
  //if (sensorA1 == HIGH) {       // UP LIGHT 13
 //   digitalWrite(13, LOW);
  //if (sensorA5 == HIGH) {
  //digitalWrite(13, LOW);      // DOWN LIGHT LED 13
  // digitalWrite(9, HIGH);       // "||" for OR        /// CANNOT USE OR FN
  //delayMicroseconds(interval);
  //digitalWrite(9, LOW);
  //delayMicroseconds(interval);
  //int sensorA2 = digitalRead(A2);   // GRN (HOT)
  //int sensorA3 = digitalRead(A4);   // BKL (GND
  //pinMode(7, OUTPUT);      // ENA -   ALWAYS READ POS V   MOTOR1

Maybe there's a problem in here?

  digitalWrite(8, LOW);     // Low CCW / High CW         MOTOR 1
  digitalWrite(9, pulse);     // Pulse                     MOTOR 1                        ***************
  digitalWrite(3, LOW);      // Low CCW / High CW      MOTOR 2
  digitalWrite(5, LOW);     // Pulse                  MOTOR 2                           ******************

You can do yourself a big favor by using descriptive names for the I/O. Instead of

pinMode(8, OUTPUT); // Dir - Pin MOTOR1

at the top where you declare your variables add something like

const uint8_t directionMtr1 = 8;

Then any time you refer to pin 8 you can do it like this:

pinMode(directionMtr1, OUTPUT); // now the comment is not needed because the variable describes what's being affected.

Another advantage is if you later move the function to a different pin you need only change it once in the const statement to apply to all instances.

It appears from the comments that DOWN would be the direction not working rather than UP. Add some Serial.print()s in the motor control to indicate what the code is doing at runtime.

You didn’t provide a schematic, but based on your code, I’ll guess you have your switch wired wrong. If you look at one side of the switch, there is pin 1a, 1, and 1b. The pin in the middle (1) needs to be tied to ground. The other two pins need to be tied to two of the Arduino pins using INPUT_PULLUP. Duplicated this for side 2 of the switch to different pins.

When the rocker switch is moved in the “UP” position, pin 1a will read LOW and pin 1b will read HIGH. When it is in the “DOWN” position, they values will be reversed.

I didn’t look at the schematic for this switch so my logic may be reversed. Test it out and find out.

Thanks guys ill give this a try and update in the morning!

UPDATE: I had noticed some intermittent continuity issues with the jumper wires into the breadboard. I swapped the jumpers to some thicker gage wire and seem to be getting “cleaner” signals.

Looking into your recommendations and DPDT switch wiring again!

I checked the wiring and everything seems fine there. Any other help?

Let's roll back a bit.

I just created a simple illustrated tutorial
Choosing the right switch!

You can have higher Ps and Ts - irrelevant in this discusssion.