#include <Arduino.h>
#include <U8g2lib.h>
#include <Wire.h> // library requires for IIC communication
#define homeButton A0
#define leftButton A1
#define rightButton A2
#define limitSwitch A3 //E9
#define pour 2 //D1
#define handle 3 //D2
#define pauseIndicator 4 //D3
extern volatile unsigned long timer0_millis;
bool pause = false; //display entering pause at next chance
bool stopFlag = false; //stops the program enters menu
bool homeInt = true; //makes sure interupts dont trigger in the menu
int cycle = 0; // cycle makes sure program runs in order
//millis() timer setup
int pourTime = 1000;
int coolTime = 1000;
int downTime = 1000;
int returnTime = 1000;
int editTime = 0;
int threshold = 2500;
long cyclesCompleted = 0;
unsigned long time_now = 0;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE); // initialization for the used OLED display
// End of constructor list
// 'RovinskyElectricLogo', 128x64px
const unsigned char epd_bitmap_RELogo[] PROGMEM = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f,
0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f,
0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1f,
0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3f, 0x00, 0xfc, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x00, 0xfe, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f,
0x00, 0xff, 0xff, 0x07, 0x00, 0xe0, 0xff, 0x7f, 0x00, 0xff, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0xff, 0xff, 0x03, 0x00, 0xc0, 0xff, 0x3f, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xff, 0xff, 0x01, 0x00, 0xc0, 0xff, 0x3f, 0x00, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0xff, 0xff, 0x01, 0x00, 0xc0, 0xff, 0x1f, 0x80, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0xff, 0xff, 0x00, 0x00, 0xe0, 0xff, 0x1f, 0x80, 0xff, 0x7f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0xff, 0xff, 0x00, 0x00, 0xe0, 0xff, 0x0f, 0xc0, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0xff, 0xff, 0xff, 0x00, 0xf0, 0xff, 0x0f, 0xc0, 0xff, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00,
0xc0, 0xff, 0xff, 0xff, 0x00, 0xf0, 0xff, 0x0f, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00,
0xc0, 0xff, 0xff, 0x7f, 0x00, 0xf8, 0xff, 0x07, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x00,
0xc0, 0xff, 0xff, 0x3f, 0x00, 0xfe, 0xff, 0x07, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00,
0xc0, 0xff, 0xff, 0x1f, 0xfe, 0xff, 0xff, 0x03, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00,
0xe0, 0xff, 0xff, 0x0f, 0xff, 0xff, 0xff, 0x01, 0xf0, 0xff, 0xff, 0xff, 0xff, 0xff, 0x07, 0x00,
0xe0, 0xff, 0xff, 0x87, 0xff, 0xff, 0xff, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00,
0xe0, 0xff, 0xff, 0xc3, 0xff, 0xff, 0x7f, 0x00, 0xf8, 0xff, 0xff, 0xff, 0xff, 0xff, 0x03, 0x00,
0xe0, 0xff, 0xff, 0xe1, 0xff, 0xff, 0x1f, 0x00, 0xf8, 0xff, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xf0, 0xff, 0xe0, 0xff, 0xff, 0x01, 0x00, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xf0, 0x7f, 0xe0, 0xff, 0x3f, 0x00, 0x00, 0xfc, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xf0, 0x3f, 0xe0, 0xff, 0x3f, 0x00, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xf8, 0x1f, 0xc0, 0xff, 0x7f, 0x00, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xf8, 0x0f, 0xc0, 0xff, 0x7f, 0x00, 0x00, 0xfe, 0xff, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xfc, 0x07, 0x80, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xfc, 0x03, 0x80, 0xff, 0xff, 0x01, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xfc, 0x01, 0x00, 0xff, 0xff, 0x01, 0x80, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0xfe, 0x00, 0x00, 0xff, 0xff, 0x03, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
0x00, 0x7e, 0x00, 0x00, 0xfe, 0xff, 0x03, 0x80, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
0x00, 0x3f, 0x00, 0x00, 0xfc, 0xff, 0x07, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x01, 0x00,
0x00, 0x1f, 0x00, 0x00, 0xfc, 0xff, 0x07, 0xc0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0x0f, 0x00, 0x00, 0xf8, 0xff, 0x0f, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x80, 0x07, 0x00, 0x00, 0xf8, 0xff, 0x0f, 0xe0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x80, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x80, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const unsigned char image_ArrowDownEmpty_14x15_bits[] U8X8_PROGMEM = { 0xf8, 0x07, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x0f, 0x3c, 0x01, 0x20, 0x02, 0x10, 0x04, 0x08, 0x08, 0x04, 0x10, 0x02, 0x20, 0x01, 0xc0, 0x00 };
static const unsigned char image_ArrowUpEmpty_14x15_bits[] U8X8_PROGMEM = { 0xc0, 0x00, 0x20, 0x01, 0x10, 0x02, 0x08, 0x04, 0x04, 0x08, 0x02, 0x10, 0x01, 0x20, 0x0f, 0x3c, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0x08, 0x04, 0xf8, 0x07 };
void setup(void) {
noInterrupts();
//home button interuppt setup
PCICR |= B00000010;
PCMSK1 |= B00000001;
interrupts();
delay(1000); //doesnt turn on till power is full plugged in
Serial.begin(9600);
pinMode(homeButton, INPUT_PULLUP);
pinMode(leftButton, INPUT_PULLUP);
pinMode(rightButton, INPUT_PULLUP);
pinMode(limitSwitch, INPUT_PULLUP);
pinMode(pour, OUTPUT);
pinMode(handle, OUTPUT);
pinMode(pauseIndicator, OUTPUT);
digitalWrite(pauseIndicator, LOW); // Turn LED off.
digitalWrite(pour, LOW); // Turn LED off.
digitalWrite(handle, LOW); // Turn LED off.
pause = false;
u8g2.begin();
// Draw RE Logo
DrawLogo(2000);
//Write "Welcome to Forge Master"
//WelcomeMessage();
//delay(3000);
PauseMenu();
}
//MAIN LOOP
void loop() {
/*//Pour Lead
Pour();
//Wait for lead to cool
Cool();
//Cycle Handle
CycleHandle();
*/
}
ISR(PCINT1_vect) {
if (homeInt) {
digitalWrite(pauseIndicator, HIGH);
pause = true;
}
}
void ResetMillis() {
uint8_t oldSREG = SREG;
cli();
timer0_millis = 0;
SREG = oldSREG;
}
void Pour() {
if (pause) { //check if need to pause
EnteringPauseMessage();
pause = false;
stopFlag = true;
}
if (stopFlag) { //will enter the pause menu before starting a new cycle
stopFlag = false;
PauseMenu();
}
if ((digitalRead(limitSwitch) == LOW) && (cycle == 0)) {
//reset milli-second timer
ResetMillis();
digitalWrite(pour, HIGH);
time_now = millis();
while ((millis() < time_now + pourTime) && (digitalRead(limitSwitch) == LOW)) {
}
digitalWrite(pour, LOW);
//If loop was exited because of the limit switch go into error mode
if (millis() < time_now + pourTime) {
Serial.println("pour error (limitSwitch)");
}
cycle = 1;
Serial.println("1");
}
}
void Cool() {
if (pause) { //check if need to pause
EnteringPauseMessage();
pause = false;
stopFlag = true;
}
if (cycle == 1) {
//reset milli-second timer
ResetMillis();
time_now = millis();
while ((millis() < time_now + coolTime) && (digitalRead(limitSwitch) == LOW)) {
}
//If loop was exited because of the limit switch go into error mode low air pressure?
if (millis() < time_now + coolTime) {
Serial.println("low air pressure?");
}
cycle = 2;
Serial.println("2");
}
}
void CycleHandle() {
//Handle Down
if (pause) { //check if need to pause
EnteringPauseMessage();
pause = false;
stopFlag = true;
}
if ((digitalRead(limitSwitch) == LOW) && (cycle == 2)) {
//reset milli-second timer
ResetMillis();
digitalWrite(handle, HIGH);
time_now = millis();
while (millis() < time_now + downTime / 10) {
}
//If limit switch is not released in a tenth of the time go into error mode
if (digitalRead(limitSwitch) == LOW) {
Serial.println("handle stuck up");
}
while (millis() < time_now + downTime) {
}
//begin return
if (digitalRead(limitSwitch) == HIGH) {
digitalWrite(handle, LOW);
time_now = millis();
while ((millis() < time_now + returnTime) && (digitalRead(limitSwitch) == HIGH)) {
}
//check how long it took to return
//If loop was exited because of time out go into error mode
if (millis() >= time_now + returnTime) {
Serial.println("handle stuck down");
}
if (millis() - time_now > threshold) {
Serial.println("handle is getting sticky");
}
}
Serial.println("0");
cycle = 0;
cyclesCompleted = cyclesCompleted + 1;
Serial.print(cyclesCompleted);
Serial.println();
}
}
void PauseMenu() {
homeInt = false;
EditProfile();
homeInt = true;
}
void EditProfile() {
int b = 1;
while (b <= 4) {
if (b == 1) {
editTime = pourTime;
} else if (b == 2) {
editTime = coolTime;
} else if (b == 3) {
editTime = downTime;
} else {
editTime = returnTime;
}
EditTimeScreen(b);
EditTimeScreenNUM();
while (digitalRead(homeButton) == HIGH) {
if ((digitalRead(rightButton) == LOW) && (editTime < 32400)) {
editTime = editTime + 500;
EditTimeScreenNUM();
Serial.print(editTime);
Serial.println();
delay(100);
}
if ((digitalRead(leftButton) == LOW) && (editTime > 500)) {
editTime = editTime - 500;
EditTimeScreenNUM();
Serial.print(editTime);
Serial.println();
delay(100);
}
}
delay(1000);
if (b == 1) {
pourTime = editTime;
} else if (b == 2) {
coolTime = editTime;
} else if (b == 3) {
downTime = editTime;
} else {
returnTime = editTime;
}
b = b + 1;
}
Serial.print(pourTime);
Serial.println();
Serial.print(coolTime);
Serial.println();
Serial.print(downTime);
Serial.println();
Serial.print(returnTime);
Serial.println();
}
void EditTimeScreen(int x) {
u8g2.clearBuffer(); // clear the internal memory
u8g2.setBitmapMode(1);
//Draw Title
u8g2.setFontMode(1);
u8g2.setFont(u8g2_font_6x10_tr);
if (x == 1)
u8g2.drawStr(2, 9, "Select Pour Time (ms)");
else if (x == 2)
u8g2.drawStr(2, 9, "Select Cool Time (ms)");
else if (x == 3)
u8g2.drawStr(2, 9, "Select Down Time (ms)");
else
u8g2.drawStr(2, 9, "Select Back Time (ms)");
//Draw Frame
u8g2.drawFrame(12, 18, 105, 24);
//Draw Button Labels
u8g2.drawXBMP(10, 48, 14, 15, image_ArrowDownEmpty_14x15_bits);
u8g2.drawXBMP(105, 48, 14, 15, image_ArrowUpEmpty_14x15_bits);
u8g2.setFont(u8g2_font_6x10_tr);
u8g2.drawStr(53, 59, "Save");
u8g2.drawFrame(49, 49, 31, 13);
u8g2.sendBuffer();
}
void EditTimeScreenNUM() {
int a = 50;
u8g2.clearBuffer();
//Draw Frame
u8g2.drawFrame(12, 18, 105, 24);
if (editTime > 9999) {
a = 37;
} else if (editTime > 999) {
a = 42;
} else {
a = 47;
}
u8g2.setFont(u8g2_font_profont22_tr);
//u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.setCursor(a, 37);
u8g2.print(editTime);
//update part of the screen
u8g2.updateDisplayArea(4, 2, 8, 3);
u8g2.clearBuffer();
}
void EnteringPauseMessage() {
}
void DrawLogo(int x) {
//Draw Rovinsky Electric Logo
u8g2.clearBuffer(); // clear the internal memory
u8g2.drawXBMP(0, 0, 128, 64, epd_bitmap_RELogo);
u8g2.sendBuffer(); // transfer internal memory to the display
delay(x);
u8g2.clearBuffer();
}