Help with code?

I’m trying to load a program I made onto the arduboy, but it just stays stuck on the arduboy screen when I upload it. Any ideas? I do have arduboy.begin(); and arduboy.clear(); at the start of setup.

Without seeing any code It’s hard to tell what’s wrong. As a base you need someting like this:

#include <Arduboy2.h>

void setup()
 {
  arduboy.begin();
}

void loop()
 {
  if (!arduboy.nextFrame()) return;

 //your code here

  arduboy.display(CLEAR_BUFFER);
}
Here's a mini example sketch
//Using Arduboy2 library mini example

#include <Arduboy2.h> // include Arduboy2 library
#include <ab_logo.c>  // include Arduboy logo graphic

//constant values
constexpr uint8_t AB_LOGO_WIDTH = 88; 
constexpr uint8_t AB_LOGO_HEIGHT = 16;

Arduboy2 arduboy;

//assign values;
int16_t x    = (WIDTH - AB_LOGO_WIDTH) / 2;
int16_t y    = 25;
int8_t  xDir = 1;
int8_t  yDir = 1;

void setup() {
  arduboy.begin(); //initialisation with startup logo
  //arduboy.setFrameRate(60); //Only needed when frameRate != 60
}

void loop() {
  if (!arduboy.nextFrame() ) return; // Do nothing until it's time for the next frame

  //program code
  arduboy.drawBitmap(x, y, arduboy_logo, AB_LOGO_WIDTH, AB_LOGO_HEIGHT); 
  x += xDir;
  y += yDir;
  if (x == 0 || x == WIDTH - AB_LOGO_WIDTH) xDir = -xDir;
  if (y == 0 || y == HEIGHT - AB_LOGO_HEIGHT) yDir = -yDir;
  
  arduboy.display(CLEAR_BUFFER); //clears display after displaying display buffer
}

It worked! Thank you! And one more question, how do I stop text from instantly disappearing?

usually you redraw everything you want to see on the display for each frame.

Should be (!arduboy.nextFrame()).
arduboy.nextFrame() returns bool, not int.

You shouldn’t include .c files,
aside from the fact they’re supposed to be C and not C++,
you could end up with multiple definition errors.

Though I admit, the Arduboy2 library needs a better way of granting access to the logo(s).
Either by changing ab_logo.c to a header file,
or by introducing a correpsonding header file.


Draw it in loop.

When loop ends, it (almost) immediately starts again.

Here’s a “Hello World” program:

#include <Arduboy2.h>

Arduboy2 arduboy;

void setup(void)
{
	arduboy.begin();
}

void loop(void)
{
	if(!arduboy.nextFrame())
		return;

	arduboy.pollButtons();

	arduboy.clear();
	arduboy.println(F("Hello World"));
	arduboy.display();
}

Oops your right, fixed for future reference

It’s the only way. But I agree, it should have been an header file instead. But even Arduino gives a a bad example with their wiring.c

1 Like

Now It’s saying it can’t compile it for the arduboy. Here’s the attempted code :P…

#include <Arduboy2.h>
#include <ab_logo.c>
Arduboy2 arduboy;
constexpr uint8_t AB_LOGO_WIDTH = 88;
constexpr uint8_t AB_LOGO_HEIGHT = 16;
//integers
int playerHP;
int playerXP;
int swords[7] = {1, 2, 3, 4, 5, 6, 7};
int armor[7] = {1, 2, 3, 4, 5, 6, 7};
void setup() {
  arduboy.begin();
  arduboy.clear();
  //starter stats
  playerHP = 20;
  playerXP = 0;
  //swords
  swords[1] = "Rusty Shortsword";
  swords[2] = "Iron Shortsword";
  swords[3] = "Iron Longsword";
  swords[4] = "Elvish Shortsword";
  swords[5] = "Elvish Longsword";
  swords[6] = "Enchanted Shortsword";
  swords[7] = "Emerald Greatsword";  
  //armor
  armor[1] = "Common Garb";
  armor[2] = "Leather Tunic";
  armor[3] = "Chainmail";
  armor[4] = "Iron Chestplate";
  armor[5] = "Elvish Chestplate";
  armor[5] = "Dwarvish Chestplate";
  armor[6] = "Enchanted Chestplate";
  armor[7] = "Dragon Scales";
}
void loop() {
  if (!arduboy.nextFrame()) 
  return;
  arduboy.clear();  
  if( arduboy.pressed(B_BUTTON) == true ) {
    arduboy.print(F("Level:" + playerXP ));
    arduboy.print(F("\n"));
    arduboy.print(F("Sword:" + swords[1] ));
    arduboy.print(F("\n"));
    arduboy.print(F("Armor:" + armor[1] ));
    arduboy.print(F("\n"));
    arduboy.print(F("HP:" + playerHP ));
    arduboy.display();
  }
}

@alt_f,

I’ve formatted the code that you posted by enclosing it as a code block. In the future, please do so yourself:

Put a line with three backticks and cpp before the first line of your code.
Put a line with three backticks after the last line of your code:

```cpp
Your code goes here.
```

On a standard US keyboard, the backtick is on the key at the top left, below the ESC key.

Sorry, my bad! Thank you!

I’m disappointed, but not suprised.

You’re trying to store const char *s in an array of int, you can’t do that.
Data types exist for a good reason.

Also array indices start at 0, so swords[0] is the first item in the array and sword[6] is the last item

You need to do something like this:

const char * sword0 = "Rusty Shortsword";
const char * sword1 = "Iron Shortsword";
const char * sword2 = "Iron Longsword";
const char * sword3 = "Elvish Shortsword";
const char * sword4 = "Elvish Longsword";
const char * sword5 = "Enchanted Shortsword";
const char * sword6 = "Emerald Greatsword";

const char * swords[7]
{
	sword0,
	sword1,
	sword2,
	sword3,
	sword4,
	sword5,
	sword6,
};

But that’s going to eat into RAM quite significantly.

Ideally you should store your strings in progmem,
but printing arrays from progmem is a bit tricky.
You need to convert to a const __FlashStringHelper *.

Fortunately I made a helper function for this:

Have a look at this demo and read the comments:

Your final code will be something more like:

If there’s anything you don’t understand or want explained, ask away.

Okay, thank you. I will take a look at it.

1 Like