system
November 29, 2010, 6:54pm
1
I am trying to control a stepper motor and I am using an EasyDriver board. How can I start and stop the stepper motor using a button as an input?
My stepper motor is running fine if I tell it to turn i.e 4000 steps but I am failing to let it run until it hits a sensor or a button.
I have attached my code. The idea is to run the stepper when the button connected to pin 7 is pressed but nothing is happening.
Please advice.
int dirpin = 2;
int steppin = 3;
int button = 7;
void setup() {
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(steppin, INPUT);
}
void loop()
{
digitalRead(button);
if (button == HIGH)
{
int i;
digitalWrite(dirpin, HIGH);
for (i = 0; i<1; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(200);
}
}
}
system
November 29, 2010, 7:06pm
2
digitalRead(button);
digitalRead returns a value (the state of the switch). You throw that value away.
int button = 7;
if (button == HIGH)
Should be fairly obvious that button is not equal to HIGH.
for (i = 0; i<1; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(200);
}
What's the point in having a for loop that executes exactly once?
system
November 29, 2010, 7:48pm
3
Ah, ok I got it.
Its working now, thanks for the reply.
How do I post my code with a grey background?
int dirpin = 2;
int steppin = 3;
int button = 7;
int val = 0;
void setup() {
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(button, INPUT);
}
void loop()
{
val = digitalRead(button);
int i;
if (val == HIGH)
{
digitalWrite(dirpin, HIGH);
for (i = 0; i<1; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(200);
}
}
}
system
November 29, 2010, 8:00pm
4
Found it
int dirpin = 2;
int steppin = 3;
int button = 7;
int val = 0;
void setup() {
pinMode(dirpin, OUTPUT);
pinMode(steppin, OUTPUT);
pinMode(button, INPUT);
}
void loop()
{
val = digitalRead(button);
if (val == HIGH)
{
int i;
digitalWrite(dirpin, HIGH);
for (i = 0; i<1; i++)
{
digitalWrite(steppin, LOW);
digitalWrite(steppin, HIGH);
delayMicroseconds(200);
}
}
}