I am running a stepper motor at different speeds using the following code :
#include <AccelStepper.h>
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
// set pin numbers:
const int buttonPin6 = 11; // the number of the pushbutton pin
const int buttonPin5 = 10; // the number of the pushbutton pin
const int buttonPin4 = 9; // the number of the pushbutton pin
const int buttonPin3 = 8; // the number of the pushbutton pin
const int buttonPin2 = 7; // the number of the pushbutton pin
const int buttonPin1 = 6; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int c1,c2,c3,c4,c5,c6=0;
// variables will change:
int buttonState1,buttonState2,buttonState3,buttonState4,buttonState5,buttonState6 = 0; // variable for reading the pushbutton status
void setup() {
stepper.setMaxSpeed(360);//max speed at 3 rpm
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
buttonState6 = digitalRead(buttonPin6);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
stepper.setSpeed(0.333333); // 20 mm/hr
c1=1;}
if (buttonState2 == HIGH) {
stepper.setSpeed(1); // 60 mm/hr
c2=1;}
if (buttonState3 == HIGH) {
stepper.setSpeed(2); // 120 mm/hr
c3=1;}
if (buttonState4 == HIGH) {
stepper.setSpeed(4); // 240 mm/hr
c4=1;}
if (buttonState5 == HIGH) {
stepper.setSpeed(8); // 480 mm/hr
c5=1;}
if (buttonState6 == HIGH) {
stepper.setSpeed(16); // 960 mm/hr
c6=1;}
if(c1+c2+c3+c4+c5+c6==1){
stepper.runSpeed();
}
c1=0;c2=0;c3=0;c4=0;c5=0;c6=0;
}
This works perfectly and I am using it regularly.
Now i want to modify the code such that when pin 12 goes high, then only the above code should work or else motor should stop.
I have done the following code, but the motor doesnt run.
#include <AccelStepper.h>
AccelStepper stepper; // Defaults to AccelStepper::FULL4WIRE (4 pins) on 2, 3, 4, 5
// set pin numbers:
const int buttonPin6 = 11; // the number of the pushbutton pin
const int buttonPin5 = 10; // the number of the pushbutton pin
const int buttonPin4 = 9; // the number of the pushbutton pin
const int buttonPin3 = 8; // the number of the pushbutton pin
const int buttonPin2 = 7; // the number of the pushbutton pin
const int buttonPin1 = 6; // the number of the pushbutton pin
const int ePin = 12; // the enable pin to run further code
int c1,c2,c3,c4,c5,c6=0;
// variables will change:
int buttonState1,buttonState2,buttonState3,buttonState4,buttonState5,buttonState6,ePinstate = 0; // variable for reading the pushbutton status
void setup() {
stepper.setMaxSpeed(360);//max speed at 3 rpm
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(buttonPin3, INPUT);
pinMode(buttonPin4, INPUT);
pinMode(buttonPin5, INPUT);
pinMode(buttonPin6, INPUT);
pinMode(ePin, INPUT);
}
void loop() {
ePinstate = digitalRead(ePin);
if (ePin == HIGH)
{
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
buttonState2 = digitalRead(buttonPin2);
buttonState3 = digitalRead(buttonPin3);
buttonState4 = digitalRead(buttonPin4);
buttonState5 = digitalRead(buttonPin5);
buttonState6 = digitalRead(buttonPin6);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
stepper.setSpeed(0.333333); // 20 mm/hr
c1=1;}
if (buttonState2 == HIGH) {
stepper.setSpeed(1); // 60 mm/hr
c2=1;}
if (buttonState3 == HIGH) {
stepper.setSpeed(2); // 120 mm/hr
c3=1;}
if (buttonState4 == HIGH) {
stepper.setSpeed(4); // 240 mm/hr
c4=1;}
if (buttonState5 == HIGH) {
stepper.setSpeed(8); // 480 mm/hr
c5=1;}
if (buttonState6 == HIGH) {
stepper.setSpeed(120); // 960 mm/hr
c6=1;}
if(c1+c2+c3+c4+c5+c6==1){
stepper.runSpeed();
}
c1=0;c2=0;c3=0;c4=0;c5=0;c6=0;
}
else
{
stepper.setSpeed(0);
stepper.runSpeed();
}
}
I am running a chart drive. The speeds moves the chart by specified distance.
Can anyone rectify my mistake and tell me a suitable solution. Thank You !!!