#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define Start 4
#define BuzzerPin 11
int led = 13;
int Volt = 0;
int Time_L = 0;
int Time_R = 0;
int direct=0;
int rest=0;
int lop=0;
int setVolt = 0;
int setTime_L = 0;
int setTime_R = 0;
int setDirect=0;
int setRest=0;
int setLop = 0;
boolean timeState = false;
#define encoderPinA 2 // encoder CLK right
#define encoderPinB 6 // encoder DT left
#define encoderButton 5 // encoder SW switch
int Switch_Tengah = 1;
int encoderPos = 0; // a counter for the dial
unsigned int lastReportedPos = 1; // change management
static boolean rotating = false; // debounce management
boolean A_set = false;
boolean B_set = false;
const unsigned char Logo_Break_In [] PROGMEM = {
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xf8, 0x00, 0x30, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x20, 0x0f, 0xfc, 0x00, 0x3c, 0x07,
0xff, 0xf8, 0x00, 0x30, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xf8, 0x00, 0x18, 0x0f,
0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0x18, 0x0f,
0xff, 0xf7, 0xff, 0xc7, 0xff, 0x87, 0xff, 0x1f, 0xff, 0x1e, 0x3e, 0x3f, 0xf9, 0xc7, 0x9b, 0xcf,
0xff, 0xe7, 0xff, 0xcf, 0xff, 0x8f, 0xff, 0x1f, 0xff, 0x1e, 0xf0, 0xff, 0xf3, 0x8f, 0xc3, 0x9f,
0xff, 0xe7, 0xff, 0xcf, 0xff, 0x8f, 0xfe, 0x1f, 0xfe, 0x1f, 0xe1, 0x80, 0x03, 0x8f, 0xe3, 0x9f,
0xff, 0xe7, 0x07, 0xcf, 0x07, 0x8f, 0x00, 0x3f, 0xfe, 0x3d, 0xc3, 0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e, 0x00, 0x02, 0x02, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x08, 0x03, 0xfe, 0x00, 0x03, 0x01, 0xff,
0xfc, 0x00, 0x04, 0x06, 0x00, 0x00, 0x00, 0x08, 0x00, 0x08, 0x03, 0xfe, 0x00, 0x03, 0x83, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xf8, 0x00, 0x00, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x1f, 0xff, 0xff,
0xff, 0xf8, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x1f, 0xff, 0xff,
0xff, 0xf0, 0x00, 0x00, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff,
0xff, 0xf7, 0xff, 0xc7, 0x99, 0xe3, 0xff, 0xe3, 0xff, 0xe3, 0xff, 0x8f, 0x87, 0x9f, 0xff, 0xff,
0xff, 0xf7, 0xff, 0xcf, 0x19, 0xc3, 0xff, 0xe3, 0xff, 0xe3, 0xff, 0x8f, 0xcf, 0xbf, 0xff, 0xff,
0xff, 0xe7, 0xff, 0xcf, 0x39, 0xc7, 0xff, 0xe3, 0xff, 0xe3, 0xff, 0x8f, 0xff, 0xbf, 0xff, 0xff,
0xff, 0xe7, 0xff, 0xcf, 0x11, 0xc7, 0xff, 0xc7, 0xff, 0xc7, 0xff, 0x0f, 0xff, 0xbf, 0xff, 0xff,
0xff, 0xcf, 0x00, 0x0f, 0x87, 0x07, 0x80, 0x00, 0x78, 0x07, 0x00, 0x1f, 0xff, 0x7f, 0xff, 0xff,
0xff, 0xce, 0x00, 0x0f, 0xce, 0x0f, 0x00, 0x00, 0x78, 0x0f, 0x01, 0x99, 0xcf, 0x7f, 0xff, 0xff,
0xff, 0xcf, 0xff, 0xa7, 0xfe, 0x4f, 0xff, 0x98, 0xf9, 0xcf, 0xf9, 0x31, 0xce, 0x7f, 0xff, 0xff,
0xff, 0x9f, 0xff, 0x27, 0xf8, 0xcf, 0xff, 0x1c, 0xf3, 0xcf, 0xf3, 0x31, 0xce, 0x7f, 0xff, 0xff,
0xff, 0x80, 0x0f, 0x33, 0xf1, 0x80, 0x0f, 0x3c, 0xf3, 0x9e, 0x03, 0x33, 0x8e, 0x7f, 0xff, 0xff,
0xff, 0x80, 0x0f, 0x33, 0xe1, 0x80, 0x0f, 0x39, 0xf3, 0x9e, 0x00, 0x73, 0x9c, 0xff, 0xff, 0xff,
0xff, 0x80, 0x1f, 0x71, 0xe3, 0x80, 0x0f, 0x39, 0xe3, 0x9e, 0x00, 0x73, 0x9c, 0xff, 0xff, 0xff,
0xff, 0x3f, 0xfe, 0x73, 0xe7, 0x1f, 0xfe, 0x39, 0xe7, 0x1f, 0xfc, 0x77, 0x1c, 0xff, 0xff, 0xff,
0xff, 0x3f, 0xfe, 0x73, 0xcf, 0x3f, 0xfe, 0x71, 0xe7, 0x3f, 0xfc, 0x77, 0x1c, 0xff, 0xff, 0xff,
0xfe, 0x7f, 0xfc, 0xe7, 0x8e, 0x3f, 0xfe, 0xf3, 0xcf, 0x3f, 0xf8, 0xee, 0x39, 0xff, 0xff, 0xff,
0xfe, 0x00, 0x00, 0xe0, 0x1e, 0x00, 0x00, 0xf0, 0x0e, 0x00, 0x00, 0x00, 0x01, 0xff, 0xff, 0xff,
0xfe, 0x00, 0x00, 0xe0, 0x1e, 0x00, 0x00, 0xe0, 0x0e, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff,
0xfc, 0x00, 0x01, 0xe0, 0x1e, 0x00, 0x00, 0xe0, 0x1e, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
};
void setup()
{
Serial.begin(9600);
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
pinMode(Start, INPUT_PULLUP);
pinMode(encoderPinA, INPUT_PULLUP); //enabling pullups
pinMode(encoderPinB, INPUT_PULLUP);
pinMode(encoderButton, INPUT_PULLUP);
attachInterrupt(0, doEncoderA, CHANGE); //pin 2
attachInterrupt(1, doEncoderB, CHANGE); //pin 3
display.clearDisplay();
display.drawBitmap(0, 0, Logo_Break_In, 128, 64, WHITE);
delay(2000);
display.display();
}
}
void doEncoderA() {
// debounce
if ( rotating ) delay (1); // wait a little until the bouncing is done
// Test transition, did things really change?
if ( digitalRead(encoderPinA) != A_set ) { // debounce once more
A_set = !A_set;
// adjust counter + if A leads B
if ( A_set && !B_set )
encoderPos = +1;
tone(BuzzerPin, 1000, 10);
rotating = false; // no more debouncing until loop() hits again
}
}
// Interrupt on B changing state
void doEncoderB() {
if ( rotating ) delay (1);
if ( digitalRead(encoderPinB) != B_set ) {
B_set = !B_set;
// adjust counter – 1 if B leads A
if ( B_set && !A_set )
encoderPos = -1;
tone(BuzzerPin, 1000, 10);
rotating = false;
}
}
void loop() {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("set Volt ");
display.print(Volt);
display.setCursor(0, 10);
display.print("set Time_L ");
display.print(Time_L );
display.setCursor(0, 20);
display.print("set Time_R ");
display.print(Time_R);
display.setCursor(0, 30);
display.print("Direct ");
display.print(direct);
display.setCursor(0, 40);
display.print("Rest ");
display.print(rest);
display.setCursor(0, 50);
display.print("looping ");
display.print(lop);
// display.println(t);
display.display();
if (digitalRead(encoderButton) == LOW)
{
Switch_Tengah = Switch_Tengah + 1;
if (Switch_Tengah == 7)
{
Switch_Tengah = 1;
}
tone(BuzzerPin, 2000, 10);
int t = 80;
while (digitalRead(encoderButton) == LOW) {
delay(1); t = t - 1;
/*display.setCursor(0, 0);
display.print(t);
display.print(" ");
*/
// display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(100, 40);
display.print("");
display.println(t);
// display.display();
if (t == 60) {tone(BuzzerPin, 200, 50);}
if (t == 40) {tone(BuzzerPin, 200, 50);}
if (t == 20) {tone(BuzzerPin, 200, 50);}
if (t <= 1) {
t = 1; timeState = true;
setVolt = Volt;
setTime_L=Time_L;
setTime_R=Time_R;
setDirect=direct;
setLop=lop;
setRest=rest;
/*
display.setCursor(0, 0);
display.print("Get Set Go ");
*/
// display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(100, 40);
display.print("Get Set Go ");
display.println(t);
display.print(Time_R);
display.display();
tone(BuzzerPin, 2000, 10);
}
}
}
if (Switch_Tengah == 1)
// { display.setCursor(0, 0);display.print("set Volt ");}
{
// display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print(" > ");
display.display();
}
if (Switch_Tengah == 2)
//{ display.setCursor(0, 0);display.print("set Time_L ");}
{
//display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 10);
display.print(" > ");
display.display();
}
if (Switch_Tengah == 3)
//{ display.setCursor(0, 0);display.print("set Time_R ");}
{
// display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 20);
display.print(" > ");
display.display();
}
if (Switch_Tengah == 4)
//{ display.setCursor(0, 0);display.print("set loopin ");}
{
// display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 30);
display.print(" > ");
display.display();
}
if (Switch_Tengah == 5)
//{ display.setCursor(0, 0);display.print("set loopin ");}
{
// display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 40);
display.print(" > ");
display.display();
}
if (Switch_Tengah == 6)
//{ display.setCursor(0, 0);display.print("set loopin ");}
{
// display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 50);
display.print(" > ");
display.display();
}
delay(1);
rotating = true; // reset the debouncer
encoderPos = constrain(encoderPos, -1, 1);
if (lastReportedPos != encoderPos) {
if (Switch_Tengah == 1) {
Volt = Volt + encoderPos;
Volt = constrain(Volt, 0, 0);
}
else if (Switch_Tengah == 2) {
Time_L = Time_L + encoderPos;
Time_L = constrain(Time_L, 0, 10);
}
else if (Switch_Tengah == 3) {
Time_R = Time_R + encoderPos;
Time_R = constrain(Time_R, 0, 20);
}
else if (Switch_Tengah == 4) {
direct = direct + encoderPos;
direct = constrain(direct, 0, 30);
String direct = "> " "# ";
// String direct = mode == 0 ? "> " : "# ";
}
else if (Switch_Tengah == 5) {
rest = rest + encoderPos;
rest = constrain(rest, 0, 40);
}
else if (Switch_Tengah == 6) {
lop = lop + encoderPos;
lop = constrain(lop, 0, 50);
}
encoderPos = 0;
lastReportedPos = encoderPos;
}
}
/*
if (digitalRead(Start) == LOW || timeState == true) { //start count down timer
tone(BuzzerPin, 1000, 100);
digitalWrite(led,HIGH);
// display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 55);
display.print("Counting Down Start!");
//display.print(t);
display.display();
timeState = true;
delay(500);
while (timeState == true) {
//delay(992); //maintime delay
int period = 1000;
unsigned long time_now = 0;
time_now = millis();
while(millis() < time_now + period){ //wait approx. [period] ms}
}
Time_R = Time_R - 1;
if (Time_L == 0 && Volt >= 1) {
Time_L = 60;
Volt = Volt - 1;
}
if (Time_R == 0 && Time_L >= 1) {
Time_R = 60;
Time_L = Time_L - 1;
}
else if (Time_L == 0 && Volt == 0 && Time_R == 0) { //count down alarm
while (timeState == true) {
digitalWrite(led,LOW);
/*
display.clearDisplay();
display.setCursor(0, 0);
display.print("COUNT DOWN TIMER");
display.setCursor(5, 1);
display.print("done!");
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 32);
display.print("TIMER DONE");
delay(1000);
display.display();
tone(BuzzerPin, 900, 100);
delay(250);
tone(BuzzerPin, 800, 250);
delay(100);
tone(BuzzerPin, 900, 50);
delay(250);
noTone(100);
tone(BuzzerPin, 800, 250);
delay(100);
timeState = false;
Time_R = 1;
/*
if (digitalRead(Start) == LOW) { // turn alarm off with switch
timeState = false;
Time_R = 1;
display.clearDisplay();
display.setCursor(0, 0);
display.print("COUNT DOWN TIMER");
display.setCursor(4, 1);
display.print("00:00:00");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("COUNT DOWN TIMER");
display.setCursor(0, 20);
display.print("00:00:00 ");
display.display();
Volt =setVolt;
Time_L=setTime_L;
Time_R=setTime_R;
direct=setDirect;
rest=setRest;
lop=setLop;
break;
}
if (digitalRead(encoderButton) == LOW) { // turn alarm off with rotary encoder switch
timeState = false;
//Time_R = 1;
/* display.clearDisplay();
display.setCursor(0, 0);
display.print("COUNT DOWN TIMER");
display.setCursor(4, 1);
display.print("Done!");
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.print("COUNT DOWN TIMER");
display.setCursor(0, 20);
display.print("Done! ");
display.display();
while(digitalRead(encoderButton) == LOW){delay(1000);}//wait for release switch
Volt =setVolt;
Time_L=setTime_L;
Time_R=setTime_R;
direct=setDirect;
rest=setRest;
lop=setLop;
break;
}
}
}
}
}
*/