Working on automating a series of actions via an arduino. Not the best at coding and would like to understand what the best way would be to design the sketch to control it.
Inputs
Start Button (MC Switch)
Reverse Button (Toggle Switch
Emergency Stop (MC Switch)
Stepper Speed Control (POT)
Outputs
Relay 1 (Clamp)
Relay 2 (Start applicator)
Stepper Motor
Action
Start button press
Clamp will Actuate
Applicator start a specified time after clamp actuates
Stepper will start a specified time after applicator starts will move a specified a number of steps at a specified speed
Applicator will stop before stepper is finished with its routine.
Clamp will disengage after stepper is finished with a delay.
I'm not worried about having the applicator stop and the clamp unclamp based on the time it takes the stepper to finish its routine (as its time to complete will change with pot speed). I'm assuming that the clamp and applicator could be run based on time from the start button press. Stepper would be a delay and then speed and steps. Start and end positions do not matter.
Would also like to be able to reverse the motor via a switch which could be standard toggle switch. Also an E stop would be nice to stop the process in case something goes wrong.
For me I can understand how to individually control some of the functions in a sketch but the momentary start and E stop are throwing me for a loop. I believe we need to watch the state of the switch during the loop but beyond that I'm not sure what to do. Any help would be appreciated.
You could first try to make the stepper work with a library.
Do you know what the hardware is ?
You could run the loop 50 times or 100 times a second and read the buttons.
You could also use a library with debounce for the buttons.
You already wrote the answer in your question: you need a state machine. That is a variable that identifies the current state. According to the current state the buttons result in certain actions.
To measure time, you could use the millis() function.
If you have not done something like this before, do it step by step, and upload your sketch so we can see how you are doing.
I'm using a ebay stepper drive L298H board. I can run the stepper alone without too much difficulty. The state changing based on millis is what I'm having difficulty with. I have started with the standard debounce tutorial and substitute the led setup for the relays. Assuming I can use a "If and"statement to set the relay states based on time since the button push. Really not sure of where to start with the code though to get the millis control.