Needing a little help with the coding part of this project.
I am trying to program discrete speeds to a multi-position switch.
I will also be implementing two hall sensors to regulate where the end stop is and switch directions of the motor.
I currently have code that will control rotation direction using the hall sensors, code that works with the switch, and code that drives the motor. I am having trouble putting it all together. I am working on just setting the discrete speeds to the different switch positions.
Can someone point me in the right direction of where I am going wrong? I am wondering if it is an order of operations issue.
I have been using the accelstepper missing manual but still unsure trying to put it together. Largely confused on using .run() or .runSpeed
As you can see, I have the hall sensor part currently turned off.
With the code as is, serial will report back the switch position along with the speed. However, the motor is not turning. Also, when in switch position 1, the speed reports as 0. If I turn to position 2, then back to position 1, it will report the speed increasing by 1 each time it prints.
#include <AccelStepper.h>
//1600 steps/rev
// // motor Connections
const int dirPin = 2;
const int stepPin = 5;
const int enPin = 8;
AccelStepper stepper(AccelStepper::DRIVER, stepPin, dirPin);
//switch connections
int motoroff = 23;
int motoron4 = 25;
int motoron5 = 27;
int motoron6 = 29;
int motoron7 = 31;
//hall effect sensors
//const byte sensorPin_1 = 18;
//const byte sensorPin_2 = 19;
// //variables
// bool setdir = LOW; //set dirPinection
//interupt handlers
// void limit_1() {
// //reverse motor
// setdir = !setdir;
// }
// void limit_2() {
// //reverse motor
// setdir = !setdir;
// }
void setup() {
//switch set-up
pinMode(motoroff, INPUT_PULLUP);
pinMode(motoron4, INPUT_PULLUP);
pinMode(motoron5, INPUT_PULLUP);
pinMode(motoron6, INPUT_PULLUP);
pinMode(motoron7, INPUT_PULLUP);
//stepper set-up
stepper.setEnablePin(enPin);
stepper.setMaxSpeed(1600);
stepper.setAcceleration(100);
stepper.disableOutputs();
// //hall effect sensor set-up
// pinMode(sensorPin_1, INPUT); //declares hall sensor as input
// pinMode(sensorPin_2, INPUT); //""
// attachInterrupt(digitalPinToInterrupt(sensorPin_1), limit_1, FALLING); //do not change it to 'CHANGE'
// attachInterrupt(digitalPinToInterrupt(sensorPin_2), limit_2, FALLING);
Serial.begin(9600);
Serial.println("testing Accelstepper");
}
//set speed to 0 in switch postion 1
void switch1() {
Serial.println("Switch in Position 1");
stepper.disableOutputs();
stepper.stop();
}
//set speed in switch postion 2
void switch2() {
Serial.println("Switch in Position 2");
stepper.enableOutputs();
stepper.setSpeed(2);
stepper.runSpeed();
}
//set speed in switch postion 3
void switch3() {
Serial.println("Switch in Position 3");
stepper.enableOutputs();
stepper.setSpeed(4);
stepper.runSpeed();
}
//set speed in switch postion 4
void switch4() {
Serial.println("Switch in Position 4");
stepper.enableOutputs();
stepper.setSpeed(6);
stepper.runSpeed();
}
//set speed in switch postion 5
void switch5() {
Serial.println("Switch in Position 5");
stepper.enableOutputs();
stepper.setSpeed(8);
stepper.runSpeed();
}
void switchloop() {
//switch loop
int switch_position_1_status = digitalRead(motoroff);
int switch_position_2_status = digitalRead(motoron4);
int switch_position_3_status = digitalRead(motoron5);
int switch_position_4_status = digitalRead(motoron6);
int switch_position_5_status = digitalRead(motoron7);
if (switch_position_1_status == LOW) {
switch1();
} else if (switch_position_2_status == LOW) {
switch2();
} else if (switch_position_3_status == LOW) {
switch3();
} else if (switch_position_4_status == LOW) {
switch4();
} else if (switch_position_5_status == LOW) {
switch5();
}
}
void loop()
//main code here, run repeatedly
{
switchloop(); //checks switch position
float mSpeed = stepper.speed();
Serial.println(mSpeed);
//endstop(); //end stop function
}