How can I have a conditional statement that performs two tasks? first one and then the other in a loop - c

So I am trying to code a program in arduino that does a task for 2 minutes and then another task for 5 mins. first one and then the other in a loop until a different condition is met.
Ive been trying to do this with if statements and while loops but im getting lost in the time part i think
//Pneumatic feed system
double minute = 1000*60;
double vibTime = 2 * minute; //2 mins
//double vibTime = 5000;
double execTime = millis();
double waitTime = 5 * minute; //time waits between vibrating
//double waitTime = 10000;
void DoPneuFeed() {
//Serial.print("Delta:");
//Serial.println(millis()-execTime);
if(Temp_Data[T_9] < 500)
{
if(execTime + vibTime < millis())
{
//turn on vibration for 2 mins
execTime = millis();
Serial.println("VIBRATING");
}
if(execTime + waitTime < millis())
{
//turn off vibration for 5 mins
execTime = millis();
Serial.println("WAITING");
}
}
if(Temp_Data[T_9] > 500)
{
relayOff(3);
relayOff(7);
}
}
void PneuFeed()
{
if(execTime + vibTime > millis())
{
//air pressure open
relayOn(3);
//vibrator on
relayOn(7);
}
else if(execTime + waitTime > millis())
{
relayOff(3);
relayOff(7);
}
}
I want to turn on the vibration mode for 2 mins and then turn it off for 5 mins. as long as the Temp_Data(T_9) < 500. if it is greater than 500 it should stay off.

Without going into the specifics of your code (since I cannot build it to test.) I only have one quick suggestion:
Instead of using a series of if-then-else (or variations of it) consider using a state machine. Implementations often include use of a switch() inside a while(expression){...} loop. The following is a very simple example of how you might be able to do the steps you need in this construct:
(Note, this is a mix of C and pseudo code for illustration. It is close to compilable , but contains a few undefined items.)
typedef enum {
START,
SET_VIB,
CHECK_TEMP,
GET_TIME,
CLEANUP,
SLEEP,
MAX_STATE
}STATE;
enum {
IN_WORK,
FAILED,
SUCCESS
};
enum {// durations in minutes
MIN_2,
MIN_5,
MIN_10,
MAX_DUR// change/add durations as profile needs
};
const int dur[MAX_DUR] = {120, 300, 600};
int RunProcess(STATE state);
int main(void)
{
STATE state = START;
RunProcess(state);
return 0;
}
int RunProcess(STATE state)//Add arguments here for temperature,
{ //Sleep duration, Cycles, etc. so they are not hardcoded.
int status;
time_t elapsed = 0; //s
BOOL running = TRUE;
double temp, setpoint;
int duration;
time_t someMsValue;
while(running)
switch (state) {
case START:
//Power to oven
//Power to vibe
//initialize state and status variables
duration = dur[MIN_2];
state = SLEEP;
status = IN_WORK;
break;
case SET_VIB:
//test and control vibe
if(duration == dur[MIN_2])
{
duration = dur[MIN_5];
//& turn off vibe
}
else
{
duration = dur[MIN_2];
//& turn on vibe
}
//init elapsed
elapsed = 0;
status = IN_WORK;
state = CHECK_TEMP;
break;
case CHECK_TEMP:
//read temperature
if(temp < setpoint)
{
state = SLEEP;
status = IN_WORK;
}
else
{
state = CLEANUP;
status = SUCCESS;
}
break;
case GET_TIME:
elapsed = getTime();
if(elapsed > duration) state = SET_VIB;
else state = SLEEP;
status = IN_WORK;
break;
case CLEANUP:
//turn off heat
//turn off vibe
running = FALSE;
break;
case SLEEP:
Sleep(someMsValue);
state = GET_TIME;
status = IN_WORK;
break;
default:
// called with incorrect state value
// error message and exit
status = FAILED;
state = CLEANUP;
break;
}
return status;
}
Suggestion for improvement to this illustration:
Expand this code to read in a "profile" file. It could include such things as parameter values typical to your process, such as temperature profiles, vibration profiles, number of cycles, etc. With such information, all of the hard-coding used here as illustration could be replaced with run-time configurable parameters, allowing the system to use many different profiles
without having to recompile the executable each time.
Another state machine example.

Related

Shortest Remaining Time Scheduling Timing Issue

I am working on finishing an Operating Systems Process Scheduling Algorithms project, and I am running into a timing issue related to the Shortest Time Remaining First algorithm. My teammates and I hand calculated the results of the waiting time, turnaround time, etc., and compared them to an online source, so we think the problem is in our code, specifically for the waiting time.
Here is the relevant function where we calculate the timing and run the algorithm:
bool shortest_remaining_time_first(dyn_array_t *ready_queue, ScheduleResult_t *result)
{
if (!ready_queue || !result)
return false;
const int numProcesses = (int)ready_queue->size;
/* should provide array in lowest arrival times */
dyn_array_sort(ready_queue, &compare_arrival_times);
dyn_array_t *run_queue = dyn_array_create(ready_queue->capacity, ready_queue->data_size, NULL);
/* ProcessControlBlock_t looks like:
typedef struct ProcessControlBlock = {
uint32_t arrival,
uint32_t remaining_burst_time,
uint32_t priority
} ProcessControlBlock_t
and dyn_array_t looks like:
typedef struct dyn_array = {
size_t capacity;
size_t size;
const size_t data_size;
void *array; /* this is an array of ProcessControlBlock_t's*/
void (*destructor)(void *);
} dyn_array_t;
*/
ProcessControlBlock_t pcb, pcbRun;
dyn_array_extract_front(ready_queue, &pcb);
/* we need 3 of these to use seperately */
int processCount = numProcesses;
int loopCounter = numProcesses;
int runCounter = 0;
/* timing stuff */
int ticks = 0;
int waitTime = 0;
int timeComplete = 0;
/* used to check if the arrival time/burst time is the same to check for the same process */
int arrivalCheck = 0;
int burstCheck = 0;
/* while there are still processes to run */
while (loopCounter > 0)
{
/* will wait to push new process onto run_queue */
while((int)pcb.arrival <= ticks && processCount > 0)
{
runCounter++;
/* this will not push a new value until that much time has passed */
dyn_array_push_front(run_queue, &pcb);
if(processCount > 0)
{
dyn_array_extract_front(ready_queue, &pcb);
processCount--;
}
}
if(runCounter > 0)
{
/* we want to re-sort each time so we can always pull the smallest burst time */
dyn_array_sort(run_queue, &compareBurstTimes);
dyn_array_extract_front(run_queue, &pcbRun);
/* assuming there is still a process to run */
if(pcbRun.remaining_burst_time > 0)
{
/* if we are on the same process, do nothing */
/* else, change the waitTime */
if((int)pcbRun.arrival == arrivalCheck && (int)pcbRun.remaining_burst_time == burstCheck){}
else
{
/* this is where we think the problem is occurring */
waitTime += ticks - pcbRun.arrival;
}
/* the process has started, then run on "CPU" and increase time passed */
pcbRun.started = true;
virtual_cpu(&pcbRun);
ticks++;
/* if still > 0 after being decremented */
if(pcbRun.remaining_burst_time > 0)
{
dyn_array_push_front(run_queue, &pcbRun);
arrivalCheck = pcbRun.arrival;
burstCheck = pcbRun.remaining_burst_time;
}
}
/* if the process has run all the way through, decrease the number of processes */
if(pcbRun.remaining_burst_time <= 0)
{
loopCounter--;
runCounter--;
timeComplete += ticks - pcbRun.arrival;
}
}
else
{
/* if there is a gap between arrival times and processes */
ticks++;
}
}
/* calculate timing values */
result->average_waiting_time = (float)waitTime / (float)numProcesses;
result->average_turnaround_time = (float)timeComplete / (float)numProcesses;
result->total_run_time = ticks;
/* this frees run_queue AND run_queue->array */
dyn_array_destroy(run_queue);
return true;
}
Our first Process Control Block comes in with a table that looks like this:
pid
arrival
remaining_burst_time
priority
0
0
15
0
1
1
10
0
2
2
5
0
3
3
20
0
From these values, we hand-calculated and verified the waiting time to be 11.75, yet our program produces a calculated waiting time to be 12.25. Any idea why this may be?
Our approach of calculating each value on essentially almost every tick has made it more difficult to find an appropriate solution.

How to determine button press duration with wiringpi in C

For a aspberry pi project I have buttons connected to GPIO and I want to take different actions depending on how long a button has been pressed. The following python code (extract) is working as expected:
on_button2(channel):
t1 = time.time()
# Wait for button release
while GPIO.input(channel) == 0:
pass
duration = time.time() - t1
print "duration: %f" % (duration)
if duration > 0.75:
mpd_client.previous()
else:
mpd_client.next()
GPIO.add_event_detect(BUTTON2_PIN, GPIO.FALLING, callback=on_button2, bouncetime=700);
I would like to convert this into a C program (don't ask why, I really don't like python and I'm much more familiar with C, so I'd like to do it in C)
Trying to convert this to C with wiringPi, I came up with this, but it is not working as expected:
unsigned long btn2_t0;
static void on_button_2_pressed() {
unsigned long duration, t1;
int level;
level = digitalRead(BUTTON_2_PIN);
// Debounce button
t1 = millis();
if (t1 - btn2_t0 < 700) {
return;
}
btn2_t0 = t1;
// Wait for button being released
while (digitalRead(BUTTON_2_PIN) == LOW) {
delay(100);
}
duration = millis() - t1;
if (duration > 5000) {
printf("Self destruction sequence initiated!\n");
}
else if (duration > 700) {
player_previous();
}
else {
player_next();
}
}
int main() {
// Setup WiringPi Lib
wiringPiSetupGpio();
pinMode(BUTTON_2_PIN, INPUT);
// Register callbacks on button press
wiringPiISR(BUTTON_2_PIN, INT_EDGE_FALLING, on_button_2_pressed);
for (;;) {
delay(500);
}
return 0;
}
It seems that the loop that should wait for button release is not being executed or that the while-condition is always true, and thus, duration always is zero.
Is the digitalRead(BUTTON_2_PIN) function equivalent to the GPIO.input(channel) in the python code at all?
If somebody could point me in the right direction on how to detect button press (software-debounced) and measure the duration of the button press in C.
Thanks a lot.
EDIT: Working solution
After playing around a lot and with the help of Francesco Boi I came around with a working solution, although I do not really understand why the compare logic with HIGH / LOW is swapped compared to the python code (I thought that the button press will cause the pin to FALL to LOW and releasing it would RAISE it to HIGH ...)
static void on_button_2_pressed() {
unsigned long duration;
static unsigned long button_pressed_timestamp;
int level = digitalRead(BUTTON_2_PIN);
if (level == HIGH) { // Why HIGH ?!?
button_pressed_timestamp = millis();
}
else {
duration = millis() - button_pressed_timestamp;
button_pressed_timestamp = millis();
if (duration < 100) {
// debounce...
return;
}
else if (duration < 700) {
syslog(LOG_NOTICE, ">> NEXT\n");
}
else if (duration < 3000) {
syslog(LOG_NOTICE, "<< PREV\n");
}
else {
syslog(LOG_NOTICE, "!! REBOOT\n");
}
}
}
int main() {
...
wiringPiISR(BUTTON_2_PIN, INT_EDGE_BOTH, on_button_2_pressed);
...
}
First of all you are reading the value of the pin into level then in the while loop you are re-reading it: why is that? Can't you do something like:
// Wait for button being released
while (level == LOW) {
delay(100);
}
?
Also don't you want to reassign the btn2_t0 = t1; even when the time was smaller than 700 ms? Like:
t1 = millis();
if (t1 - btn2_t0 < 700) {
btn2_t0 = t1;
return;
}
The behaviour depends on how you built your electronic circuit: is the button-press suppose to make the pin high or low? Be sure about the behaviour is the one you are expecting by either connecting a led and a resistor or using a Voltmeter.
However since your Python code worked I am assuming the electronic is correct and your algorithm too.
Be sure of course you are pressing the button long enough. Add some prints to your code to understand which branches it is executing because when there is electronic involved it is difficult to understand only by the code.
While waiting for news from you, in my opinion is better to do the following: define the callback as: INT_EDGE_BOTH so that it got called when the bottom is pressed and when the button is released. You can keep the time elapsed with a static variable.
void yourCallback()
{
unsigned long ela, t;
int level = digitalRead(BUTTON_2_PIN);
static unsigned long button_pressed_timestamp;
//if button pressed:
if(level==LOW)
{
//start counting
button_pressed_timestamp = millis();
}
else //button released
{
duration = millis()-button_pressed_timestamp;
button_pressed_timestamp = millis(); //just to be sure....
if (duration > 5000) {
printf("Self destruction sequence initiated!\n");
}
else if (duration > 700) {
player_previous();
}
else {
player_next();
}
}
}

Getting volume value from pulseaudio

I've written this code by looking at various examples: Python pulseaudio monitor, Pavumeter source, async playback example, and Pacat source.
I have successfully connected to a sink and am able to record it, but my problem is, I'm stuck at getting the volume value out. If I try printing value from the read function, I just get a bunch of random numbers at a second's interval.
Now I'm not asking for someone to finish writing the code for me, I'd just like some tips, help so that I could head towards the right direction. How do I retrieve the volume value?
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <pulse/pulseaudio.h>
static int latency = 20000; // start latency in micro seconds
static int sampleoffs = 0;
static short sampledata[300000];
static pa_buffer_attr bufattr;
static int underflows = 0;
static pa_sample_spec ss;
// This callback gets called when our context changes state. We really only
// care about when it's ready or if it has failed
void pa_state_cb(pa_context *c, void *userdata) {
pa_context_state_t state;
int *pa_ready = userdata;
state = pa_context_get_state(c);
switch (state) {
// These are just here for reference
case PA_CONTEXT_UNCONNECTED:
case PA_CONTEXT_CONNECTING:
case PA_CONTEXT_AUTHORIZING:
case PA_CONTEXT_SETTING_NAME:
default:
break;
case PA_CONTEXT_FAILED:
case PA_CONTEXT_TERMINATED:
*pa_ready = 2;
break;
case PA_CONTEXT_READY:
*pa_ready = 1;
break;
}
}
static void stream_read_cb(pa_stream *s, size_t length, void *userdata) {
const void *data;
pa_stream_peek(s, &data, &length);
data = (const unsigned char*) data;
printf("%u", data);
pa_stream_drop(s);
}
int main(int argc, char *argv[]) {
pa_mainloop *pa_ml;
pa_mainloop_api *pa_mlapi;
pa_context *pa_ctx;
pa_stream *recordstream;
int r;
int pa_ready = 0;
int retval = 0;
unsigned int a;
double amp;
int test = 0;
// Create a mainloop API and connection to the default server
pa_ml = pa_mainloop_new();
pa_mlapi = pa_mainloop_get_api(pa_ml);
pa_ctx = pa_context_new(pa_mlapi, "Simple PA test application");
pa_context_connect(pa_ctx, NULL, 0, NULL);
// This function defines a callback so the server will tell us it's state.
// Our callback will wait for the state to be ready. The callback will
// modify the variable to 1 so we know when we have a connection and it's
// ready.
// If there's an error, the callback will set pa_ready to 2
pa_context_set_state_callback(pa_ctx, pa_state_cb, &pa_ready);
// We can't do anything until PA is ready, so just iterate the mainloop
// and continue
while (pa_ready == 0) {
pa_mainloop_iterate(pa_ml, 1, NULL);
}
if (pa_ready == 2) {
retval = -1;
goto exit;
}
ss.rate = 44100;
ss.channels = 2;
ss.format = PA_SAMPLE_U8;
recordstream = pa_stream_new(pa_ctx, "Record", &ss, NULL);
if (!recordstream) {
printf("pa_stream_new failed\n");
}
pa_stream_set_read_callback(recordstream, stream_read_cb, NULL);
r = pa_stream_connect_record(recordstream, NULL, NULL, PA_STREAM_PEAK_DETECT);
if (r < 0) {
printf("pa_stream_connect_playback failed\n");
retval = -1;
goto exit;
}
// Run the mainloop until pa_mainloop_quit() is called
// (this example never calls it, so the mainloop runs forever).
// printf("%s", "Running Loop");
pa_mainloop_run(pa_ml, NULL);
exit:
// clean up and disconnect
pa_context_disconnect(pa_ctx);
pa_context_unref(pa_ctx);
pa_mainloop_free(pa_ml);
return retval;
}
Looking at the original question from UNIX.StackExchange, it looks like you're trying to create a VU meter. It can be done using an envelope detector. You have to read the input values and then average their rectified value. A simple envelope detector can be done as an exponential moving average filter.
float level = 0; // Init time
const float alpha = COEFFICIENT; // See below
...
// Inside sample loop
float input_signal = fabsf(get_current_sample());
level = level + alpha * (input_signal - level);
Here, alpha is the filter coefficient, which can be calculated as:
const float alpha = 1.0 - expf( (-2.0 * M_PI) / (TC * SAMPLE_RATE) );
Where TC is known as the "time constant" parameter, measured in seconds, which defines how fast you want to "follow" the signal. Setting it too short makes the VU meter very "bumpy" and setting it too long will miss transients in the signal. 10 mS is a good value to start from.

Serial control and waiting for a rsponse

I am using an esp 8266 to control an Epson projector over its serial line. I am using a computer and OSC to call functions on the esp that run Epson serial commands. The command I'm having trouble implementing is zoom. The Epson Serial command to zoom the lens in 1 "click" is
ZOOM INC\r
Once the projector executes the code it returns either a : or :ERR. In my function it is suppose to execute the command several times depending on what the user inputs. In my function I want to wait until the projector responds before executing the command again, and this is what I have been trying.
void zoom_inc(OSCMessage &msg){
OSCMessage qLab_msg("/cue/p0/name");
Serial.print("Zoom ");
lock = 1;
char cmd[10] = "ZOOM INC\r";
if(msg.getInt(0) < 0){
cmd[5] = 'D';
cmd[6] = 'E';
}
int high = (abs(msg.getInt(0)) > 50)? 50 : abs(msg.getInt(0));
Serial.print(cmd);
Serial.print(" ");
Serial.print(high);
Serial.println(" times");
unsigned long startTime;
unsigned long currentTime;
unsigned long diff;
boolean response = false;
String readString; //create response string
for(int i = 0; i < high; i++){
projSerial.write(cmd);
startTime = millis();
while(!response){
while (projSerial.available() > 0) { //look for projector response
Serial.write(projSerial.read());
delay(3);
char c = projSerial.read();
readString += c;
}
readString.trim(); //clean projector response
if(readString.length() == 1){
Serial.println("Read Data");
Serial.println(readString.length());
Serial.println(readString);
Serial.println("------------------------------------");
response = true;
}
currentTime = millis();
diff = currentTime - startTime;
if(diff >= 5000 || diff < 0){
Serial.println("Timeout");
response = true;
}
}
delay(200);
}
qLab_msg.add("Zoom Incremental");
Udp.beginPacket(qLabIP, qLabPort);
qLab_msg.send(Udp);
Udp.endPacket();
qLab_msg.empty();
}
This doesn't work because it only does about half of what its suppose to. For example if the user sends 30 it only does 14
This looks suspicious:
Serial.write(projSerial.read());
delay(3);
char c = projSerial.read();
readString += c;
You've just read two characters from projSerial. One got echoed to your main serial port, and the other will be added to the string. Losing approximately every other character from the projector's response seems consistent with getting only about half the steps you expected.
Try:
char c = projSerial.read(); // read it just once
Serial.write(c);
readString += c;
I assume the delay(3) was from an earlier guess.

Arduino Switch to Turn a Relay timer

Briefly: I would like to turn on a relay for 30 seconds, after I toggle a switch on.
I'm trying to do a blinds automation at home.
I have a simple ON-OFF-ON switch, attached to an Arduino connected to Relays.
I want to turn on Relay#1 for a maximum of 30 seconds if I toggle the switch down from center. In other words, relay turns on when I switch, and when timer reaches 30 seconds relay turns off.
similarly I want to turn on Relay#2 for exactly 30 seconds if I toggle the switch up from center
And when I switch back to center, I would like the timer to reset.
I could not figure out how. Could anyone help?
I have been trying to use elapsedMillis library for this, which is a nice library that helps me avoid using Delays:
http://playground.arduino.cc/Code/ElapsedMillis
However even though I could work the relays without the 30 second limitation, I couldn't figure out the code to end working of the relays. Here is my current code:
#include <elapsedMillis.h>
#define RELAY_ON 0
#define RELAY_OFF 1
#define RELAY1_TURNS_ON_BLINDS 5
#define RELAY2_SHUTS_DOWN_BLINDS 6
#define shutswitch A0
#define openswitch A1
bool LocalCommandToOpen;
bool LocalCommandToShut;
void setup() ////////SETUP////////
{
digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_OFF);
digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_OFF);
pinMode(RELAY1_TURNS_ON_BLINDS, OUTPUT);
pinMode(RELAY2_SHUTS_DOWN_BLINDS, OUTPUT);
pinMode(shutswitch, INPUT);
pinMode(openswitch, INPUT);
} ////SETUP
void loop() { ///////LOOP
if (digitalRead(shutswitch) == 1)
{
LocalCommandToOpen = 1;
}
else
{
LocalCommandToOpen = 0;
}
if ( digitalRead(openswitch) == 1)
{
LocalCommandToShut = 1;
}
else
{
LocalCommandToShut = 0;
}
unsigned int CloseInterval = 14000;
elapsedMillis timeElapsedSinceCloseButtonPush = 0;
unsigned int OpenInterval = 14000;
elapsedMillis timeElapsedSinceOpenButtonPush = 0;
//MANUAL SWITCH OPERATION
if ( LocalCommandToShut == 1 )
{
digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_OFF);
digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_ON);
}
else
{
digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_OFF);
}
//MANUEL DUGME ILE ACMA
if ( LocalCommandToOpen == 1)
{
digitalWrite(RELAY2_SHUTS_DOWN_BLINDS, RELAY_OFF);
digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_ON);
}
else
{
digitalWrite(RELAY1_TURNS_ON_BLINDS, RELAY_OFF);
}
delay(500);
} /////////////////LOOP////////////////////////////////////
One suggestion here is to use a "state machine", so that upon a switch transition, you get 'State 1'; in that state, a timer starts, and puts you in 'State 2'. In 'State 2', you check the time, and if it goes beyond X seconds, you go to 'State 3'. You can monitor the transition of a switch from low to high (or high to low), and use this to reset the state of the system.
A sample bit of code gives you an idea of how to implement this. The variable 'SystemState' is an integer, and SYSTEM_ABORT, SYSTEM_IDLE, etc. are constants.
The beauty of this is that the transition is easy to figure out. For example, if you are in the SYSTEM_WAIT state, the only thing you are looking for is the time to be greater than 5 seconds. Also, you can look at input transitions or values to set states (like SystemStopButton == 0, to set the state to be 'SYSTEM_ABORT').
// SystemStopButton is an input
void SystemStateMachine(void)
///////////////////////////////////////////////////////////////////////////////////////////
{
if (SystemStopButton == 0)
{
SystemState = SYSTEM_ABORT;
}
switch (SystemState)
{
case SYSTEM_IDLE:
{
RunPinState = OFF;
StopPinState = OFF;
if (SystemRunButton == 0)
{
SystemState = SYSTEM_START;
ShowStep();
}
break;
}
case SYSTEM_START:
{
StandardMessage = "START ";
RunPinState = ON;
StopPinState = OFF;
SystemState = SYSTEM_WAIT;
ShowStep();
break;
}
case SYSTEM_WAIT: // wait for 5 seconds
{
StandardMessage = "WAIT ";
if ((CurrentMillis - StateStepTimeMillis) > 5000)
{
SystemState = SYSTEM_RETRACT;
ShowStep();
}
break;
}
case SYSTEM_RETRACT: //
{
StandardMessage = "RETRACT";
/* motor stuff goes here... */
if ((CurrentMillis - StateStepTimeMillis) > 5000)
{
SystemState = SYSTEM_ADVANCE_TAPE_WAIT
ShowStep();
}
break;
}
// etc. etc. etc.
case SYSTEM_ABORT: //
{
StandardMessage = "ABORT";
/* motor stop stuff goes here... */
SystemState=SYSTEM_IDLE;
break;
}
default:
{
break;
}
}
}
void ShowStep(void)
/////////////////////////////////////////////////////////////////////
// show step and set time so we can keep track of time in each step
{
Serial.print("SystemState = ");
Serial.println(String(SystemState));
SetStepTime();
}
void SetStepTime(void)
/////////////////////////////////////////////////////////////////////
{
StateStepTimeMillis = CurrentMillis;
}
You might use a state machine; this makes things a bit easier to follow.
Similar to:
Arduino Switch to Turn a Relay timer
A nice discussion of state machines is here:
Complicated state transitions: best practices

Resources