Would it be possible for someone to help me with a motor sequence loop that I need?
I have two stepper motors, I would like the following events sequence.
1: If SetupModeState is (LOW) than no motors turn
2: If SetupModeState is (HIGH) than motor sequence loop runs
motor sequence is;
1: motor 1 turns for 3200 steps
2: motor 1 stops
3: slight pause
4: motor 2 turns for 3200 steps
5: motor 2 stops
6: slight pause
7: repeat 1-6 until a variable reaches 0
if (SetupModeState == LOW) { // If the unit is in SETUP mode (HIGH), the quantity and length values can be changed by the user display panel
// If the unit is in RUN mode (LOW), the quantity and length buttons are disabled and the motor loop is enabled
int i;
digitalWrite(DirectionPullThrough, LOW); // Set the Direction of Pull-through motor (LOW = Anti-clockwise/HIGH = Clockwise)
for (i = 0; i<3200; i++) // Turn the stepper the value of the ButtonPushLength * 2 ( in this case 1600 steps)
digitalWrite(StepPullThrough, HIGH); // Step set to HIGH
digitalWrite(StepPullThrough, LOW); // Step set to LOW
delayMicroseconds(1000);
Right now this is for just the first motor, if I run the code above in the voidsetup, it runs and operates fine, but if I put it in the loop it does not run at all.
Hi Robin2, I have added more code from my previous snippet, I can only demonstrate one of the motors currently as that is all I have to hand, apologies. So would rather conecentrate on getting the one motor to function then introduce the other.
The Updated code now moves the motor in the voidLoop section, and pauses for a period before continuing.
The issue now is getting rid of the delay, I have read various threads for using millis() function but I'm afraid my brains frazzled as I cannot get my head around the implementation, as I understand it where the;
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
Is placed is where the counter starts, and is referenced to each time through the loop until the counter is complete?
However I have no idea where you set the counter in the code.........
I would like the pause to be around 4 seconds, but obviously for it not to be a delay as that would interrupt everything else in my other code.
Sorry for this but I really have tried looking at examples, to no avail.
RunModeState = digitalRead(RunModeSwitch); // Read the Setup Mode Switch input Pin 38
{
if (RunModeState == HIGH){ // If the unit is in SETUP mode (HIGH), the quantity and length values can be changed by the user display panel
// If the unit is in RUN mode (LOW), the quantity and length buttons are disabled and the motor loop is enabled
int i;
digitalWrite(DirectionPullThrough, LOW); // Set the Direction of Pull-through motor (LOW = Anti-clockwise/HIGH = Clockwise)
for (i = 0; i<3200; i++) // Turn the stepper the value of the ButtonPushLength * 2 ( in this case 1600 steps)
{
digitalWrite(SleepPullThrough, HIGH); // ENABLE the PullThroughMotor
digitalWrite(StepPullThrough, HIGH); // Step set to HIGH
digitalWrite(StepPullThrough, LOW); // Step set to LOW
delayMicroseconds(1000); // Motor Speed (fast = < slow = >)
unsigned long currentMillis = millis();
if (currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
} else {
digitalWrite(SleepPullThrough, LOW); // Set the PullThroughMotor to SLEEP mode
}
lastRunModeState = RunModeState;
Okay forgive my implemenation of it all as this is my first ever coding project.
(I'm an electronic technician working on a college project).
The coding is actually an extra task I have set myself whilst designing an electronic circuit.
The project is an automatic wire cutter, it consists of a user display board comprising of;
2 seperate 3 x 7 segment displays one set to represent the quantity and another the length values input by the user. The values can range from 0-999.
The count is controlled by SPST push-buttons to SN74LS192N logic IC's> SN74LS47N drivers to the common anode 7-segments.
The arduino is detecting the state change of each pushbutton press, and upon each triggered edge changes the counter IC accordingly.
The idea is that there is 2 modes, a SETUP mode and a RUN mode.
In SETUP: the pushbuttons can be used to change the quantity values and the length values, and the motors are idle.
In RUN: the pushbuttons are idle and no input is recorded, but the motors use the saved values of the quantity and length to perform their given functions.
MOTOR 1 = length function, this is the pull through motor.
So the idea for this is that the motor turns a given number of steps in accordance to how much length of wire is required.
MOTOR 2 = Quantity function, this is the cutting motor.
The idea for this is that the motor turns on a linear actuator to move a cutting press up/down the number of times to perform the cut, specified by the quantity until the value reaches 0.
I now have everything working correctly, however I know it could be simplified and condensed greatly. But as its my first project I dont mind it in its current state.
What I would like is for someone if possible to help me with my RunModeState and SetupModeState as if you look at the last section of code where the //MOTOR SEQUENCE takes place, the pushbuttons state change can still be detected when they should be idle.
I think I can fix it by moving the //MOTOR SEQUENCE section up the code order but i'm unsure, its become a nest of instructions now, need fresh eyes.
You have the entire program in your loop() function. Have a look at how the code is split into short single-purpose functions in Planning and Implementing a Program Breaking the code into several short single-purpose functions makes it much easier to develop, test, debug and maintain.
It will also make it much easier to manage your different states. Your loop() might reduce to something like