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. :
Here is my code, but it is not working.
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
Code wasnt working cause battery was too low 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 part of studying.
Thank you all at the moment. Hopefully ill get it to work now
Now ill start adding more functions like speed and "Home"
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"
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.