#include <Arduino.h>
#include <U8g2lib.h> // u8g2 library for drawing on OLED display - needs to be installed in Arduino IDE first
#include <Wire.h> // wire library for IIC communication with the OLED display
#include "pins.h"
#include "constants.h"
// U8G2_SSD1306_128X64_NONAME_1_HW_I2C u8g2(U8G2_R0); // set the OLED display to 128x64px, HW IIC, no rotation, used in WOKWI
U8G2_SH1107_128X128_1_HW_I2C u8g2(U8G2_R0); // final display, 128x128px [page buffer, size = 128 bytes], HW IIC connection
// IIC connection of the OLED display and Arduino UNO
// +5V > +5V
// GND > GND
// SCL (serial clock) > A5 or SCL
// SDA (serial data) > A4 or SDA
bool isInOperationLoop = true;
bool inDropLoop = false;
long isInOperationLoopSince = 0;
bool arcadeButtonPressed = false;
void setup(void) { // Arduino setup
Serial.begin(115200);
u8g2.begin(); // begin the u8g2 library
u8g2.setContrast(255); // set display contrast/brightness
pinMode(Arcade_BTN, INPUT);
delay(1000); // Button seems to be misbehaving on startup
Serial.println("Setup Comlpete");
}
void loop(void) { // main Arduino loop
if(arcadeButtonPressed){
if(isInOperationLoopSince == 0){
isInOperationLoop = true;
isInOperationLoopSince = millis();
} else {
isInOperationLoop = false;
inDropLoop = true;
isInOperationLoop = 0;
}
}
if(isInOperationLoopSince !=0 ){
long remainingTotalSeconds = (CLAW_MACHINE_REST_TIMEOUT_MS - (millis() - isInOperationLoopSince))/1000;
if(remainingTotalSeconds <= 0){
isInOperationLoopSince = 0;
isInOperationLoop = false;
}
}
u8g2.firstPage(); // select the first page of the display (page is 128x8px), since we are using the page drawing method of the u8g2 library
u8g2.firstPage();
u8g2.setFont(u8g_font_6x10);
do {
u8g2.drawStr(0, LINE_1_Y, " Tampa Hackerspace");
if(inDropLoop){
u8g2.drawStr(0, LINE_2_Y, "Dropping Payload");
} else if (isInOperationLoop){
long remainingTotalSeconds = (CLAW_MACHINE_REST_TIMEOUT_MS - (millis() - isInOperationLoopSince))/1000;
long remainingMinutes = remainingTotalSeconds / 60;
long remainingSeconds = remainingTotalSeconds % 60;
u8g2.drawStr(0, LINE_2_Y, "Grab Loot!");
char buffer[20];
sprintf(buffer,"Time Left: %02d:%02d",remainingMinutes,remainingSeconds);
u8g2.drawStr(0, LINE_3_Y, buffer);
} else {
// Idle display
u8g2.drawStr(0, LINE_2_Y, " Press Button");
u8g2.drawStr(0, LINE_3_Y, " to start");
}
} while ( u8g2.nextPage() );
if(inDropLoop){
delay(1000);
inDropLoop = false;
isInOperationLoop = false;
isInOperationLoopSince = 0;
}
bool isButtonPressed = digitalRead(Arcade_BTN);
if(arcadeButtonPressed != isButtonPressed){
if(isButtonPressed){
Serial.println("Button Pressed");
} else {
Serial.println("Button Released");
}
arcadeButtonPressed = isButtonPressed;
}
}