Stop a Function Loop

Support forum for MegunoLink
Post Reply
nebula
Posts: 2
Joined: Fri Jun 28, 2019 10:27 pm

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();
}
philr
Posts: 298
Joined: Mon May 26, 2014 10:58 am

Mon Jul 01, 2019 8:45 am

I'll post my email reply here just for completeness.

Tricky one. Yeah I'm not sure it's possible. 

The signal from Megunolink requires the command handler process function to be called frequently. While it's running another function there is no way to process commands. If you had a hardware switch you could attach it to an interrupt and the interrupt could change a Boolean which the forward function could check in a number of places to allow a break...

What's the move function look like? Maybe it could be broken up into smaller sub moves allowing you to exit and let the command handler run as well... Then you could send stop signal and interrupt any further sub moves...

Cheers
Phil
Post Reply