Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //OPLADER Copyright Can Ur, Bob Schrama, Wesley Oerlemans
- #include <Wire.h>
- #include <Adafruit_GFX.h>
- #include <Adafruit_SSD1306.h>
- #include <SPI.h>
- #include <Fonts/Org_01.h>
- #define on 2
- #define change 4
- #define next 3
- #define reset 5
- #define output 7
- #define r A0
- #define g A1
- #define b A2
- int red = 0, green = 255, blue = 0;
- const uint8_t screenS[2] = {128, 48};
- Adafruit_SSD1306 display(-1);
- int seconds = 0, minutes = 1, hours = 0;
- bool ticking = false, finished = false;
- int index = 1, values[6] = {0, 0, 0, 0, 0, 0};
- void setup()
- {
- Serial.begin(9600);
- pinMode(on, INPUT);
- pinMode(change, INPUT);
- pinMode(next, INPUT);
- pinMode(reset, INPUT);
- pinMode(output, OUTPUT);
- digitalWrite(output, LOW);
- display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
- display.clearDisplay();
- }
- void loop()
- {
- int totalTime = seconds + minutes*10 + hours*100;
- if(digitalRead(on) == 1 && !ticking && totalTime > 4)
- {
- finished = false;
- ticking = true;
- }
- if(digitalRead(reset) == 1)
- {
- if(!ticking) seconds = minutes = hours = 0;
- ticking = false;
- finished = true;
- }
- if(ticking) drawCharge();
- else drawSelect();
- if(ticking) green = 255;
- else green = 0;
- blue = 0;
- if(!ticking) red = 255;
- else red = 0;
- if(ticking)
- {
- digitalWrite(output, HIGH);
- tickClock();
- }
- else
- {
- digitalWrite(output, LOW);
- selectTimer();
- }
- analogWrite(r, red);
- analogWrite(g, green);
- analogWrite(b, blue);
- }
- void fontTxt()
- {
- display.setFont(&Org_01);
- }
- void writeScreen(String text, int x, int y, int textSize, int scrollDel)
- {
- display.setTextSize(textSize);
- if(!ticking) display.setTextColor(WHITE);
- else display.setTextColor(BLACK);
- display.setCursor(x, y);
- display.print(text);
- display.display();
- delay(scrollDel);
- if (scrollDel > 0) display.startscrollleft(0x00, 0x0F);
- }
- void drawCharge()
- {
- display.clearDisplay();
- uint8_t color = 1;
- int x = screenS[0] / 2 - 2, y = 12;
- int countPos[2] = {x - 34, y + 18};
- String hrs = String(hours), mins = String(minutes), secs = String(seconds);
- if(hrs.length() < 2) hrs = "0" + String(hours);
- if(mins.length() < 2) mins = "0" + String(minutes);
- if(secs.length() < 2) secs = "0" + String(seconds);
- display.fillRect(0, 0, screenS[0], screenS[1], WHITE);
- int chargeX = x - 5;
- display.fillRect(chargeX + 4, y - 8, 18, 14, 0x0000); //Skin
- display.fillRect(chargeX + 11, y - 9, 13, 16, 0x0000); //HAIR - RIGHT
- display.fillRect(chargeX + 9, y - 6, 4, 10, color % 2); //EyeLeft
- display.fillRect(chargeX + 15, y - 6, 4, 10, color % 2); //EyeRight
- display.fillRect(chargeX - 13, y - 4, 15, 7, 0x0000); //HAIR - KNOT
- display.fillRect(chargeX + 23, y - 7, 7, 5, 0x0000); //stekkerTop
- display.fillRect(chargeX + 23, y + 1, 7, 5, 0x0000); //stekkerBottom
- fontTxt();
- writeScreen(hrs + ":" + mins + ":" + secs, countPos[0], countPos[1]-1, 2, 0);
- display.display();
- }
- void tickClock()
- {
- if(hours <= 0 && minutes <= 0 && seconds <= 0)
- {
- ticking = false;
- finished = true;
- }
- if(seconds > 0) seconds--;
- else
- {
- if(minutes > 0)
- {
- seconds = 60;
- minutes--;
- }
- else
- {
- if(hours > 0)
- {
- minutes = 60;
- hours--;
- }
- else hours = 0;
- }
- }
- delay(1000);
- }
- void selectTimer()
- {
- if(digitalRead(next) == 1)
- {
- index = ((index + 1) < 6)? index + 1 : 1;
- delay(100);
- }
- if(digitalRead(change) == 1 && index > 0)
- {
- if((index == 0 || index == 2 || index == 4)) values[index] += 10;
- else if((index == 3 && values[index-1] < 60) || (index == 5 && values[index-1] < 60) || (index != 3 && index != 5)) values[index]++;
- if(values[1] > 2) values[1] = 0;
- values[0] = 0;
- if((index == 4 || index == 2 || index == 0) && values[index] > 60) values[index] = 0;
- if((index == 1 || index == 3 || index == 5) && values[index] > 9) values[index] = 0;
- if(index == 2 && values[index] >= 60) values[index + 1] = 0;
- if(index == 4 && values[index] >= 60) values[index + 1] = 0;
- delay(100);
- }
- if(values[2] == 6) values[3] = 0;
- if(values[4] == 6) values[5] = 0;
- seconds = String(values[4] + values[5]).toInt();
- minutes = String(values[2] + values[3]).toInt();
- hours = String(values[0] + values[1]).toInt();
- }
- void drawSelect()
- {
- display.clearDisplay();
- uint8_t color = 1;
- int x = screenS[0] / 2 - 2, y = 10;
- int countPos[2] = {x - 34, y + 18};
- String hrs = String(hours), mins = String(minutes), secs = String(seconds);
- if(hrs.length() < 2) hrs = "0" + String(hours);
- if(mins.length() < 2) mins = "0" + String(minutes);
- if(secs.length() < 2) secs = "0" + String(seconds);
- int clockX = x - 9;
- display.fillRect(clockX - 3, y - 6, 7, 10, color % 2); //Linkerkant
- display.fillRect(clockX + 4, y - 8, 18, 14, color % 2); //Base
- display.fillRect(clockX + 9, y - 6, 4, 7, 0x0000); //Wijzer
- display.fillRect(clockX + 13, y - 1, 8, 2, 0x0000); //WijzerKlein
- display.fillRect(clockX + 22, y - 6, 7, 10, color % 2); //Rechterkant
- fontTxt();
- writeScreen(hrs + ":" + mins + ":" + secs, countPos[0], countPos[1], 2, 0);
- display.fillRect(countPos[0] + (index*14-(index%2+1)), countPos[1] - 10, 10, 15, color & 2);
- display.display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement