Arduboy Buttons Cleanup

I noticed in the example that the Buttons are treated as pins being activated.
It would be great if there were a function for this, like so:

void loop() {
  if(Arduboy.button(LEFT, true)) {
    display.print("Pressing Left!")
  }
}

I feel it would help beginners by giving them a easy to use environment. :smile:

The way the buttons are handled in the example is actually pretty common on simpler platforms like the NES and Game Boy. You could code a nice shorthand like that yourself, but every extra step like that, both in written code and time to execute, is going to add up on a low power platform like this. The more “raw” you’re willing to do it, the better, I’d say.

If you want to make it look nicer, you could probably do something like this (not sure if the code will work perfectly as written without testing it, I’m rusty):

//in declarations at the top
#define B_BUTTON 1
#define A_BUTTON 2
#define DOWN 4
#define RIGHT 8
#define UP 16
#define LEFT 32

void loop() {
  uint8t buttons = display.getInput();

  if(buttons & LEFT) {
    display.print("Pressing Left!");
  }
  //etc.
}

buttons contains a single byte consisting of the current state of all the buttons. Left on the d-pad is represented by the 6th bit from the right, which seen in binary is b00100000 which is 32, which is why LEFT is defined as 32. Doing a logical AND between buttons and LEFT checks whether buttons contains a 1 in the sixth “slot,” to see if left is being pressed.

2 Likes

I tried your code on the Dev kit.
Started new Arduino sketch, put the declarations in the void Setup() function. Put the test in the normal void loop() function. So my final code ended up looking like:

void setup() {
    #define B_BUTTON 1
    #define A_BUTTON 2
    #define DOWN 4
    #define RIGHT 8
    #define UP 16
    #define LEFT 32
}

void loop() {
    uint8t buttons = display.getInput();
    
    if(buttons & LEFT) {
      display.print("Pressing Left!");
    }
}

I get an error:

testsketch:14: error: 'display' was not declared in this scope
'uint8t' was not declared in this scope.

Don’t forget to load the Arduboy.h header and initialize an Arduboy display object which holds the instructions for display.

#include "Arduboy.h"

#define B_BUTTON 1
#define A_BUTTON 2
#define DOWN 4
#define RIGHT 8
#define UP 16
#define LEFT 32

Arduboy display;

void loop() {
    uint8_t buttons = display.getInput();
    
    if(buttons & LEFT) {
      display.print("Pressing Left!");
    }
}
1 Like

Looks like it needs to be

uint8_t buttons = display.getInput();

All I’m doing is declaring a one byte variable to receive a single byte of controller data.

And you will need to include arduboy.h and arduboy.cpp with the project, because that is what sets up the display scope.

So all projects should include arduboy.cpp and arduboy.h at the start?

For now, yes. They include the functions that allow you to draw and write to the screen. I covered them here: Reference Page for Development

You will need to get them from here, and then include them the same way the Breakout example game does, as well as call the initial screen setup function: https://github.com/Arduboy/ArduBreakout

This is a great start, we are going to build a full on tutorial how to get you started with the Arduboy. It will start assuming you’ve never used programming languages before and finish you off that you can write text and draw images to the screen and move them around with the buttons.

You’ll be able to jump in at whatever level you are comfortable at. Examples like this will be perfect for the page so thanks so much for helping out while I’ve been away running the kickstarter campaign!

I got a basic Hello World program working, this is the minimum code I could get it to work with.

#include <SPI.h>
#include "Arduboy.h"

Arduboy display;

void setup()
{
    SPI.begin();
    display.start();
    display.setTextSize(1);
    display.setCursor(0, 0);
    display.print("1.0.0.4");
    display.display();
    delay(1000);

}

void loop()
{
    display.clearDisplay();
    display.setCursor(30,30);
    display.print("Hello World");
    display.display();
}

I made a GitHub repository for the code in case other people want to easily grab it: https://github.com/albertfilice/Arduboy-Hello-World

This is on the list of things I’ve added to the API. I’ll push a pull request in the next few days. I went with this:

if (arduboy.pressed(LEFT_BUTTON + RIGHT_BUTTON))

You pass in a bit mask of buttons and it returns true/false depending on whether all the buttons you requested are pressed at the current moment. You could also use | above but since the constants are bits it works just as easy to add them.

I hadn’t considered “negative” press queries… is that needed? IE, not_pressed?

1 Like