How can I make text flash?


(Sonic) #1

I’m making a game and I want some of the text in the title screen to turn on and off(flash) continuously, like in the ardubreakout’s title screen. I tried looking at the source code, but I couldn’t figure it out.
Please help!! I would appreciate some explanations too, so I can change the values as I want.


(Josh Goebel) #2

Draw the text for frames 0-20, erase the text for frames 20-40, repeat, etc.


(Sonic) #3

O…k…
So can you please elaborate how to do that?? I’m such a noob.


(Holmes) #4

You need to create a variable that can keep track of how long the text is displayed and how long it hasn’t been displayed. While that variable is less than a certain number, show the text. When it is bigger than that number, do not show the text. When the variable is large enough, reset it to 0. I will give some example code:

Create a global variable and set it to 0.

int counter = 0;

Now, in your main loop() area…

counter = counter + 1; //Increase counter variable
if(counter < 60) {
arduboy.setCursor(0, 0);
arduboy.print("Text"); //If the counter is less than 60, show the text
}
if(counter >= 120) {
counter = 0; //If counter is greater than or less than 120, reset it back to 0
}

(Sonic) #5

Thanks for your help!!
I started thinking of making a game after reading your guide!!!
I loved it!!
By the way, is your guide series over??
The most recent one I could find was making the ping pong game from scratch…


(Holmes) #6

Nope! I have just been busy with a lot of things lately! :sweat_smile:


(Mike McRoberts) #7

Another method for doing this would be to use timers. That way you have accurate on/off times. e.g. if you wanted the text to flash on and off every 1/2 second, which is 500 milliseconds you would do this…

Create a global variable for storing the current value in millis(); and create a boolean value that can be toggled:

int flashTime;
boolean flashOrNot = 1;

If you don’t know what millis() is :- Inside your Arduboy is a timer that starts to count up in milliseconds (it’s actually microseconds) when you turn it on. So 60 seconds after turning your Arduboy on, the millis() timer will have the value 60,000 stored in it. By accessing the timer and storing the value you can compare the current time with a time in the past to see how long has passed since that event occurred.

https://www.arduino.cc/en/reference/millis

Then in your Setup() function save the current value of millis() to the variable:

void setup()
{
    // store the current millis timer value
    flashTime = millis();
 }

Then in your main loop only show the text if flashOrNot == 1. Then every 500 milliseconds toggle the value of flashOrNot:

arduboy.setCursor(0, 0);
if (flashOrNot == 1) arduboy.print("Text");
if ((millis() - flashTime) > 500
{
       // toggle flashOrNot so if 1 it becomes 0 and if 0 it becomes 1
       flashOrNot = !flashOrNot;
        // store the value in millis() again
        flashTime = millis();
}

This way you know the text is going on and off exactly every 1/2 second (or whatever value you put into the above expression).


(Mike McRoberts) #8

Please note that the millis timer is a 4 byte number (unsigned long) so can store values from 0 to 4,294,967,295. This gives you the ability to time events for up to approx 50 days. As in my code above I am only interested in things that have happened within the last 500 milliseconds I have used an int (to save memory as an int is 2 bytes). An int (integer) can store a maximum value of 32,767 only. Don’t use an int if you wish to time things more than 32 seconds ago as it won’t work.


(Celine) #9

follow up question on text flashing… Technically it could work with images too right, I would just have to switch the arduboy.print(“Text”) to arduboy.drawBitmap?


(Mike McRoberts) #10

Correct.