Stepper motor camera slider

My question is simple actually. I have 2 endswitches (when pressed=LOW), stepper motor and stepper motor driver a4988. When endswitch is pressed then the motor starts rotating in 1way and if endswitch2 is pressed then its rotating in other direction. How could i achieve that?

Hope my englsih is not so bad that you cant understand. ::slight_smile:

Here is my code, but it is not working. :angry:

const int lopuNupp1 = 8; //endswitch 1
const int sammPin = 3; //stepPin
const int suundPin = 4; //directionPin
const int lopuNupp2 = 9; //endswitch2
int sammK = 500; //stepping speed

void setup() {

pinMode(lopuNupp1, INPUT);
pinMode(lopuNupp2, INPUT);
pinMode(sammPin, OUTPUT);
pinMode(suundPin, OUTPUT);
}

void loop() {

if(digitalRead(lopuNupp1) == LOW && digitalRead(lopuNupp2) == HIGH){
digitalWrite(suundPin, LOW);
digitalWrite(sammPin, HIGH);
delayMicroseconds(sammK);
digitalWrite(sammPin, LOW);
delayMicroseconds(sammK);
}

if(digitalRead(lopuNupp2)==HIGH && digitalRead(lopuNupp2) == LOW){
digitalWrite(suundPin, HIGH);
digitalWrite(sammPin, HIGH);
delayMicroseconds(sammK);
digitalWrite(sammPin, LOW);
delayMicroseconds(sammK);
}
}

Do you have pullup resistors on the limit switch pins ?
How are the inputs wired ?

https://www.ebay.com/itm/3pcs-Mechanical-Endstop-Limit-Switch-With-Cable-for-CNC-3D-Printer-RAMPS-1-4-NEW-/401196858554?hash=item5d69323cba%3Ag%3ADSAAAOSw44BYZ1WW

green wire connected to pin 8 (first switch)
pin 9 (second switch)

What doesn't work? What happens when you run it?

Why are you not using the Stepper library? So much easier to just call step(1) or step(-1).

if(digitalRead(lopuNupp2)==HIGH && digitalRead(lopuNupp2) == LOW){

If the state of the pin is HIGH AND the state is LOW, do something. What are the odds that the state of the pin will be both HIGH and LOW?

This code

void loop() {

  if(digitalRead(lopuNupp1) == LOW && digitalRead(lopuNupp2) == HIGH){
    digitalWrite(suundPin, LOW);
    digitalWrite(sammPin, HIGH);
    delayMicroseconds(sammK);
    digitalWrite(sammPin, LOW);
    delayMicroseconds(sammK);
    }

  if(digitalRead(lopuNupp2)==HIGH && digitalRead(lopuNupp2) == LOW){
    digitalWrite(suundPin, HIGH);
    digitalWrite(sammPin, HIGH);
    delayMicroseconds(sammK);
    digitalWrite(sammPin, LOW);
    delayMicroseconds(sammK);
    }
 }

looks as if it would cause the motors to step only when a switch is pressed. (And there is a big error in the second IF)

To make the system move back and forth you need a variable, let's call it direction and let's start it with the value 'F'

Then you have a function to move the motor forwards - if (direction == 'F')

When the motor gets to the end and touches the end-stop-switch the code changes the value of direction to 'R'

And I guess you can figure out the rest your self. The code in loop() could be as simple as

void loop() {
   checkEndStopSwitches();
   if (direction == 'F') {
      stepForward();
   }
   else {
     stepReverse();
  }
}

...R
Stepper Motor Basics
Simple Stepper Code

Code wasnt working cause battery was too low :slight_smile: and yeah that stupid mistake in if().

Its now working, but not like i need . Motor is steping when switches are pressed only (as you said). It is my
first code writing so nothing weird :stuck_out_tongue: part of studying.

Thank you all at the moment. Hopefully ill get it to work now :slight_smile:

Thank you all once again! :wink:

Now ill start adding more functions like speed and "Home" :smiley:

New code is here:

  const int lopuNupp1 = 8;   //endswitch 1  
  const int sammPin = 11;    //stepPin
  const int suundPin = 12;   //directionPin
  const int lopuNupp2 = 9;   //endswitch2
  int sammK = 500;           //stepping speed
  
 
  
  void setup() {
  
   pinMode(lopuNupp1, INPUT);    //Määran sisendid ja väjundid
   pinMode(lopuNupp2, INPUT); 
   pinMode(sammPin, OUTPUT);
   pinMode(suundPin, OUTPUT);
  }

  void loop() {

   

   digitalWrite(suundPin, HIGH);    // suuna määramine. Kui nupu olek loetud, hakkab alus sõitma.  
  while (digitalRead(lopuNupp1) == HIGH) // mootor liigutab alust nii kaua kuni vasakut lülitit vajutatakse
  {
   mootorLiigu(); 
  }
  
  
   digitalWrite(suundPin, LOW);  // suuna määramine. Kui nupu olek loetud, hakkab kaamera alus sõitma.
  while (digitalRead(lopuNupp2) == HIGH) // mootor liigutab alust nii kaua kuni paremat lülitit vajutatakse
  {
  mootorLiigu(); 
  }
 }


  void mootorLiigu()                        //Koodi blokk, mis hakkab jooksma, kui nupu olek loetud ja pöörlemise suund määratud
  {
   digitalWrite(sammPin, HIGH);
   delayMicroseconds(sammK);
   digitalWrite(sammPin, LOW);
   delayMicroseconds(sammK); 
  }

sepapoiss3:
Now ill start adding more functions like speed and "Home" :smiley:

New code is here:

Don't start extending the program until you get the basic stuff working.

Your program does not reflect what I suggested about using the end switches to change the value of a variable and using the value of the variable to control the motor. In effect the variable is used to remember which switch was pushed.

...R

F is value (1 or 0) yeah?

How i can change the value(F) with switches.

Program is not wrong so far?

i dont know what to do anymore :slight_smile:

const int lopuNupp1 = 8;       //Deklareerin pinid
  const int sammPin = 11;    
  const int suundPin = 12;   
  const int lopuNupp2 = 9;   
  const int sammKiirus = 30;
  int directions = 0;
             
  
 
  
  void setup() {
  
   pinMode(lopuNupp1, INPUT);    //Määran sisendid ja väjundid
   pinMode(lopuNupp2, INPUT); 
   pinMode(sammPin, OUTPUT);
   pinMode(suundPin, OUTPUT);
  }

  void loop() {
    checkEndStopSwitches();
    if(directions == 'F'){
      digitalWrite(suundPin, HIGH);
      digitalWrite(sammPin, HIGH);
   delay(sammKiirus);
   digitalWrite(sammPin, LOW);
   delay(sammKiirus);
    }
   else{
    digitalWrite(suundPin, LOW);
      digitalWrite(sammPin, HIGH);
   delay(sammKiirus);
   digitalWrite(sammPin, LOW);
   delay(sammKiirus);
     
   }
  }
  void checkEndStopSwitches(){
    
  }

sepapoiss3:
F is value (1 or 0) yeah?

NO.

In my code in Reply #5 'F' and 'R' are the alternate values in the variable direction.

To use the end stop to change the value of direction you could do something like this

void checkEndStopSwitches() {
   byte endStopAState = digitalRead(endStopAPin);
   byte endStopBState = digitalRead(endStopBPin);

   if (endStopBState == LOW) {
     direction = 'F';
   }
   if (endStopAState == LOW) {
     direction = 'R';
   }
}

...R