Cannot Turn Stepper Motor Off with a Three-Way Switch [SOLVED]

Hello,

I posted about this project a while ago for help, but I’ve run into a new problem, so I’m back!

I am trying to control a stepper motor using the Sparkfun EasyDriver and a three-way switch. When I have the switch in one position it is supposed to spin the motor one way, when it’s in the opposite position it’s supposed to spin the other way, and when it’s in the middle the motor’s supposed to stop.

I’m having trouble getting that last part to work right. I’ve been playing around with different ways for a couple weeks now, from switch/case declarations to if/else statements. Nothing seems to work right. The code that has been working best for the first two conditions above is attached here. The problem is that when the switch is in the middle it acts as if it were position B.

I don’t know if my problem is because I’m not telling the code how to turn off the motor right or if it’s not acknowledging that the switch is in the middle position right, but something’s not working here.

Here’s my code:

#include "Arduino.h"

const int posA = 4;     // Position A pin
const int posB = 5;     // Position B pin

int dirpin = 2;
int steppin = 3;

void setup() 
{
pinMode(posA, INPUT);    // set posA as input  
pinMode(posB, INPUT);    // set posB as input
digitalWrite(posA, HIGH);
digitalWrite(posB, HIGH);      //Sets step pin as an output
pinMode(dirpin, OUTPUT);       //Sets direction pin as an output
pinMode(steppin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int stateA = digitalRead(posA);     // variables for reading the switches status
int stateB = digitalRead(posB);
      

//If the switch is turned to side A it should rotate as direction LOW
  if(stateA == HIGH){
    digitalWrite(dirpin, LOW);
    digitalWrite(steppin, LOW); 
    digitalWrite(steppin, HIGH);
    delayMicroseconds(500);
  }
 //If the switch is turned to side B it should rotate as direction HIGH
  else if(stateB == HIGH){
    digitalWrite(dirpin, HIGH);
    digitalWrite(steppin, LOW);  
    digitalWrite(steppin, HIGH); 
    delayMicroseconds(500);
  }
  
  //If the switch is in the middle it should now rotate, because step pin does not alternate to create a "rising edge"
  else if(stateA == LOW && stateB == LOW){
    digitalWrite(dirpin, LOW);
    digitalWrite(steppin, LOW);
  }
}

Attached is a picture of how my breadboard is wired up. Any help would be appreciated. Thanks!

Jason

Since you did a digitalWrite(posA/B, HIGH) on your input pins, they are tied internally to HIGH; So when the switch is in the middle position, you will not get LOW on either of them! Try to reverse the checks in the if statements to negative logic (i.e. HIGH means inactive, LOW means active).

Wow, I'm really surprised I never thought of that. Thank you so much, it works perfectly now!

JasonB579:
Wow, I'm really surprised I never thought of that. Thank you so much, it works perfectly now!

:slight_smile: You pick these things up with experience...