hi. so i have this code i'm working on for an automated metal cutting saw and i'm trying to get it finished up and ran into a small issue and don't know what i'm doing wrong.
in short the code waits for me to press a button to start going home. Once it arrives at home it will start the saw and stepper motor which will bounce between two limit switches. I installed a 10K pot to allow me to change the speed of the stepper motor, but it's giving me problems. I included it in the loop and it works to a degree.... like when i first turn it on, wherever the pot is positioned, that's what it sets the speed as, but it does not allow me to change it on the fly unless i reset the arduino and then it changes the speed to the position of the pot. can someone please tell me what i'm doing wrong..
and fyi. the stopStep() at the bottom isn't working at the moment because of the while(digitalRead(next limit switch)) is blocking me from activating the button, but that will be changed an interrupt to pause the saw and stepper in the event there's a problem, which i'm about to do, but right now i'm just concerned about the Pot and speed control
thanks
#include <Stepper.h>
Stepper stepper1(200, 6, 5);
const int stepPin = 5; // stepper1 step pin
const int dirPin = 6; // stepper1 direction pin
const int enPin = 4; // stepper1 enable pin
const int relay =7; // relay enable pin
const byte front_Switch = 8; // frontStep switch
const byte back_Switch = 9; // backStep switch
const byte stop_Switch = 1; // cutting switch
byte switch1State; // cutting switch state
byte switch2State; // frontStep switch state
byte switch3State; // stopstep state
int stepSpeed;
void setup() {
Serial.begin(115200);
pinMode(enPin, OUTPUT); // stepper1 enable output
pinMode(relay, OUTPUT); // stepper2 enable output
pinMode( stop_Switch, INPUT_PULLUP); // stop switch mode
pinMode( front_Switch, INPUT_PULLUP); // frontStep switch mode
pinMode( back_Switch, INPUT_PULLUP); // backStep switch mode
digitalWrite(enPin, HIGH); // stepper1 enable = high
digitalWrite(relay, LOW); // relay enable = low, off
// Serial.println(" ");
// Serial.println(" Press Stop To Begin");
// Serial.println(" ");
// while (digitalRead(stop_Switch)){ }
// Serial.println(" Going Home");
// Serial.println(" ");
// digitalWrite(enPin, HIGH);
while (digitalRead(front_Switch)) { //CCW rotation
stepper1.step(-1);
delayMicroseconds(400);
}
}
void loop() {
int sensorReading= analogRead(A0);
int stepSpeed = map(sensorReading, 5, 1023, 100, 750);
if (stepSpeed > 0) {
stepper1.setSpeed(stepSpeed);
}
checkButtons(); // read buttons status
frontStep(); // step forward
backStep(); // step backward
stopStep(); // cutting stop
}
void checkButtons() {
switch1State = digitalRead(front_Switch); // reading frontStep button
switch2State = digitalRead(back_Switch); //reading backStep button
switch3State = digitalRead(stop_Switch); // reading stop button
}
void frontStep(){ // front cutting
if (switch1State == LOW){ // if front_Switch button activated
while(digitalRead(back_Switch)) {
digitalWrite(enPin, HIGH);
digitalWrite(relay, HIGH);
stepper1.step(1);
delayMicroseconds(stepSpeed);
}
}
}
void backStep(){ //CCW rotation step counting
if (switch2State==LOW) { // if back_Switch button activated
while(digitalRead(front_Switch)) {
digitalWrite(enPin, HIGH);
digitalWrite(relay, HIGH);
stepper1.step(-1);
delayMicroseconds(stepSpeed);
}
}
}
void stopStep() { // stop step and cutting
if (switch3State == LOW){ // if stop switch activated
digitalWrite(relay, LOW);
digitalWrite(enPin, LOW);
}
}
[\code]