0

i'm trying to write a sketch in C and i need to play an animation on my LED Matrix while initializing function for my program. The problem is, because i need to play the animation in loop and because during delays function the board does not do anything else, i dont know how to loop it while keep doing my other functions.

Here is the animation:

void Animation(){ 
  max7219.clr();
  if(InternetStatus){    //This is to draw a circle if internet has connected during the animation
    max7219.setColumn(0, B11111111);
    for(int i=1; i<7; i++){
      max7219.setColumn(i, B10000001);
    }
    AnUp = B10011101;
    AnCenter = B10111001;
    AnDown = B11110001;
    max7219.setColumn(10, B10000001);
    max7219.setColumn(11, B10000001);
    max7219.setColumn(15, B10000001);
    max7219.setColumn(16, B10000001);
    max7219.setColumn(20, B10000001);
    max7219.setColumn(21, B10000001);
    for(int i=25; i<31; i++){
      max7219.setColumn(i, B10000001);
    }
    max7219.setColumn(31, B11111111);
  } else {
    AnUp = B00011100;
    AnCenter = B00111000;
    AnDown = B01110000;
  }
   max7219.setColumn(7, AnCenter);
   max7219.setColumn(8, AnCenter);
   max7219.setColumn(9, AnCenter);
   max7219.setColumn(12, AnCenter);
   max7219.setColumn(13, AnCenter);
   max7219.setColumn(14, AnCenter);
   max7219.setColumn(17, AnCenter);
   max7219.setColumn(18, AnCenter);
   max7219.setColumn(19, AnCenter);
   max7219.setColumn(22, AnCenter);
   max7219.setColumn(23, AnCenter);
   max7219.setColumn(24, AnCenter);
   max7219.refreshAll();
   delay(AnDelay);   //delays are set between frames
   AnIndex = 0;
   max7219.setColumn(7+(AnIndex*5), AnUp);
   max7219.setColumn(8+(AnIndex*5), AnUp);
   max7219.setColumn(9+(AnIndex*5), AnUp);
   max7219.refreshAll();
   delay(AnDelay);
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   AnIndex = 1;
   max7219.setColumn(7+(AnIndex*5), AnUp);
   max7219.setColumn(8+(AnIndex*5), AnUp);
   max7219.setColumn(9+(AnIndex*5), AnUp);
   max7219.refreshAll();
   delay(AnDelay);
   AnIndex=0;
   max7219.setColumn(7+(AnIndex*5), AnDown);
   max7219.setColumn(8+(AnIndex*5), AnDown);
   max7219.setColumn(9+(AnIndex*5), AnDown);
   AnIndex=1;
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   AnIndex=2;
   max7219.setColumn(7+(AnIndex*5), AnUp);
   max7219.setColumn(8+(AnIndex*5), AnUp);
   max7219.setColumn(9+(AnIndex*5), AnUp);
   max7219.refreshAll();
   delay(AnDelay);
   AnIndex=0;
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   AnIndex=1;
   max7219.setColumn(7+(AnIndex*5), AnDown);
   max7219.setColumn(8+(AnIndex*5), AnDown);
   max7219.setColumn(9+(AnIndex*5), AnDown);
   AnIndex=2;
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   AnIndex=3;
   max7219.setColumn(7+(AnIndex*5), AnUp);
   max7219.setColumn(8+(AnIndex*5), AnUp);
   max7219.setColumn(9+(AnIndex*5), AnUp);
   max7219.refreshAll();
   delay(AnDelay);
   AnIndex=1;
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   AnIndex=2;
   max7219.setColumn(7+(AnIndex*5), AnDown);
   max7219.setColumn(8+(AnIndex*5), AnDown);
   max7219.setColumn(9+(AnIndex*5), AnDown);
   AnIndex=3;
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   max7219.refreshAll();
   delay(AnDelay);
   AnIndex=2;
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   AnIndex=3;
   max7219.setColumn(7+(AnIndex*5), AnDown);
   max7219.setColumn(8+(AnIndex*5), AnDown);
   max7219.setColumn(9+(AnIndex*5), AnDown);
   max7219.refreshAll();
   delay(AnDelay);
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   max7219.refreshAll();
   delay(AnDelay);
}

And i need to run that animation while executing these functions:

void Wait4Connection(){
  Serial.println("Waiting for connection.");
  InternetStatus = false;
  WaitCon:
  max7219.clr();
  InternetStatus = false;
  RetimeTry = 0;
  Serial.println("Trying to connect.");
  wifiManager.autoConnect("Clock Configuration");
  ret = Ping.ping(ip);
  if(ret){ //prova connessione internet
    Serial.println("Device connected to the internet.");
    timeClient.begin();
    Retime:
    InternetStatus = true;
    Serial.println("Clock synchronization.");
    timeClient.update();
    Hours = timeClient.getHours() + HH + IsDst(day(timeClient.getEpochTime()), month(timeClient.getEpochTime()), weekday(timeClient.getEpochTime()));
    Minutes = timeClient.getMinutes() + MM;
    Seconds = timeClient.getSeconds();
    if(timeClient.getEpochTime() > 1000){                                                    
      RetimeTry=0;
      Serial.println("Starting clock mode.");
      tasker.setInterval(Clocking, 1000);
      tasker.setInterval(Refresh, 300000);
    } else {
      if(RetimeTry<10){
        Serial.print("No time received. Retrying. x");
        Serial.println(RetimeTry+1);
        RetimeTry++;
        goto Retime;
      } else {
        Serial.println("Can't obtain time packets. Reconnecting.");
        goto WaitCon;
      }
      RetimeTry++;
    }
  } else {
    Serial.println("Connection failed.");
    InternetStatus = false;
    Serial.println("Slowing down 30s");
    delay(30000);
    tasker.cancel(Animation);
    goto WaitCon;
  }
}

Thanks.

Parzival
  • 1
  • 2
  • You could write a scheduler for your microcontroller and switch between the different concurrent tasks. – Christian Gibbons Nov 05 '18 at 17:01
  • I would highly highly recommend refactoring your code into two finite state machines that can trade off running one state at a time. This will make it easy to add another FSM if you have to and give you good practice with microcontrollers (any bigger project you do will likely be in a FSM). For more on structuring FSM's in C/C++, see here: https://stackoverflow.com/questions/133214/is-there-a-typical-state-machine-implementation-pattern – scohe001 Nov 05 '18 at 17:03

1 Answers1

0

Well, at the end i came up with this:

short AnimationControl = 0;

void AnimationMain(){
  switch (AnimationControl){
    case 0:
    Animation1();
    break;

    case 1:
    Animation2();
    break;

    case 2:
    Animation3();
    break;

    case 3:
    Animation4();
    break;

    case 4:
    Animation5();
    break;

    case 5:
    Animation6();
    break;

    case 6:
    Animation7();
    break;

    case 7:
    Animation8();
    break;
  }
}

void Animation1()
  max7219.clr();
  if(InternetStatus){
    max7219.setColumn(0, B11111111);
    for(int i=1; i<7; i++){
      max7219.setColumn(i, B10000001);
    }
    AnUp = B10011101;
    AnCenter = B10111001;
    AnDown = B11110001;
    max7219.setColumn(10, B10000001);
    max7219.setColumn(11, B10000001);
    max7219.setColumn(15, B10000001);
    max7219.setColumn(16, B10000001);
    max7219.setColumn(20, B10000001);
    max7219.setColumn(21, B10000001);
    for(int i=25; i<31; i++){
      max7219.setColumn(i, B10000001);
    }
    max7219.setColumn(31, B11111111);
  } else {
    AnUp = B00011100;
    AnCenter = B00111000;
    AnDown = B01110000;
  }
  max7219.setColumn(7, AnCenter);
  max7219.setColumn(8, AnCenter);
  max7219.setColumn(9, AnCenter);
  max7219.setColumn(12, AnCenter);
  max7219.setColumn(13, AnCenter);
  max7219.setColumn(14, AnCenter);
  max7219.setColumn(17, AnCenter);
  max7219.setColumn(18, AnCenter);
  max7219.setColumn(19, AnCenter);
  max7219.setColumn(22, AnCenter);
  max7219.setColumn(23, AnCenter);
  max7219.setColumn(24, AnCenter);
  max7219.refreshAll();
  AnimationControl=1;
}


void Animation2(){
   AnIndex = 0;
   max7219.setColumn(7+(AnIndex*5), AnUp);
   max7219.setColumn(8+(AnIndex*5), AnUp);
   max7219.setColumn(9+(AnIndex*5), AnUp);
   max7219.refreshAll();
   AnimationControl=2;
}

void Animation3(){
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   AnIndex = 1;
   max7219.setColumn(7+(AnIndex*5), AnUp);
   max7219.setColumn(8+(AnIndex*5), AnUp);
   max7219.setColumn(9+(AnIndex*5), AnUp);
   max7219.refreshAll();
   AnimationControl=3;
}

void Animation4(){
   AnIndex=0;
   max7219.setColumn(7+(AnIndex*5), AnDown);
   max7219.setColumn(8+(AnIndex*5), AnDown);
   max7219.setColumn(9+(AnIndex*5), AnDown);
   AnIndex=1;
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   AnIndex=2;
   max7219.setColumn(7+(AnIndex*5), AnUp);
   max7219.setColumn(8+(AnIndex*5), AnUp);
   max7219.setColumn(9+(AnIndex*5), AnUp);
   max7219.refreshAll();
   AnimationControl = 4;
}

void Animation5(){
   AnIndex=0;
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   AnIndex=1;
   max7219.setColumn(7+(AnIndex*5), AnDown);
   max7219.setColumn(8+(AnIndex*5), AnDown);
   max7219.setColumn(9+(AnIndex*5), AnDown);
   AnIndex=2;
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   AnIndex=3;
   max7219.setColumn(7+(AnIndex*5), AnUp);
   max7219.setColumn(8+(AnIndex*5), AnUp);
   max7219.setColumn(9+(AnIndex*5), AnUp);
   max7219.refreshAll();
   AnimationControl = 5;
}

void Animation6(){
   AnIndex=1;
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   AnIndex=2;
   max7219.setColumn(7+(AnIndex*5), AnDown);
   max7219.setColumn(8+(AnIndex*5), AnDown);
   max7219.setColumn(9+(AnIndex*5), AnDown);
   AnIndex=3;
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   max7219.refreshAll();
   AnimationControl = 6;
}

void Animation7(){
   AnIndex=2;
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   AnIndex=3;
   max7219.setColumn(7+(AnIndex*5), AnDown);
   max7219.setColumn(8+(AnIndex*5), AnDown);
   max7219.setColumn(9+(AnIndex*5), AnDown);
   max7219.refreshAll();
   AnimationControl = 7;
}

void Animation8(){
   max7219.setColumn(7+(AnIndex*5), AnCenter);
   max7219.setColumn(8+(AnIndex*5), AnCenter);
   max7219.setColumn(9+(AnIndex*5), AnCenter);
   max7219.refreshAll();
   AnimationControl = 0;
}

And i called AnimationMain with the Ticker library every setted interval (in my case 125ms).

Hopefully helps someone.

Parzival
  • 1
  • 2
  • It's a little clunky to make each animation step responsible for modifying the counter, not to mention the switch statement. Right now, if you wanted one step of the animation to be the same as another, you'd need to create a new function for it. Since your animation is simple and cyclical, consider using an array of function pointers instead, and have the main animation function drive the counter. For example: https://ideone.com/n0Om36 – paddy Nov 05 '18 at 21:46