Detect Motor Boolean Change and Send Only One Serial Message

Hello,

I have a controller program (puredata) communicating with arduino and listening for commands. So I send a command to arduino from puredata that makes a motor move. When the motor is no longer moving I want to send a “ready” command back to puredata only ONCE.

In puredata I could use the [change] object which sends a [0< or [1< only when the message has changed but I want to do as much as I can on arduino side.

So right now I am using AccelStepper lib with MultiStepper. The steppers.run(); command returns a TRUE if running and a FALSE if not running.

How can I, once the motor is “not running”, send only ONE ready back to my control program? Right now I just get an endless stream of “ready”.

My loop is below:

void loop() {
    recvWithStartEndMarkers();
    
    if (newData == true) {
        strcpy(tempChars, receivedChars);
        parseData();
        showParsedData();
        newData = false;
    }

    long positions[2]; // Array of desired stepper positions
  
    positions[0] = stepsM1;
    positions[1] = stepsM2;

    if (strcmp(ctlMessage, "move")  == 0) { // Compare two strings
       steppers.moveTo(positions);
       steppers.run();
    }

    if (strcmp(ctlMessage, "release")  == 0) {
       stepMotor1->release(); 
       stepMotor2->release();
    }

    // Here I want to send a "ready" message only ONCE when steppers.run() = false
    Serial.println("<ready>");

    }

Open up the state change example in the IDE. Although that reads a buttons, the principle is the same :)

septillion: Open up the state change example in the IDE. Although that reads a buttons, the principle is the same :)

Not really, it isn't.

When you send a message, set a flag, bool messageSent, to true. Don't send another message if the flag is true. Clear the flag when you receive a (complete) message.

Yes it is.... It's not about receiving a message, it's about sending a message when a process becomes done.