stepper motor to move set number of degrees with a switch

hey guys, need a hand this may be simple but i cant work it out and need help with it,

im trying to get a stepper motor to move a set number of degrees when a toggle switch is on and then

turn the same amount of degrees the other way when its off, almost like a servo i need to move a slider

so far, i a can it to turn 1 way all the time and the other way all the time when a switch is pressed but i

need it to stop after so many degrees

here is my code

const int stepPin = 4;   //pin to pulse for steps
const int dirPin = 3;    //pin to change step direction
const int Switch = 7;


void setup(){

 //set pins as outputs
 pinMode(stepPin, OUTPUT);
 pinMode(dirPin, OUTPUT);
 pinMode(Switch, INPUT_PULLUP);


   if(digitalRead(Switch) == HIGH){ 

  digitalWrite(dirPin, HIGH);

   for(int x = 0; x < 200; x++) {
       digitalWrite(stepPin,HIGH);
       delayMicroseconds(800);
       digitalWrite(stepPin,LOW);
       delayMicroseconds(800);}
 

 }
 
  else{

 digitalWrite(dirPin, LOW);
 
   for(int x = 0; x < 200; x++) {
       digitalWrite(stepPin,HIGH);
       delayMicroseconds(800);
       digitalWrite(stepPin,LOW);
       delayMicroseconds(800);}
  }
 
}



void loop() {



}

Welcome to the forums. Please read the sticky post at the top of the forum about how to post your code using code tags. It will help people help you.

If I understand your question correctly, you really need to detect when the switch changes state, not detect what the current switch state is. There is an example in the IDE (File -> Examples ->02.Digital -> StateChangeDetection) that should help.

Then, when you detect the switch goes from OFF->ON, you move your stepper however fall you want. When you detect ON->OFF, you go the other direction. If nothing has changed, you don't move at all.

thanks for your reply it looked into this and it's far too complicated for my knowledge at this time,

I was hoping for maybe an example if someone is clever enough to know how to do it from my sketch

if that made any sense,

I've not done anything this advance before if it even is and I just can't wrap my head around it

okay so I’ve got a little farther with this and managed to do what I need to do do but there 1 problem it’s only doing it once and then I have to reset the Arduino again, now that is most likely due to it being void setup and not loop but if I put the code in loop it doesn’t stop turning

this is the code in the void setup if I move it to void loop it continues to spin over and over and I don’t want that I need to turn left when the switch is on and right when the switch is off but to a set number of steps/degrees

const int stepPin = 4;   //pin to pulse for steps
const int dirPin = 3;    //pin to change step direction
const int Switch = 7;


void setup(){

  //set pins as outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(Switch, INPUT_PULLUP);


    if(digitalRead(Switch) == HIGH){ 

   digitalWrite(dirPin, HIGH);

    for(int x = 0; x < 200; x++) {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(800);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(800);}
  

  }
  
   else if(digitalRead(Switch) == LOW) {

  digitalWrite(dirPin, LOW);
  
    for(int x = 0; x < 200; x++) {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(800);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(800);}
   }
  
}


void loop() {



}

18minimus18:
I was hoping for maybe an example if someone is clever enough to know how to do it from my sketch

Have you studied carefully the example that was suggested in Reply #1 ?

...R

After some careful studying of stepper motor driver a4988 and more about how to write the code I have finally done what I set out to do, this has been bugging me all day and I've done it

thanks for the input its help a lot to understand more about Arduino coding

sorry im a bit late but here is an easy way to detect a change. I like doing things like this manually because there are times you want to only trigger after consecutive readings to be sure things are closed or avoid interferience.

bool newread;
bool oldread;
void loop() {
 if(digitalRead(somepin)==HIGH){newread = true}
 else{newread = false;}

if(newread!=oldread){oldread = newread;

//Do stuff

}delay(10);}

okay so i thought I had this was sorted but it seems I don’t, I’ve got the stepper motor to turn so many degrees with a switch but if I turn off and on the Arduino then it resets and turns to an angle that could break the setup, here’s the code, I really don’t know that much about coding and need an answer in a form of codes,

here’s the code I have that kinda works

// defines pins numbers
const int stepPin = 4;
const int dirPin  = 3;
// Button
const int switchPin = 7;


int currentAngle = 0;
int angle = 0;
float stepPerAngle = 1.8; // full step = 1.8
int   numstep;

void setup() {
  Serial.begin(9600);
  // Sets the two pins as Outputs
  pinMode(stepPin,OUTPUT);
  pinMode(dirPin,OUTPUT);

  pinMode(switchPin,OUTPUT);
  digitalWrite(switchPin,LOW);
  digitalWrite(dirPin,HIGH);

  pinMode(switchPin, INPUT_PULLUP);
 
}
void loop() {
 
    int n;
    if( digitalRead(switchPin) == HIGH){
        angle = 2000;
       
    }
    else if( digitalRead(switchPin) == LOW){
        angle = -8000;  //9000 = 90 degrees -- 20000 = 200 degrees
       
       
    }

    if( currentAngle != angle ){

        if( currentAngle < angle){
            digitalWrite(dirPin,HIGH);
             n = angle - currentAngle;
            numstep = n / stepPerAngle;
        }
        else if( currentAngle > angle){
            digitalWrite(dirPin,LOW);
            n = currentAngle - angle;
            if( angle == 0){
               n =currentAngle;
            }
            numstep = n / stepPerAngle;
        }

        for(int x = 0; x < numstep; x++) {
           digitalWrite(stepPin,HIGH);
           delayMicroseconds(1000);
           digitalWrite(stepPin,LOW);
           delayMicroseconds(1000);
        }

        currentAngle = angle;
    }

   delay(500);
 
}

but heres that code i think with work better if i can get it to stop after so meny steps and not go round and round

const int stepPin = 4;   //pin to pulse for steps
const int dirPin = 3;    //pin to change step direction
const int Switch = 7;


void setup(){

  //set pins as outputs
  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);
  pinMode(Switch, INPUT_PULLUP);


 
}



void loop() {

     if(digitalRead(Switch) == HIGH){ 

   digitalWrite(dirPin, HIGH);

    for(int x = 0; x < 2000; x++) {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(800);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(800);}
  

  }
  
   else if(digitalRead(Switch) == LOW) {

  digitalWrite(dirPin, LOW);
  
    for(int x = 0; x < 2000; x++) {
        digitalWrite(stepPin,HIGH);
        delayMicroseconds(800);
        digitalWrite(stepPin,LOW);
        delayMicroseconds(800);}
   }
  





}

When you power cycle your Arduino, it has no idea what current position the stepper motor is in. That is why most projects like this contain limit switches that trip when the motor is at the maximum forward or reverse position. You then add code to your movement code that checks these limit switches and stops when they are tripped rather than blindly continuing to run and break things.

To add to Reply #8. it is usual to move the motor in setup() until it triggers the limit switch and establishes the HOME or ZERO position.

...R

so ive added limit Switches and its fix the problems it work floorlessly apart from when the motor goes clockwise it vibrates the motor alot and i dont know why, it works going anti clockwise nice and smooth but clockwise its hurts to hear it move, here is my code if anyone can find somthing ive missed

#define step_pin 4  // Pin 4 connected to Steps pin
#define dir_pin 3   // Pin 3 connected to Direction pin


#define ToggleSwitch 12  // Pin 12 connected to Switch

#define Limit01 6  // Pin 6 connected to Limit switch out
#define Limit02 7  // Pin 7 connected to Limit switch out

int step_speed = 1000;  // Speed of Stepper motor (higher = slower)

void setup() {
   pinMode(dir_pin, OUTPUT);
   pinMode(step_pin, OUTPUT);
   
   pinMode(Limit01, INPUT_PULLUP);
   pinMode(Limit01, INPUT_PULLUP);
   
   pinMode(ToggleSwitch, INPUT_PULLUP);
   
}

void loop() {
   
    
  if (digitalRead(ToggleSwitch) == HIGH) {  //  If Switch is Turned on
    if (!digitalRead(Limit01)) {}  // check if limit switch is activated
    
      else {  //  if limit switch is not activated, move motor clockwise
      
        digitalWrite(dir_pin, LOW);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delayMicroseconds(step_speed);
        digitalWrite(step_pin, LOW);
        delayMicroseconds(step_speed);
    }      
  }
  
    if (digitalRead(ToggleSwitch) == LOW) {  // If Switch is Turned off
    
    if (!digitalRead(Limit02)) {}  // check if limit switch is activated
    
      else {  //  if limit switch is not activated, move motor counter clockwise
      
        digitalWrite(dir_pin, HIGH);  // (HIGH = anti-clockwise / LOW = clockwise)
        digitalWrite(step_pin, HIGH);
        delayMicroseconds(step_speed);
        digitalWrite(step_pin, LOW);
        delayMicroseconds(step_speed);
    }      
  }
}

I don't see anything different between the code for the two directions.

What happens if you halve or quarter the motor speed - i.e. use values of 2000 or 4000?

Incidentally it seems a bit mad to use motorSpeed as the name for a variable which is really the length of an interval. But the compiler is too ignorant to care :slight_smile:

...R

You appear to have a cut & paste error in your code

   pinMode(Limit01, INPUT_PULLUP);
   pinMode(Limit01, INPUT_PULLUP);

blh64:
You appear to have a cut & paste error in your code

Full marks.

I actually looked for the absence of pullups and did not spot that.

...R

the Copy and paste was a mistake the code I uploaded is fixed and I’ve double checked it, I’ve got a video of it uploading on youtube up don’t know if I’m allowed to post links at all

regards

Andrew H

18minimus18:
don’t know if I’m allowed to post links at all

Of course you are. There is a link icon above the edit window.

he Copy and paste was a mistake the code I uploaded is fixed

Please post the “fixed” code in your next Reply so we can compare it with the earlier version.

…R