Search code examples
c++arduinoribbon-control

delay function on Arduino doesn't behave as expected


I am working with an addressable ribbon with Arduino. The point is to light up different parts of my ribbon at different moment. To do this, I used the function delay as below:

void un_a()  //first third of ribbon length
{      
  for (uint16_t i = 0; i < N; i++) {
  strip.setPixelColor(i, strip.Color(100,255,100));
  }
  strip.show();
}

void deux_a()    //second third of ribbon length
{
  for (uint16_t i = N; i < 2*N; i++) {
    strip.setPixelColor(i, strip.Color(100,255,100));
  }
  strip.show();
}

void trois_a()    //last third of ribbon length
{
  for (uint16_t i = 2*N; i < 3*N; i++) {
    strip.setPixelColor(i, strip.Color(100,255,100));
  }
  strip.show();
}

void wave(){
  void un_a();
  delay(2000);
  void deux_a();
  delay(2000);
  void trois_a();
}

So when wave() is called, the expected behavior is:

  • 1/3 lights up,
  • after +-2s the 2/3 lights up as well,
  • after +-2s, the last third lights up.

In reality, it just blocks and lights a part of the 1st third.
I went around again and again, I don't see what I am missing. Any clue?


Solution

  • void un_a();
    

    This is a function declaration. It tells, that such symbol un_a exists, and it is a function of type void (*)().
    If you want to call a function, you use a expresssion statement. Notice it does not have a returned type on the beginning as in a declaration:

    int a; // declaration
    a = 1; // statement
    un_a(); // statement, this executed the un_a function
    1 + 1; // another statement, this adds 1 + 1
    int func(int b); // declaration, this does nothing, just the compiler knows that a function `func` exists
    int (*(*func2)(int a, int (*(*)(int arr[a]))[a]))[5]; // another declaration
    (void)func2(5, (int (* (*)(int *))[5])0); // statement
    

    Try calling the functions:

    void wave(){
      un_a();
      delay(2000);
      deux_a();
      delay(2000);
      trois_a();
    }