#include <Wire.h>
// #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSansBold9pt7b.h>
//#include <Fonts/FreeSansBold12pt7b.h>
//#include <Fonts/FreeSansBold18pt7b.h>
#include <Fonts/FreeSansBold24pt7b.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int Count = 0;
double tar1 = 14; //add meg a tárak méretét
double tar2 = 30; //add meg a tárak méretét
double tar = 0;
double csok = 0;
bool vissza = false;
volatile int sensorState = LOW;
// const int InputPin = A0;
const byte D2 = 2;
const int InputPin = 2;
const int buttonPin = 3;
const int tarvPin = 4;
const int iranyPin = 5;
int LastInputState = LOW;
const unsigned char felsokep [] PROGMEM = {
0xff, 0xf3, 0xff, 0xf3, 0xff, 0xe7, 0xff, 0xfc, 0xff, 0xff, 0x9f, 0xff, 0x3f, 0xff, 0x3f, 0xfc,
0xff, 0xf3, 0xff, 0xf3, 0xff, 0xe7, 0xff, 0xfc, 0xff, 0xff, 0x9f, 0xff, 0x3f, 0xff, 0x3f, 0xfc,
0xff, 0xf3, 0xff, 0xf3, 0xff, 0xf3, 0xff, 0xfc, 0xff, 0xff, 0x3f, 0xff, 0x3f, 0xff, 0x3f, 0xfc,
0xff, 0xf1, 0xff, 0xf1, 0xff, 0xf3, 0xff, 0xfc, 0xff, 0xff, 0x3f, 0xfe, 0x3f, 0xfe, 0x3f, 0xfc,
0xff, 0xf9, 0xff, 0xf9, 0xff, 0xf3, 0xff, 0xfc, 0xff, 0xff, 0x3f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfc,
0xff, 0xf9, 0xff, 0xf9, 0xff, 0xf1, 0xff, 0xfc, 0xff, 0xfe, 0x3f, 0xfe, 0x7f, 0xfe, 0x7f, 0xfc,
0xff, 0xf9, 0xff, 0xf9, 0xff, 0xf1, 0xff, 0xf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 0xf0, 0x00, 0x00,
0x00, 0x00, 0x00, 0xff, 0x3f, 0xfe, 0x3f, 0xfc, 0xff, 0xf1, 0xff, 0xf3, 0xfc, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xfe, 0x3f, 0xfc, 0xff, 0xf1, 0xff, 0xe0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xfc, 0xff, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void setup()
{
Serial.begin(115200);
pinMode(InputPin, INPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(tarvPin, INPUT_PULLUP);
pinMode(iranyPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(InputPin), setSensorState, CHANGE);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) // Address 0x3D for 128x64
{
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 5);
display.println(" R S O ");
display.println(" I F ");
display.println(" A T ");
display.println(" A M M O ");
display.println(" C R ");
display.println(" O E ");
display.println(" U N T ");
display.display();
delay(3000);
display.setTextSize(3);
tar = 63 / tar1; //63 jelöli a képernyő közepétől a széléig a képpontok számát
display.clearDisplay();
display.setFont (&FreeSansBold24pt7b);
display.setTextSize(1);
printNumberToDisplay(Count++);
}
void loop()
{
int inputState = digitalRead(InputPin);
int buttonState = digitalRead(buttonPin);
int tarv = digitalRead(tarvPin);
int irany = digitalRead(iranyPin);
if(irany == 0){
if(!vissza){
vissza = true;
}
else{
vissza = false;
}
printNumberToDisplay(Count-1);
}
if(tarv == 0){
display.clearDisplay();
if(tar == 63/tar1){
tar = 63/tar2;
display.setCursor(30, 35);
display.setFont (&FreeSansBold9pt7b);
display.setTextSize(1);
display.println(" Hi-Cap");
display.println(" magazine set");
display.display();
display.setFont (&FreeSansBold24pt7b);
delay(3000);
}
else{
if(tar == 0){
tar = 63/tar1;
display.setCursor(30, 35);
display.setFont (&FreeSansBold9pt7b);
display.setTextSize(1);
display.println("Mid-Cap");
display.println(" magazine set");
display.display();
display.setFont (&FreeSansBold24pt7b);
delay(3000);
}
else{
tar = 0;
display.setCursor(25, 35);
display.setFont (&FreeSansBold9pt7b);
display.setTextSize(1);
display.println("Unlimited");
display.println(" shot!");
display.display();
display.setFont (&FreeSansBold24pt7b);
delay(3000);
}
}
Count = 0;
printNumberToDisplay(Count++);
}
if (buttonState == 0)
{
display.clearDisplay();
Count = 0;
printNumberToDisplay(Count++);
}
if(Count<=63/tar){
if (sensorState == HIGH)
{
printNumberToDisplay(Count++);
}
LastInputState = inputState;
csok = Count*tar;
if(Count >= 0)
{
display.drawBitmap(1, 0, felsokep, 126, 21, WHITE);
display.fillRect(1, 0, 1+csok, 21, BLACK);
display.fillRect(127-csok, 0, 1+csok, 21, BLACK);
display.display();
}
}
else{
display.display();
sensorState = 0;
display.clearDisplay();
display.fillRect(60, 3, 11, 40, WHITE);
display.fillCircle(65, 53, 6, WHITE);
}
}
void setSensorState()
{
// ISR Function
sensorState = HIGH;
}
void printNumberToDisplay(int currentNumber)
{
if(vissza){
String strCurrentNumber = String(int(63/tar) - currentNumber);
display.setTextSize(1);
display.fillRect(0, 22, 128, 50, BLACK);
display.setCursor ((display.width() / 2) - (strCurrentNumber.length()*13), 60 );
display.print(int(63/tar) - currentNumber);
}
else{
String strCurrentNumber = String(currentNumber);
display.setTextSize(1);
display.fillRect(0, 22, 128, 50, BLACK);
display.setCursor ((display.width() / 2) - (strCurrentNumber.length()*13), 60 );
display.print(currentNumber);
}
display.display();
sensorState = LOW;
}
void printTarNumberToDisplay(int currentNumber)
{
String strCurrentNumber = String(currentNumber);
display.setFont (&FreeSansBold24pt7b);
display.setTextSize(1);
display.fillRect(0, 22, 128, 50, BLACK);
display.setCursor ((display.width() / 2) - (strCurrentNumber.length()*13), 60);
display.print(currentNumber);
display.display();
sensorState = LOW;
}