Teensy resetting due to infinite loop conflict with 4 clocks

NuttyMonk

Well-known member
Hi all,

my Teensy 3.2 was resetting and i couldn't figure out why. The Teensy is being used to run 4 clocks for modular synths which all have various settings. One of the functions i've built is for one clock to reset another when it gets back to the start of its loop. This will keep clocks in time with each other over longer periods if required. I realised that i was creating an infinite loop when i was adjusting clock 1 to be reset by clock 2 which was already set up to be reset by clock 1. I've realised that when i make an adjustment to a clock, i need a way to recursively check each clock and see if by making that adjustment i am going to create an infinite loop. I already have it set up so that each clock is unable to use itself to reset but i need to expand that.

For example, if i set up clock 1 to reset clock 2 which resets clock 3 which resets clock 4 which resets clock 1, then an infinite loop conflict will occur, but i can't just check that by checking if clock 2 is set to reset clock 1. I need to follow the chain of clocks resetting each other to the end.

I'm looking for help from anyone who has a good idea about how to turn this idea into code. When i have to build up recursive loops i end up in a muddle and find it hard to understand what is going on.

Any help would be greatly appreciated since all i can come up with is an idea for a huge amount of code to check through the clock reset settings in a manual way and not an elegant way like i know can be done, just not by me.

Cheers

NM
 
p.s. i have the values for which clock resets which in an array named

Code:
int resetClock[5] = {0, 0, 1, 1, 1};

In this example, clocks 2, 3 and 4 are all reset by clock 1. I ignore the value stored in resetClock[0].

Ta
 
If all clocks are started, and no clock changes any other clock, does it run without failure or restart?

Please post reference code per the Forum Rule ... at the top of each forum page.

Without code, a reader has no idea what clocks are in use or how they might be updated.
 
If all clocks are started, and no clock changes any other clock, does it run without failure or restart?

Yes, it works fine. I know what the issue is and am not needing help debugging any code, but i am looking for suggestions for code

Please post reference code per the Forum Rule ... at the top of each forum page.

Without code, a reader has no idea what clocks are in use or how they might be updated.

I am looking for ideas for code that will help me with this issue. I am using a Teensy 3.2 with 4 instances of elapsedMicros as my clocks. I check them each time the loop() function runs to see if their time is up. The issue isn't with the clocks but with the possible infinite loop i am creating when i allow clocks to reset each other in a circular manner. The elapsedMicros values are reset to 0 when a clock is reset but if there is an infinite loop, they just keep resetting each other and the Teensy restarts.

In a previous post i used this code to show how i save the clocks that reset each other

Code:
int resetClock[5] = {0, 0, 1, 1, 1};

In this case, clocks 2, 3 and 4 are all reset by clock 1.

Code:
int resetClock[5] = {0, 4, 1, 2, 3};

In this case clock 1 resets clock 2 which resets clock 3 which resets clock 4 which resets clock 1. I have therefore created an infinite loop and the Teensy 3.2 restarts itself. I am looking for suggestions for code which can allow me to check if this sort of situation is going to occur before i make the change to the variables.

Cheers

NM
 
Usually an infinite loop doesn't cause Teensy to reboot. Well, not unless you've enabled the watchdog timer.

If you're not using the watchdog, perhaps the problem is hardware related, like power supply instability?
 
If you're not using the watchdog, perhaps the problem is hardware related, like power supply instability?

I thought it might be hardware at first but it only happens when i set the first clocks reset number to that of another clock, and they are all set to be reset by clock 1. All 3 of them will be reset by clock 1 and they in turn will all reset clock 1.

The issue literally never happens unless i make a change to a clocks reset and that causes an infinite loop. When a clock is used to reset another clock, a couple of functions are called. The first function below is called to see if that clock should be resetting any of the other 3 clocks and if so the second function is called. The second function is the one that is used for clock 1.

Code:
//////////////////////////////////////////////////////
// send clock resets from Internal clocks
//////////////////////////////////////////////////////
void sendResetsINT(int thisClock) {

  for (int i = 1; i <= 4; i++) {
    if (resetClock[i] == thisClock) {
      if (i == 1) {
        //Serial.println("Send Clock 1 Reset");
        clock1ResetINT();
      }
      
      if (i == 2) {
        //Serial.println("Send Clock 2 Reset");
        clock2ResetINT();
      }

      if (i == 3) {
        //Serial.println("Send Clock 3 Reset");
        clock3ResetINT();
      }

      if (i == 4) {
        //Serial.println("Send Clock 4 Reset");
        clock4ResetINT();
      }
    }
  }
}

//////////////////////////////////////////////////////
// reset clocks from Internal clocks
//////////////////////////////////////////////////////
void clock1ResetINT() {
  clock1Timer = 0;
  currentStep[1] = 1;
  clockSet[1] = HIGH;
  digitalWrite(clock1Pin, HIGH);

  sendResetsINT(1);
}

Damn, i think i just figured out the problem. These two function are both calling each other. Would that cause the Teensy to reset?

Also, i still need to figure out how to write some recursive code to make sure that i can't create a loop of clock resets otherwise they won't work right

Cheers

NM
 
With the two functions calling each other the stack won't last long and it will go beyond that causing undesired and unplanned behavior ... such as a reset.

That's one side effect and value of posting code. Either cleaning it up to present - or as read by other eyes - things like that get noticed.
 
With the two functions calling each other the stack won't last long and it will go beyond that causing undesired and unplanned behavior ... such as a reset.

That's one side effect and value of posting code. Either cleaning it up to present - or as read by other eyes - things like that get noticed.

So it would seem :)
 
I'm looking for help from anyone who has a good idea about how to turn this idea into code. When i have to build up recursive loops i end up in a muddle and find it hard to understand what is going on.

The easiest way is to add a recursion count - if you recurse more times than the number of clocks you know you are on a cycle.
 
Back
Top