// Expample Code from https://www.youtube.com/watch?v=HNhJCcOiL3o&ab_channel=LetsMakeit
#include <Arduino.h>
#include <U8g2lib.h>
#include <Keypad.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
//Display Setup
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE);
//keypad Setup
const byte ROWS=4;
const byte COLS=4;
char hexaKeys[ROWS][COLS] = { // Array to represent keys on keypad
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; // Connections to Arduino
byte colPins[COLS] = {5, 4, 3, 2}; // Connections to Arduino
Keypad keypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); // Create keypad object
int MotorPin1=11;
int MotorPin2=10;
int buzzerPin=13;
int dt=100;
char currentTimeValue[4];
int currentState=1; // 1: Timer is not running 2: Timer is running
int timerSeconds=0;
int lpcnt=0;
int Motormode;
byte modeswitch=12;
byte modeswitchstate;
String motorfuction;
//functions
void displayCodeEntryScreen() {
int DISPW;
int TEXTW1;
int TEXTW2;
int TEXTW3;
int T1="PRESS";
int T2="ANY KEY";
int T3="TO START";
int FONTH;
int LINEH;
int DISPH;
int HPOS;
int EMPTYS;
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_profont22_tf); // choose a suitable font
TEXTW1=u8g2.getStrWidth(T1);
// Serial.print("TEXTW1:"); Serial.println(TEXTW1);
TEXTW2=u8g2.getStrWidth(T2);
// Serial.print("TEXTW2:"); Serial.println(TEXTW2);
TEXTW3=u8g2.getStrWidth(T3);
// Serial.print("TEXTW3:"); Serial.println(TEXTW3);
DISPW=u8g2.getDisplayWidth();
// Serial.print("DISPW:"); Serial.println(DISPW);
FONTH=u8g2.getAscent();
// Serial.print("FONTH:"); Serial.println(FONTH);
LINEH=(FONTH+2);
// Serial.print("LINEH:"); Serial.println(LINEH);
DISPH=u8g2.getDisplayHeight();
// Serial.print("DISPH:"); Serial.println(DISPH);
HPOS=(DISPH-(((FONTH+2)*3)/2));
// Serial.print("HPOS:"); Serial.println(HPOS);
EMPTYS=((DISPH-(3*LINEH))/2);
u8g2.drawStr((DISPW - TEXTW1) / 2,(EMPTYS+LINEH) , T1);
u8g2.drawStr((DISPW - TEXTW2) / 2,(EMPTYS+(2*LINEH)) , T2);
u8g2.drawStr((DISPW - TEXTW3) / 2,(EMPTYS+(3*LINEH)) , T3);
u8g2.sendBuffer();
}
void buzzer(){
digitalWrite (buzzerPin,HIGH);
delay(1000);
digitalWrite(buzzerPin,LOW);
}
void contin(){
}
void setup() {
u8g2.begin();
displayCodeEntryScreen();
Serial.begin (9600);
pinMode(modeswitch, INPUT_PULLUP);
pinMode (MotorPin1, OUTPUT);
digitalWrite(MotorPin1,LOW);
pinMode (MotorPin2, OUTPUT);
digitalWrite(MotorPin2,LOW);
pinMode (buzzerPin,OUTPUT);
digitalWrite(buzzerPin,LOW);
displayCodeEntryScreen();
currentTimeValue[0]='0';
currentTimeValue[1]='0';
currentTimeValue[2]='0';
currentTimeValue[3]='0';
}
void loop() {
int l;
char tempVal[3];
char key=keypad.getKey();
if(int(key)!=0 and currentState ==1) {
switch(key) {
case'*': //Stop and Reset Button
motorStatus(false);
currentTimeValue[0]='0';
currentTimeValue[1]='0';
currentTimeValue[2]='0';
currentTimeValue[3]='0';
showEnteredTime();
currentState=1;
lpcnt=0;
timerSeconds=0;
break;
case'#': //Start Button
//Store Minutes
tempVal[0]=currentTimeValue[0];
tempVal[1]=currentTimeValue[1];
tempVal[2]=0;
timerSeconds=atol(tempVal)*60; //The atol() function converts a string representing an integer to a long integer
//Store Seconds
tempVal[0]=currentTimeValue[2];
tempVal[1]=currentTimeValue[3];
tempVal[2]=0;
// Add Minutes and Seconds
timerSeconds=timerSeconds + atol(tempVal);
//Set Set to Timer is Running
currentState=2;
break;
default:
currentTimeValue[0]=currentTimeValue[1];
currentTimeValue[1]=currentTimeValue[2];
currentTimeValue[2]=currentTimeValue[3];
currentTimeValue[3]=key;
showEnteredTime();
break;
}
}
//Stop and Reset
if(currentState==2){
if(int(key) !=0){
if(key== '*'){
motorStatus(false);
currentTimeValue[0]='0';
currentTimeValue[1]='0';
currentTimeValue[2]='0';
currentTimeValue[3]='0';
showEnteredTime();
currentState=1;
lpcnt=0;
timerSeconds=0;
}
//timer
} else{
if(lpcnt >=9){
lpcnt=0;
--timerSeconds;
showcountdown();
if (timerSeconds<=0){
currentState=1;
motorStatus(false);
buzzer();
displayCodeEntryScreen();
} else {
motorStatus(true);
}
}
++lpcnt;
delay(100);
}
}
}
void showEnteredTime(){
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_t0_13b_tf); // choose a suitable font
u8g2.setCursor(0, 13);
u8g2.print("Enter Time:"); // write Timer to the internal memory
u8g2.sendBuffer();
u8g2.setFont(u8g2_font_courB18_tf); // choose a suitable font
u8g2.setCursor(0, 34);
u8g2.print(char(currentTimeValue[0])); // write something to the internal memory
u8g2.print(char(currentTimeValue[1])); // write something to the internal memory
u8g2.print(":"); // write something to the internal memory
u8g2.print(char(currentTimeValue[2])); // write something to the internal memory
u8g2.print(char(currentTimeValue[3])); // write something to the internal memory
u8g2.sendBuffer();
}
void motorStatus (bool state){
if (state) {
digitalWrite(MotorPin1, HIGH);
digitalWrite(MotorPin2, LOW);
}
else {
digitalWrite(MotorPin1, LOW);
digitalWrite(MotorPin2, LOW);
}
}
void showcountdown(){
char timest [6];
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_courB18_tf); // choose a suitable font
u8g2.setCursor(0, 16);
u8g2.print("Timer:"); // write Timer to the internal memory
u8g2.sendBuffer();
sprintf(timest,"%02d:%02d",(timerSeconds/60),(timerSeconds-((timerSeconds/60)*60)));
u8g2.setFont(u8g2_font_courB18_tf); // choose a suitable font
u8g2.setCursor(0, 34);
u8g2.print(timest);
u8g2.sendBuffer();
}
//modeswitchstate=digitalRead(modeswitch);
//Serial.print("STATE:");
//Serial.println(modeswitchstate);
//switch (modeswitchstate) {
// case 1:
// motorfuction="agitate";
// break;
// case 0:
// motorfuction="continious";
// break;
//}
//Serial.print("MOTORFUNCTION:");
//Serial.println(motorfuction);