Reset arduino after X time while looping

In my code I have the following snippet:

void loop()
{ 
    recvWithEndMarker();
    if (newData == true) {
        switch (receivedChars[0]) {
            case '1':
                DW1000Ranging.loop();
                break;
        }
    }
}

The recvWithEndMarker(); receives the data from the serial monitor, if I write a 1 the value will be assigned to receivedChars[0].

But that is not my problem, my problem comes with DW1000Ranging.loop(); It enters the loop of another function and never exits, so "break" is not executed. (To break this loop I have to modify the library, but I don't know professional C++, so it's best to look for an alternative like the one I just thought of.)

I'm thinking of some solution like Arduino reset after X time, no matter it's in the loop. That is, first enter the loop, pass the time and then reset the Arduino. Any solution?

Note: I use ESP32 and it is reset with ESP.restart();

That sounds very unlikely

Please post your full sketch so that we can see your code snippet in context. For instance, which library are you using to create the W1000Ranging object and where did you get it from ?

My complete code is:

#include <SPI.h>
#include "DW1000Ranging.h"

#define ANCHOR_ADD "83:17:5B:D5:A9:9A:E2:9C"

#define SPI_SCK 18
#define SPI_MISO 19
#define SPI_MOSI 23
#define DW_CS 4

// connection pins
const uint8_t PIN_RST = 27; // reset pin
const uint8_t PIN_IRQ = 34; // irq pin
const uint8_t PIN_SS = 4;   // spi select pin

const byte numChars = 32;
char receivedChars[numChars];
boolean newData = false;

void setup()
{
    Serial.begin(115200);
    delay(1000);
    //init the configuration
    SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
    DW1000Ranging.initCommunication(PIN_RST, PIN_SS, PIN_IRQ); //Reset, CS, IRQ pin
    //define the sketch as anchor. It will be great to dynamically change the type of module
    DW1000Ranging.attachNewRange(newRange);
    DW1000Ranging.attachBlinkDevice(newBlink);
    DW1000Ranging.attachInactiveDevice(inactiveDevice);
    //Enable the filter to smooth the distance
    //DW1000Ranging.useRangeFilter(true);
    
    DW1000Ranging.startAsAnchor(ANCHOR_ADD, DW1000.MODE_LONGDATA_RANGE_LOWPOWER, false);
}

void loop()
{ 
    recvWithEndMarker();
    if (newData == true) {

        switch (receivedChars[0]) {
            case '1':
                DW1000Ranging.loop();
                break;
        }
    }

    //DW1000Ranging.loop();
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;
  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();
    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0';
      ndx = 0;
      newData = true;
    }
  }
}

void newRange()
{
    //Serial.print("from: ");
    //Serial.print(DW1000Ranging.getDistantDevice()->getShortAddress(), HEX);
    //Serial.print("\t Range: ");
    Serial.println(DW1000Ranging.getDistantDevice()->getRange());
    //Serial.print(" m");
    //Serial.print("\t RX power: ");
    //Serial.print(DW1000Ranging.getDistantDevice()->getRXPower());
    //Serial.println(" dBm");
}

void newBlink(DW1000Device *device)
{
    Serial.print("blink; 1 device added ! -> ");
    Serial.print(" short:");
    Serial.println(device->getShortAddress(), HEX);
}

void inactiveDevice(DW1000Device *device)
{
    Serial.print("delete inactive device: ");
    Serial.println(device->getShortAddress(), HEX);
}

But as you can see, there is no where to get DW1000Ranging.loop();

DW1000Ranging.loop(); you get it straight from the library:

Line 373

Is there a way to break the loop class?

You never set newData to false, so once you receive a serial input, the if statement will always be true, and the switch statement will be executed repeatedly for the same input.

Do you know how to make a counter? Let it count to 10 and then execute newData = false;

You seem to have not yet understood how receive with endmarker works:

after first program start newData is false

only if the endmarker is received the flag variable newData is set to true

Now

You

the user has to do

two

things :

  1. react on the data
  2. set flag-variable newData to false again to enable processing the next data that is received

I have no idea what you want to say with that.
trying to be quick through beeing short on words turns out to be slow in fact.
problem-solving can be accelerated through taking time for a detailed description

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.