Understanding the language, error messages, etc.
Post a reply

How do you replace the delay() function?

Tue Dec 30, 2014 5:02 pm

Hi, it's all in the title!

How do you proceed if for instance you want to display a message or image on the screen for 1 second? I tried with frameCount without success :/

Thanks!

Re: How do you replace the delay() function?

Tue Dec 30, 2014 8:59 pm

Is there any particular reason you don't want to use delay()? All you have to do is call the relevant functions to clear the screen, draw your image and display it on-screen before calling your delay function. Something like this in your loop() function will work:

Code:
   // wait for A button to be pressed
   if (gb.buttons.pressed(BTN_A))
    {
      // draw image and wait, I'm using println here but drawBitmap will work just as well.
      gb.display.clear();
      gb.display.println(F("This shows\nfor 1 second"));
      gb.display.update();
      delay(1000);
    }

Re: How do you replace the delay() function?

Wed Dec 31, 2014 1:44 pm

Hi!

You can take a look at a previous thread about Setting up a timer.

If you don't want to use gb.frameCount or delay() you can use a "manual" timer. You could use something like the following if you only want to display a message and do nothing else in the meantime:
Code:
void show_a_message()
{
    bool exit = false;
    byte timer = 0;

    // loop while we want to show the message
    while (!exit)
    {
        if (gb.update())
        {
            // increment counter every frame (therefore: inside "if (gb.update())" )
            timer++;
           
            // if message is displayed for 20 frames: exit loop
            // how long a frame takes depends on the frame rate
            if (timer > 20)
            {
                timer = 0; // unnecessary in this example, but maybe useful in others
                exit = true; // in this example a simple "break;" would do it as well
            }
           
            // display message
            gb.display.print(F("A message."));
        }
    }
}

By default there are 20 frames drawn per second, therefore this loop will display the message for one second. However you can change the frame rate by using gb.setFrameRate, but you probably won't need that.
The technique of counting frames can also be used in many other situations, e.g. to animate something.
Post a reply