Skip to content

Instantly share code, notes, and snippets.

@Pharap
Created June 3, 2018 15:19
Show Gist options
  • Save Pharap/974a8704d5c183cb6b269925e145e2b8 to your computer and use it in GitHub Desktop.
Save Pharap/974a8704d5c183cb6b269925e145e2b8 to your computer and use it in GitHub Desktop.
A demonstration of delta timing on Arduboy
#include <Arduboy2.h>
Arduboy2 arduboy;
unsigned long previousTime = 0;
uint8_t frameRate = 60;
void setup()
{
arduboy.begin();
arduboy.setFrameRate(frameRate);
previousTime = millis();
}
float x = WIDTH * 0.5f;
float y = HEIGHT * 0.5f;
constexpr uint8_t pixelsPerSecond = 15;
bool displayInfo = true;
void loop()
{
if(!arduboy.nextFrame())
return;
unsigned long currentTime = millis();
unsigned long deltaTime = currentTime - previousTime;
previousTime = currentTime;
float deltaSeconds = deltaTime * (1.0f / 1000.0f);
arduboy.pollButtons();
if(arduboy.pressed(A_BUTTON))
{
if(arduboy.justPressed(UP_BUTTON))
if(frameRate < 255)
{
frameRate += 15;
arduboy.setFrameRate(frameRate);
}
if(arduboy.justPressed(DOWN_BUTTON))
if(frameRate > 0)
{
frameRate -= 15;
arduboy.setFrameRate(frameRate);
}
}
else
{
if(arduboy.pressed(RIGHT_BUTTON))
x += (pixelsPerSecond * deltaSeconds);
if(arduboy.pressed(LEFT_BUTTON))
x -= (pixelsPerSecond * deltaSeconds);
if(arduboy.pressed(DOWN_BUTTON))
y += (pixelsPerSecond * deltaSeconds);
if(arduboy.pressed(UP_BUTTON))
y -= (pixelsPerSecond * deltaSeconds);
}
if(arduboy.justPressed(B_BUTTON))
displayInfo = !displayInfo;
arduboy.clear();
if(displayInfo)
{
arduboy.println(frameRate);
arduboy.println(deltaTime);
arduboy.println(deltaSeconds, 4);
arduboy.println(x, 4);
arduboy.println(y, 4);
}
arduboy.setCursor(static_cast<uint8_t>(x), static_cast<uint8_t>(y));
arduboy.print('@');
arduboy.display();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment