Make Your Own Arduboy Game: Part 3 - Storing Data & Loops


(Holmes) #1

This is Part 3 in a series on learning how to program your own Arduboy game. If you have skipped the previous parts, please read over Part 1 and Part 2.

Variables

Computers work a lot with calculations and data. To make most video games, you’re going to need to be able to store data, like high scores, or player location, or lives remaining. In order to remember data, a computer must set aside some memory to put that data into. Then, the computer can be told to change the data stored there.

Loops

Remember how I said that computers have to be given specific instructions? Ever notice how the back of shampoo bottles say to 1. Lather, 2. Rinse, 3. Repeat? If a computer were given those instructions, they would be stuck in an infinite loop of lathering, rinsing, and repeating. In programming, having instructions repeat in a loop can be very useful. I’ll show you how to use the loop() function to do this.

Getting Started

In this program, we’re going to make the Arduboy keep track of a number and display it to you as it is increased.

Let’s do it! Grab the code from the previous tutorial so that we can build off of it:

//Jonathan Holmes (crait)
//October 18th, 2016
//Printing Text

#include <Arduboy.h>
Arduboy arduboy;

void setup() {
  // put your setup code here, to run once:
  arduboy.begin();
  arduboy.clear();
  arduboy.print("Holmes is cool!");
  arduboy.display();
}

void loop() {
  // put your main code here, to run repeatedly:

}

Initialization

Whenever you create a variable, you have to initialize it, which is setting aside memory for the data and giving it a name. You’ve already done this but didn’t realize it. Check out the Arduboy arduboy; line. You initialized an object called arduboy. Objects are a lil’ more complex than I want to get into during this tutorial, but there are different kinds of variables that you can initialize. Here are some: Integers, booleans, characters, objects, doubles, and many more.

We’ll initialize our number that’s going to increase as an integer. This basically means that it’s a whole number, without fractions. Integers will appear as int inside of C++ code.

To initialize a variable, you must put 3 things: The type of variable, the name of the variable, then a semi-colon. Let’s call our variable counter. Here’s the code: int counter; Put it under the Arduboy arduboy; line.

Assignment

Whenever you create a variable, you can give it a value. This is called assignment. We don’t know what counter's value is because we never gave it one.

Let’s clean up the code by removing the arduboy.print(); and arduboy.display() functions from setup(). Instead, let’s put the assignment there:

counter = 0;

This line of code is saying that counter's value is now equal to 0. Instead of 0, you could put another number, a mathematical formula, or some other things that I’ll explain below.

Here’s what your code should look like:

//Jonathan Holmes (crait)
//October 18th, 2016
//Counter

#include <Arduboy.h>
Arduboy arduboy;

int counter;

void setup() {
  // put your setup code here, to run once:
  arduboy.begin();
  arduboy.clear();
  counter = 0;
}

void loop() {
  // put your main code here, to run repeatedly:

}

Incrementing

Okay, our program will be repeating a few simple instructions over and over again. Basically, we’ll change the value of counter, then display the value of counter, then repeat. To do this, we should add some code into the loop() function.

counter = counter + 1;

This line of code means that you are assigning the value of counter to itself plus 1. Or, in other words, counter's value is now equal to the value of counter + 1.

Displaying The Variable’s Value

Now that we have the variable increasing, let’s display it! Remember how we used arduboy.print() to print some text to the screen? Well, we can use that same function to display numbers to the screen, too. Add arduboy.print(counter); .

If you were to run this code as it is, now, then the Arduboy’s screen would fill up with numbers. If we’re going to print something new, we need to be sure to erase what was previously on the screen. We need to add in arduboy.clear(); at the beginning of loop() and arduboy.display(); at the end to display the updated screen.

Have you ever used a type writer? Whenever you type letters, the cursor moves over. The arduboy.print() function works similarly. Every time you use the arduboy.print() function, it moves the cursor over. So we need to reset the cursor to the top of the screen with arduboy.setCursor(0, 0); . I will explain this more in a later tutorial, but just throw that at the top of the loop().

The Completed Code

I’ve added in some comments, but your code should look like the following:

//Jonathan Holmes (crait)
//October 18th, 2016
//Printing Text

//Include the Arduboy Library
#include <Arduboy.h>
//Initialize the arduboy object
Arduboy arduboy;
//Initialize our counter variable
int counter;
//The setup() function runs once when you turn your Arduboy on
void setup() {
  //Start the Arduboy properly and display the Arduboy logo
  arduboy.begin();
  //Get rid of the Arduboy logo and clear the screen
  arduboy.clear();
  //Assign our counter variable to be equal to 0
  counter = 0;
}
//The loop() function repeats forever after setup() is done
void loop() {
  //Clear whatever is printed on the screen
  arduboy.clear();
  //Move the cursor back to the top-left of the screen
  arduboy.setCursor(0, 0);
  //Increase counter's value by 1
  counter = counter + 1;
  //Print out the value of counter
  arduboy.print(counter);
  //Refresh the screen to show whatever's printed to it
  arduboy.display();
}

Running The Code

Connect your Arduboy to your computer and upload this code to it. Your Arduboy should start counting up!

What’s Next?

In the next tutorial, we’ll be learning about testing the values of variables, booleans, and pressing buttons! Click here to go to Part 4!

Credits

I wrote this tutorial in order to give back to the programming community that taught me to get into it about 10 years ago. If you’d like to follow me on Twitter, please do so at http://www.twitter.com/crait . I’d greatly appreciate that. :smile:


Make Your Own Arduboy Game: Part 4 - Questions & Button Input
Make Your Own Arduboy Game: Part 5 - Your First Game!
Make Your Own Arduboy Game: Part 7 - Make Pong From Scratch!
How do I get enemy's to chase?
How Easy Are These To Program
Make Your Own Arduboy Game: Part 9 - Mapping DinoSmasher
Make Your Own Arduboy Game: Part 8 - Starting DinoSmasher
Make Your Own Arduboy Game: Part 6 - Graphics!
(John Saunders) #2

Very cool of you to make these man. I only skimmed them, but the instructions seem easy to follow and concise. Cant wait to give your lessons a shot over the weekend


(Holmes) #3

I have a lil’ problem with making things too wordy for some people, but I try to emphasize information that’ll be important in the future tutorials. :smiley:

Sweet! I’m going to try to get 4-5 more tutorials made by then! :smiley:


(John Saunders) #4

Awesome man. Weve needed something like this for awhile. Reading books you dont understand just doesnt cut it when theres no examples and no one to answer your questions.


#5

I have a question about the counter = counter + 1; code, can i use counter ++; instead?
if yes are there any benefits of doing that?

Btw thanks for an excellent guide you explain everything nice and clear.


(Simon) #6

Same, same, only different.

You can even use ++counter;

You will find links saying the ++ is faster and that may be true on some compilers. A quick test using the Arduino IDE shows that they all compile to exactly the same size.

Edit: Someone wil ljump in and talk about how the i = I + 1 and the I++ both require a temproary variable to store the value in before reassigning and that the ++i should therefore be the fastest. As my quick test proved to me, the C++ compiler that the Arduino IDE uses is smart enough to optimise them all to the same code or at least equivalent code of the same size.


#7

Thank you for your reply. Helped me to understand the concept of a bit more :slight_smile:


#8

Easy enough, but I wish there were some “what if” scenarios in here.

What if I get this error, what if I uploaded without adding all the lines, what happens if I let the counter run. At this early stage I’m a little concerned about breaking things and want to be reassured I won’t mess something up.


(Pharap) #9

It’s pretty hard to properly brick the Arduboy.

Occaisionally the Arduboy goes into a state where ‘flashlight mode’ or the rest button are needed to upload a new sketch, but that’s pretty easy to manage.

There’s been the odd case of the bootloader being overwritten because the bootloader protection pins weren’t set right in the factory, but even that is fixable with the right tools and only affects certain batches of Arduboys (mainly older ones).

I have yet to hear of an Arduboy being broken beyond repair due to badly written software.
(It’s not impossible, but it’s highly unlikely.)