How i make function like void loop and void setup in arduino?

Viewed 848

I want make function like void loop or void setup like arduino main function which that function can input another execution code.

unsigned long NOW;

void setup() {
}

void loop() {
  
  void doEvery(2){ //do Every 2 second
    //Put Code that Execute every 2 second
  }

  void doEvery(4){ //do Every 4 second
    //Put Code that Execute every 4 second
  }

}

How to declare/define function doEvery? i think that function must contain

if(millis()-NOW>=EVERY){
NOW=millis();
//THE LINE CODE
}
2 Answers

Taking THIS as initial idea:

unsigned long previousMillis2 = 0, previousMillis100 = 0;

void setup() {
}

void loop() {
  unsigned long currentMillis = millis();

  //doEvery 2
  if (currentMillis - previousMillis2 >= 2) {
    previousMillis2 = currentMillis; //stores last execution's timestamp
    //CODE EVERY 2  millis
  }

  //doEvery 100
  if (currentMillis - previousMillis100 >= 100) {
    previousMillis100 = currentMillis; //stores last execution's timestamp
    //CODE EVERY 100  millis
  }
}

With this, you will use millis() to ask for how many millis passed since initialization of Arduino. Then you store last time you executed your code and compare how many time passed since it. It's not a dynamic function for defining new intervals but if you only need a pair of it, you can code it easily.

EDIT:

If you need something more dynamic, you should use anonymous functions. SEE THIS or THIS The point here is function as parameter.

I've made an animation library out of this for the WS2812 ledstrip:

https://github.com/laurijssen/ledstripanimator/blob/master/ledanim.c

The timing stuff that jabujavi describes happens in the UpdateFrame function where every animation in the list advances to the next frame after it's configured milliseconds.

class AnimList
{
  Animation *first = NULL;
  Adafruit_NeoPixel strip;

public:
  AnimList(int nleds) : strip(nleds, 9, NEO_GRB + NEO_KHZ800)
  {
    strip.begin();
  }

  void UpdateFrame()
  {
    strip.clear();
    
    Animation **anim = &first;

    while (*anim)
    {
      if (millis() - (*anim)->last >= (*anim)->ms)
      {
        if (!(*anim)->UpdateFrame(strip))
        {
          *anim = (*anim)->next;
        }
        else
        {
          (*anim)->last = millis();
          anim = &(*anim)->next;
        }
      }
    }
  }
};

Now you just call AnimList::UpdateFrame as fast as possible inside loop()

void loop() {   
  list->UpdateFrame();
  list->Render();

  if (!list->Find(&r1))
  {
    r1 = Rain (rand() % NUMLEDS, rand() % NUMLEDS, {BRIGHTNESS, 0, 15 }) ;
    list->AddFirst(&r1);
  } 
}

edit

So if you want to execute code every 2 seconds you create a class which inherits from Animation and override Updateframe. Then put ms variable at the time you want to pass between frames. Of course remove all Adafruit and animation references if you just want a task based lib.

class TaskA : public Task
{
public:
  TaskA() { ms = 2000; }

  bool UpdateFrame()
  {    
    Serial.println("2 seconds passed");
    
    return true;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  }
  
};

TaskA t;

void setup()
{
  list->AddTask(&t);
}

void loop()
{
  list->UpdateTasks();
}
Related