-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnerf_ammo_counter_oled_pro-mini.ino
133 lines (102 loc) · 2.81 KB
/
nerf_ammo_counter_oled_pro-mini.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <stdio.h>
#include <Arduino.h>
#include <Pushbutton.h>
#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#define OLED_ADDR 0x3C
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
Adafruit_SSD1306 display(-1);
#define STATUS_LED_PIN 13
#define SELECT_BUTTON_PIN 9
#define MAG_BUTTON_PIN 8
#define TRIGGER_BUTTON_PIN 7
Pushbutton trigger_btn(TRIGGER_BUTTON_PIN);
Pushbutton mag_btn(MAG_BUTTON_PIN, DEFAULT_STATE_HIGH);
Pushbutton select_btn(SELECT_BUTTON_PIN);
byte mag_sizes[] = {12, 30, 6};
byte mag_size_selected = 0;
byte ammo_max = mag_sizes[mag_size_selected];
byte ammo_current = ammo_max;
byte ammo_prev = ammo_current;
byte one_third_ammo = ammo_max / 3;
byte ammo_critical = one_third_ammo;
byte ammo_warning = one_third_ammo * 2;
void set_mag_size() {
Serial.println("Setting Mag Size");
if (mag_size_selected < (sizeof(mag_sizes)) -1) {
mag_size_selected++;
} else {
mag_size_selected = 0;
}
mag_size_selected = mag_size_selected++;
ammo_max = mag_sizes[mag_size_selected];
ammo_current = ammo_max;
ammo_prev = ammo_current;
one_third_ammo = ammo_max / 3;
ammo_critical = one_third_ammo;
ammo_warning = one_third_ammo * 2;
display_ammo();
}
void count_ammo_down() {
ammo_prev = ammo_current;
if (ammo_current > 0 ) {
ammo_current--;
} else {
ammo_current = 0;
}
Serial.print("Ammo: ");
Serial.println(ammo_current);
}
void reload() {
Serial.println("Reloading");
ammo_current = ammo_max;
ammo_prev = ammo_current;
display_ammo();
}
void display_ammo() {
String ammo_current_display = "";
display.clearDisplay();
display.setCursor(40,0);
if (ammo_current < 10 && ammo_current > 0) {
ammo_current_display = "0" + String(ammo_current);
} else if (ammo_current == 0) {
display.setCursor(5,0);
ammo_current_display = "EMPTY";
} else {
ammo_current_display = String(ammo_current);
}
display.println(ammo_current_display);
display.display();
ammo_prev = ammo_current;
}
void loop() {
if (select_btn.getSingleDebouncedPress()) {
set_mag_size();
}
if (trigger_btn.getSingleDebouncedPress()) {
count_ammo_down();
}
if (mag_btn.getSingleDebouncedPress()) {
reload();
}
if (ammo_current == ammo_prev) {
return;
} else {
display_ammo();
}
}
void setup() {
Serial.begin(9600);
delay(500); // needed to be able to flash a new sketch more easily
display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
display.clearDisplay();
display.setCursor(0,0);
display.setTextSize(4);
display.setTextColor(WHITE);
display.print("NERF");
display.display();
display_ammo();
Serial.println("Startup complete");
}