Skip to content

Commit

Permalink
library version of this code + example
Browse files Browse the repository at this point in the history
  • Loading branch information
LCobeaga authored Nov 20, 2020
1 parent 17c270a commit 98f2c7e
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
113 changes: 113 additions & 0 deletions AmmoCounter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//AmmoCounter

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <JC_Button.h>
#include <Arduino.h>
#include <AmmoCounter.h>

/*----------------------------------------------------------------------*
* nerf-ammo-counter *
* Monty Choy, 2017 *
* converted to library by Luca Cobeaga *
* *
* Arduino sketch for ammo counters in NERF blasters *
* Be sure to check out the links: *
* Youtube Video: https://goo.gl/KjQfdK *
* Tutorial: https://goo.gl/8JSXbc *
*----------------------------------------------------------------------*/

byte MSA[11] = {5, 6, 10, 12, 15, 18, 20, 22, 25, 35, 0}; //keep track of the magazine sizes

AmmoCounter::AmmoCounter( int TRIGGER_BTN_PIN, int RELOAD_BTN_PIN, int MAG_SZ_TOG_BTN_PIN) {
_oled_reset = 4;
_invert = true;
_pullup = true;
_debounce = 20;

display = new Adafruit_SSD1306(_oled_reset); //display

triggerBtn = new Button(TRIGGER_BTN_PIN, _debounce, _pullup, _invert); //trigger button
reloadBtn = new Button(RELOAD_BTN_PIN, _debounce, _pullup, _invert); //reloading button
magSzTogBtn = new Button(MAG_SZ_TOG_BTN_PIN, _debounce, _pullup, _invert); //magazine size toggle button

//stuff to help keep track of magazine stuff
//if you want to add/remove magazine sizes, do that here. Remember, they go in order when you toggle between them, from left to right in this.
//
//when you change the value, the "currentMagSize"(several lines below) has to be less than the number of different magazines sizes,
//the number of different magazine size values in the "magSizeArr" you can change "currentMagSize" to whatever you want.
//When it program first starts, when the microcontroller turns on, the 5th element of "magSizeArr" is the current magazine size,
//starting from 0. 0 is used for count-up mode.
//Ex: byte array = {1(0), 2(1), 3(2), 4(3)} - the numbers without parenthesis are the values this array/list
//stores, and the number between the parenthesis indicates which place they are, their "index", where they are in the list/array.
//If I want to access the value 1, which is the first value of the array/list, which the computer sees as the
//"zeroith" value, I would do array[0]. If I want to access the value 3, the third value of the array, I would do array[2]

magSizeArr = MSA;
currentMagSize = 0; //keep track of the current magazine size
currentAmmo = magSizeArr[currentMagSize]; //keep track of how much ammo there currently is
maxAmmo = magSizeArr[currentMagSize]; //keep track of what the max ammo is, for use when reloading
}



void AmmoCounter::begin (int ts, int r, int h, int w) {
display->begin(SSD1306_SWITCHCAPVCC, 0x3C); //begin stuff for the display
TEXT_SIZE = ts;
ROTATION = r;
HEIGHT = r;
WIDTH = w;
initDisplayAmmo(); //show the ammo
}

//actually dispaly ammo onto screen
void AmmoCounter::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
display->setRotation(ROTATION);
display->setCursor(HEIGHT , WIDTH); //center text
display->print(ammoToDisplay); //print the text
display->display(); //display the text
}

//set up ammo to be displayed
void AmmoCounter::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
}

void AmmoCounter::countAmmo () {
triggerBtn->read(); //read trigger button
if (triggerBtn->wasPressed()) { //trigger button pressed
if (maxAmmo != 0 && currentAmmo < 99 && currentAmmo > 0) { //make sure that the ammo is less than 99 so it doesnt overflow the display and not in count-up mode
currentAmmo--; //increment ammo
} else if (maxAmmo == 0 && currentAmmo < 99) { //make sure that the ammo is more than 0 so no negative numbers are displayed and in count-up mode
currentAmmo++; //decrement ammo
}
initDisplayAmmo(); //display ammo
}
}

void AmmoCounter::reload () {
reloadBtn->read(); //read reload button
if (reloadBtn->wasPressed()) { //reload button pressed
currentAmmo = maxAmmo; //reset ammo
initDisplayAmmo(); //display ammo
}
}

void AmmoCounter::toggleMags () {
magSzTogBtn->read(); //read magazine size toggle button
if (magSzTogBtn->wasPressed()) { //magazine size toggle button pressed
//cycle through mag sizes based on array, and make sure array doens't overflow
currentMagSize = (currentMagSize < (sizeof(MSA)/sizeof(MSA[0]) - 1)) ? currentMagSize + 1 : 0;

//there's a new max ammo, because there's a new magazine size
maxAmmo = magSizeArr[currentMagSize];
currentAmmo = maxAmmo;

initDisplayAmmo(); //display ammo
}
}
52 changes: 52 additions & 0 deletions AmmoCounter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// LCD nerf ammo counter

// Luca Cobeaga's conversion of Monty Choy's ammo counter code to a library

#ifndef AmmoCounter_h
#define AmmoCounter_h

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <JC_Button.h>
#include <arduino.h>

class AmmoCounter
{
public:
AmmoCounter(int TRIGGER_BTN_PIN, int RELOAD_BTN_PIN, int MAG_SZ_TOG_BTN_PIN);

void begin (int TEXT_SIZE, int ROTATION, int HEIGHT, int WIDTH);
void countAmmo ();
void reload ();
void toggleMags ();

int TRIGGER_BTN_PIN;
int RELOAD_BTN_PIN;
int MAG_SZ_TOG_BTN_PIN;

int TEXT_SIZE;
int ROTATION;
int HEIGHT;
int WIDTH;
private:
int _oled_reset;
bool _invert;
bool _pullup;
int _debounce;

Adafruit_SSD1306 *display; //display

Button *triggerBtn; //trigger button
Button *reloadBtn; //reloading button
Button *magSzTogBtn; //magazine size toggle button

void displayAmmo (String ammoToDisplay);
void initDisplayAmmo ();

byte currentMagSize;
byte currentAmmo;
byte maxAmmo;
byte *magSizeArr;
};

#endif
31 changes: 31 additions & 0 deletions ShadoW_Price_Firing_Code.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@


// Shadow Price Firing & ammo counter code

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <JC_Button.h>
#include <AmmoCounter.h>

//pins for button
#define TRIGGER_BTN_PIN 0
#define RELOAD_BTN_PIN 1
#define MAG_SZ_TOG_BTN_PIN 4

#define TEXT_SIZE 6 // multiplier on text size
#define ROTATION 1 // rotation of text i.e. 0 = 0deg, 1 = 90deg, 2 = 180deg, 3 = 270deg
#define WIDTH 0 // how far to move from long edge (128 pixel edge)
#define HEIGHT -32 // fow far from edge with wires (32 pixel edge)

AmmoCounter AC = AmmoCounter(TRIGGER_BTN_PIN, RELOAD_BTN_PIN, MAG_SZ_TOG_BTN_PIN);

void setup() {
AC.begin(TEXT_SIZE, ROTATION, WIDTH, HEIGHT);
}

void loop() {
// put your main code here, to run repeatedly:
AC.countAmmo(); //count ammo, constantly check for the trigger switch to be pressed to count
AC.reload(); //reload, constantly check for the magazine switch to be pressed/not pressed
AC.toggleMags(); //toggle between magazine sizes, constanly check for the magazine toggle switch to be pressed
}

0 comments on commit 98f2c7e

Please sign in to comment.