How do you replace the delay() function?

Understanding the language, error messages, etc.

How do you replace the delay() function?

Postby yawn » 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!
yawn
 
Posts: 32
Joined: Tue Dec 23, 2014 1:54 pm

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

Postby Myndale » 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: Select all
   // 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);
    }
Myndale
 
Posts: 507
Joined: Sat Mar 01, 2014 1:25 am

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

Postby DelphiMarkus » 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: Select all
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.
User avatar
DelphiMarkus
 
Posts: 20
Joined: Sun May 04, 2014 7:59 pm


Return to Programming Questions

Who is online

Users browsing this forum: No registered users and 14 guests

cron