I've modified my code to use unsigned longs for all time variables and calculations, just casting to a float when I apply deltaTime to my character. This all works correctly and I think it solves my error in using floats.
#include <Arduboy.h>
Arduboy arduboy;
const unsigned char background[] PROGMEM = {0x81, 0x00, 0x12, 0x40, 0x4, 0x11, 0x00, 0x4,};
const unsigned char player[] PROGMEM = {0x00, 0x00, 0x00, 0x00, 0x34, 0xfc, 0x8f, 0x34, 0x6, 0x36, 0x8e, 0x94, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2, 0x1e, 0xe6, 0xa3, 0xda, 0x83, 0xc4, 0xb8, 0x00, 0x00, 0x00, 0x00,};
bool aBuffer;
bool showBG;
float playerX;
float playerY;
unsigned long oldTime = 0;
void setup() {
arduboy.begin();
arduboy.clear();
ResetGame();
}
void loop() {
unsigned long deltaTime = CalculateDeltaTime();
arduboy.clear();
// Draw Background.
if (showBG) {
for (int i = 0; i < 128; i += 8) {
for (int j = 0; j < 64; j += 8) {
arduboy.drawBitmap(i, j, background, 8, 8, WHITE);
}
}
}
// Draw player character.
arduboy.fillRect(playerX + 4, playerY, 8, 16, BLACK);
arduboy.drawBitmap(playerX, playerY, player, 16, 16, WHITE);
if (arduboy.pressed(LEFT_BUTTON))
playerX -= (float)deltaTime / 8;
if (arduboy.pressed(RIGHT_BUTTON))
playerX += (float)deltaTime / 8;
if (arduboy.pressed(UP_BUTTON))
playerY -= (float)deltaTime / 8;
if (arduboy.pressed(DOWN_BUTTON))
playerY += (float)deltaTime / 8;
if (arduboy.pressed(A_BUTTON) and !aBuffer) {
showBG = !showBG;
aBuffer = true;
}
if (arduboy.pressed(A_BUTTON) and arduboy.pressed(B_BUTTON))
ResetGame();
if (arduboy.notPressed(A_BUTTON))
aBuffer = false;
arduboy.display();
}
void ResetGame()
{
aBuffer = false;
showBG = true;
playerX = 5;
playerY = 10;
return;
}
unsigned long CalculateDeltaTime(){
unsigned long currentTime = millis();
unsigned long deltaTime = currentTime - oldTime;
oldTime = currentTime;
return deltaTime;
}