I've got a code where I have a motor running back and forth and buttons connected to a scanner, when I press the buttons it causes the motor to stop and over rides it. I would like them to run parallel to each other so the codes don't interrupt each other.
Here is my code
#include <AccelStepper.h>
// Define some steppers and the pins they will use
AccelStepper stepper1(AccelStepper::FULL2WIRE, 2, 3);
const int buttonPin = 4;
const int button2Pin = 14;
const int pulseopto1 = 9;
const int startScan = 11;
int buttonState = 0;
long previousMillis = 0;
long interval = 5;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(pulseopto1, OUTPUT);
pinMode(startScan, OUTPUT);
stepper1.setMaxSpeed(40000.0);
stepper1.setAcceleration(100000.0);
stepper1.moveTo(25000);
}
void loop()
{
buttonState = digitalRead(buttonPin);
if (buttonState == LOW)
{
digitalWrite(startScan, HIGH);
}
else (buttonState == HIGH);
{
digitalWrite(startScan, LOW);
}
{
buttonState = digitalRead(button2Pin);
if (buttonState == LOW)
{
// turn LED on:
digitalWrite(pulseopto1, HIGH);
delay(5);
digitalWrite(pulseopto1, LOW);
delay(5);
}
else
{
// turn LED off:
digitalWrite(pulseopto1, LOW);
}
}
// Change direction at the limits
if (stepper1.distanceToGo() == 0)
{
stepper1.moveTo(-stepper1.currentPosition());
}
stepper1.run();
}
delay
? Could it be that yourdelay
s are making the motor stop? $\endgroup$ – Shahbaz Nov 6 '13 at 14:21pulseopto1
and gotes toWAIT_STATE_2
for example again setting start time to current time. Later again in the loop function, you check if enough time has passed to enter the next state (END_STATE) $\endgroup$ – Shahbaz Nov 6 '13 at 15:23