Stop a Function Loop
Posted: Fri Jun 28, 2019 11:03 pm
Noob question here...
Is there a way to stop a function loop during execution by triggers from another function?
For instance in the pseudo-code below, can the Stop function be used to break the Forward function in mid-execution.
It seems the function loop has to be completed before any other function is run.
I do want to go down the rabbit hole of watchdog timers, I figure some way of restarting the CommandHandler.Process() i.e. the Event Stack might be the way to go.
This code is for a simple stepper motor control.
#include "CommandHandler.h"
CommandHandler<> SerialCommands;
const int PinA = 7;
const int PinB = 6;
void setup()
{
pinMode(PinA, OUTPUT);
pinMode(PinB, OUTPUT);
Stop();
Serial.begin(9600);
SerialCommands.AddCommand(F("Forward"), Cmd_Forward);
SerialCommands.AddCommand(F("Stop"), Cmd_Stop);
}
void loop()
{
SerialCommands.Process();
}
void Stop()
{
digitalWrite(PinA, LOW); //doesn't work
digitalWrite(PinB, LOW); //doesn't work
}
void Move()
{
for (int x = 0; x < 8000; x++) {
digitalWrite(PinA, LOW);
delay(1000)
digitalWrite(PinB, HIGH);
}
}
void Cmd_Forward(CommandParameter &p)
{
Move();
}
void Cmd_Stop(CommandParameter &p)
{
Stop();
}
Is there a way to stop a function loop during execution by triggers from another function?
For instance in the pseudo-code below, can the Stop function be used to break the Forward function in mid-execution.
It seems the function loop has to be completed before any other function is run.
I do want to go down the rabbit hole of watchdog timers, I figure some way of restarting the CommandHandler.Process() i.e. the Event Stack might be the way to go.
This code is for a simple stepper motor control.
#include "CommandHandler.h"
CommandHandler<> SerialCommands;
const int PinA = 7;
const int PinB = 6;
void setup()
{
pinMode(PinA, OUTPUT);
pinMode(PinB, OUTPUT);
Stop();
Serial.begin(9600);
SerialCommands.AddCommand(F("Forward"), Cmd_Forward);
SerialCommands.AddCommand(F("Stop"), Cmd_Stop);
}
void loop()
{
SerialCommands.Process();
}
void Stop()
{
digitalWrite(PinA, LOW); //doesn't work
digitalWrite(PinB, LOW); //doesn't work
}
void Move()
{
for (int x = 0; x < 8000; x++) {
digitalWrite(PinA, LOW);
delay(1000)
digitalWrite(PinB, HIGH);
}
}
void Cmd_Forward(CommandParameter &p)
{
Move();
}
void Cmd_Stop(CommandParameter &p)
{
Stop();
}