Hi people! I am testing push buttons to control stepper motor! I use this code below! The motor works with this code. However, when I press the button, I don't manage to make the motor goes through a specific number of steps and stop! Instead of this, the motor stops when I stop to press the button! How do I solve this?
const int stepPin = 7;
const int dirPin = 8;
const int numerodepassos =72;
const int Mode0 = 2;
const int Mode1 = 4;
const int Mode2 = 3;
const int nRESET = 11;
const int button0 = 5;
const int button1= 6;
boolean dir;
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode (nRESET, OUTPUT);
pinMode(Mode0, OUTPUT);
pinMode(Mode1, OUTPUT);
pinMode(Mode2, OUTPUT);
pinMode (button0, INPUT_PULLUP);
pinMode (button1, INPUT_PULLUP);
digitalWrite(Mode0,LOW);
digitalWrite(Mode1, LOW);
digitalWrite(Mode2, LOW);
Serial.begin(9600); // put your setup code here, to run once:
}
void loop() {
if (digitalRead(button0) == HIGH )
{
digitalWrite(dirPin,HIGH);
digitalWrite (nRESET,HIGH);
for(int i=1;i <=numerodepassos; i++){
Serial.println(button0);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
}
else
{
if (digitalRead(button1) == HIGH)
{
digitalWrite(dirPin,LOW);
digitalWrite (nRESET,HIGH);
for(int i=1;i <=numerodepassos; i++){
Serial.println(button1);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1000);
digitalWrite(stepPin, LOW);
delayMicroseconds(1000);
}
}
}
}