Skip to content

Commit

Permalink
functions initDisplayAmmo and displayAmmo for displaying ammo
Browse files Browse the repository at this point in the history
  • Loading branch information
mochoy committed Oct 12, 2017
1 parent 3c1061c commit a6dd667
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions nerf-ammo-counter/nerf-ammo-counter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

//initialize stuff for display
#define OLED_RESET 4
#define TEXT_SIZE 8

//pins for button
#define TRIGGER_BTN_PIN 1
Expand Down Expand Up @@ -59,11 +60,8 @@ byte maxAmmo = magSizeArr[currentMagSize]; //keep track of what the max ammo

//this code will run when the Arduino turns on
void setup() {
//begin stuff for the display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

//show the ammo
displayAmmo();
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //begin stuff for the display
initDisplayAmmo(); //show the ammo
}

//this code loops many times a second
Expand All @@ -74,5 +72,20 @@ void loop() {
toggleMags();
//change magazine, constantly check for the magazine switch to be pressed/not pressed
changeMag();

}

void displayAmmo (String ammoToDisplay) {
display.clearDisplay(); //clear the display, so the stuff that was here before is no longer here
display.setTextSize(TEXT_SIZE); //set the size of the text
display.setTextColor(WHITE); //set the color of text text
//tell the display where to draw the text
display.setCursor( (SCREEN_WIDTH/2) - ((text.length()*2) * (textSize * 1.5)), (SCREEN_HEIGHT/2) - (textSize * 3) ); //center text
display.print(ammoToDisplay); //print the text
display.display(); //display the text
}

void initDisplayAmmo () {
//if the ammo to print, current ammo, is less that 10, make it like '01' or '04'
String ammoToDisplay = currentAmmo < 10 ? ("0" + (String)currentAmmo) : (String)currentAmmo;
displayAmmo(ammoToDisplay); //display the text, the ammo
}

0 comments on commit a6dd667

Please sign in to comment.