Hi all, I rarely post on forums so hopefully I'm following all of the rules. I could use some advice on a project. I've read several forum posts and blogs trying to implement what is seemingly simple, and have tried many different methods, but I think that my programming chops just aren't up to par. I'm not looking for a hand hold, just a point in the right direction.
Essentially, I'm looking to control the speed of a NEMA 17 stepper motor, with the TB6600 controller with a potentiometer. Then also toggle On/Off, Direction, and Enable with momentary switches acting as toggles. Ideally, I want the motor to turn indefinitely until there is user input.
The different implementations of using a momentary switch as a toggle, either by using millis or by using something like state=!state don't seem to work, because when they are inserted into the main loop, it's getting thrown off by the indefinite loop that is turning the motor. If I insert code into the loop that is turning the motor, it goes by so fast that it's not catching my button presses. Occasionally I can get one of the buttons to work, until the iteration repeats and resets.
Do I need to be using switch cases and programming every different possibility of button combinations then breaking out of each loop? It seems like a waste of code to me. Should I be looking at libraries instead? To me, logically, the DIR and ENA states should be variables, but I have no way of changing them while the motor iterates. Am I asking too much from an Arduino UNO? Anyways, you guys are the experts, just looking for ideas on the most efficient way to implement this. Thanks for any ideas that you have!
Here is my base code which I've been modifying and trying different things (please don't burn me). You won't see the button toggle code in here, simply because every method I've tried hasn't worked. The speed controller works fine.
int PUL=7; //define Pulse pin
int DIR=6; //define Direction pin
int ENA=5; //define Enable Pin
int customDelay,customDelayMapped; // Defines speed variables
int buttonOnOff=8; //On Off toggle
int buttonDir=4; //direction toggle
int buttonEna=3; //enable toggle
void setup() {
pinMode (PUL, OUTPUT);
pinMode (DIR, OUTPUT);
pinMode (ENA, OUTPUT);
pinMode (buttonOnOff, INPUT_PULLUP);
pinMode (buttonDir, INPUT_PULLUP);
pinMode (buttonEna, INPUT_PULLUP);
digitalWrite(ENA,HIGH); //Disable ENA on startup
}
//variable for speed control via potentiometer
int speedUp() {
int customDelay = analogRead(A0); // Reads the potentiometer
int newCustom = map(customDelay, 0, 1023, 10 ,1000);
return newCustom;
}
void loop() {
for (int i=0; i<1600; i++) //sets motor steps
{
digitalWrite(ENA,LOW);
digitalWrite(DIR,LOW);
customDelayMapped = speedUp();
digitalWrite(PUL,HIGH);
delayMicroseconds(customDelayMapped);
digitalWrite(PUL,LOW);
delayMicroseconds(customDelayMapped);
}
}