Guest User

PowerBuddy

a guest
Apr 13th, 2018
45
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //OPLADER Copyright Can Ur, Bob Schrama, Wesley Oerlemans
  2.  
  3. #include <Wire.h>
  4. #include <Adafruit_GFX.h>
  5. #include <Adafruit_SSD1306.h>
  6. #include <SPI.h>
  7. #include <Fonts/Org_01.h>
  8.  
  9. #define on 2
  10. #define change 4
  11. #define next 3
  12. #define reset 5
  13. #define output 7
  14.  
  15. #define r A0
  16. #define g A1
  17. #define b A2
  18.  
  19. int red = 0, green = 255, blue = 0;
  20.  
  21. const uint8_t screenS[2] = {128, 48};
  22. Adafruit_SSD1306 display(-1);
  23.  
  24. int seconds = 0, minutes = 1, hours = 0;
  25.  
  26. bool ticking = false, finished = false;
  27.  
  28. int index = 1, values[6] = {0, 0, 0, 0, 0, 0};
  29.  
  30. void setup()
  31. {
  32.   Serial.begin(9600);
  33.  
  34.   pinMode(on, INPUT);
  35.   pinMode(change, INPUT);
  36.   pinMode(next, INPUT);
  37.   pinMode(reset, INPUT);
  38.   pinMode(output, OUTPUT);
  39.   digitalWrite(output, LOW);
  40.  
  41.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  42.   display.clearDisplay();
  43. }
  44.  
  45. void loop()
  46. {
  47.   int totalTime = seconds + minutes*10 + hours*100;
  48.   if(digitalRead(on) == 1 && !ticking && totalTime > 4)
  49.   {
  50.     finished = false;
  51.     ticking = true;
  52.   }
  53.  
  54.   if(digitalRead(reset) == 1)
  55.   {
  56.     if(!ticking) seconds = minutes = hours = 0;
  57.     ticking = false;
  58.     finished = true;
  59.   }
  60.  
  61.   if(ticking) drawCharge();
  62.   else drawSelect();
  63.  
  64.   if(ticking) green = 255;
  65.   else green = 0;
  66.   blue = 0;
  67.   if(!ticking) red = 255;
  68.   else red = 0;
  69.  
  70.   if(ticking)
  71.   {
  72.     digitalWrite(output, HIGH);
  73.     tickClock();
  74.   }
  75.   else
  76.   {
  77.     digitalWrite(output, LOW);
  78.     selectTimer();
  79.   }
  80.  
  81.   analogWrite(r, red);
  82.   analogWrite(g, green);
  83.   analogWrite(b, blue);
  84. }
  85.  
  86. void fontTxt()
  87. {
  88.   display.setFont(&Org_01);
  89. }
  90.  
  91. void writeScreen(String text, int x, int y, int textSize, int scrollDel)
  92. {
  93.   display.setTextSize(textSize);
  94.   if(!ticking) display.setTextColor(WHITE);
  95.   else display.setTextColor(BLACK);
  96.   display.setCursor(x, y);
  97.   display.print(text);
  98.   display.display();
  99.   delay(scrollDel);
  100.   if (scrollDel > 0) display.startscrollleft(0x00, 0x0F);
  101. }
  102.  
  103. void drawCharge()
  104. {
  105.   display.clearDisplay();
  106.   uint8_t color = 1;
  107.   int x = screenS[0] / 2 - 2, y = 12;
  108.  
  109.   int countPos[2] = {x - 34, y + 18};
  110.   String hrs = String(hours), mins = String(minutes), secs = String(seconds);
  111.   if(hrs.length() < 2) hrs = "0" + String(hours);
  112.   if(mins.length() < 2) mins = "0" + String(minutes);
  113.   if(secs.length() < 2) secs = "0" + String(seconds);
  114.  
  115.   display.fillRect(0, 0, screenS[0], screenS[1], WHITE);
  116.  
  117.   int chargeX = x - 5;
  118.  
  119.   display.fillRect(chargeX + 4, y - 8, 18, 14, 0x0000); //Skin
  120.   display.fillRect(chargeX + 11, y - 9, 13, 16, 0x0000); //HAIR - RIGHT
  121.   display.fillRect(chargeX + 9, y - 6, 4, 10, color % 2); //EyeLeft
  122.   display.fillRect(chargeX + 15, y - 6, 4, 10, color % 2); //EyeRight
  123.   display.fillRect(chargeX - 13, y - 4, 15, 7, 0x0000); //HAIR - KNOT
  124.   display.fillRect(chargeX + 23, y - 7, 7, 5, 0x0000); //stekkerTop
  125.   display.fillRect(chargeX + 23, y + 1, 7, 5, 0x0000); //stekkerBottom
  126.  
  127.   fontTxt();
  128.   writeScreen(hrs + ":" + mins + ":" + secs, countPos[0], countPos[1]-1, 2, 0);
  129.   display.display();
  130. }
  131.  
  132. void tickClock()
  133. {
  134.   if(hours <= 0 && minutes <= 0 && seconds <= 0)
  135.   {
  136.     ticking = false;
  137.     finished = true;
  138.   }
  139.  
  140.   if(seconds > 0) seconds--;
  141.   else
  142.   {
  143.     if(minutes > 0)
  144.     {
  145.       seconds = 60;
  146.       minutes--;                                      
  147.     }
  148.     else
  149.     {
  150.       if(hours > 0)
  151.       {
  152.         minutes = 60;
  153.         hours--;
  154.       }
  155.       else hours = 0;
  156.     }
  157.   }
  158.   delay(1000);
  159. }
  160.  
  161. void selectTimer()
  162. {
  163.   if(digitalRead(next) == 1)
  164.   {
  165.     index = ((index + 1) < 6)? index + 1 : 1;
  166.     delay(100);
  167.   }
  168.   if(digitalRead(change) == 1 && index > 0)
  169.   {
  170.     if((index == 0 || index == 2 || index == 4)) values[index] += 10;
  171.     else if((index == 3 && values[index-1] < 60) || (index == 5 && values[index-1] < 60) || (index != 3 && index != 5)) values[index]++;
  172.     if(values[1] > 2) values[1] = 0;
  173.     values[0] = 0;
  174.    
  175.     if((index == 4 || index == 2 || index == 0) && values[index] > 60) values[index] = 0;
  176.     if((index == 1 || index == 3 || index == 5) && values[index] > 9) values[index] = 0;
  177.    
  178.     if(index == 2 && values[index] >= 60) values[index + 1] = 0;
  179.     if(index == 4 && values[index] >= 60) values[index + 1] = 0;
  180.    
  181.     delay(100);
  182.   }
  183.   if(values[2] == 6) values[3] = 0;
  184.   if(values[4] == 6) values[5] = 0;
  185.   seconds = String(values[4] + values[5]).toInt();
  186.   minutes = String(values[2] + values[3]).toInt();
  187.   hours = String(values[0] + values[1]).toInt();
  188. }
  189.  
  190. void drawSelect()
  191. {
  192.   display.clearDisplay();
  193.   uint8_t color = 1;
  194.   int x = screenS[0] / 2 - 2, y = 10;
  195.  
  196.   int countPos[2] = {x - 34, y + 18};
  197.   String hrs = String(hours), mins = String(minutes), secs = String(seconds);
  198.   if(hrs.length() < 2) hrs = "0" + String(hours);
  199.   if(mins.length() < 2) mins = "0" + String(minutes);
  200.   if(secs.length() < 2) secs = "0" + String(seconds);
  201.  
  202.   int clockX = x - 9;
  203.  
  204.   display.fillRect(clockX - 3, y - 6, 7, 10, color % 2); //Linkerkant
  205.   display.fillRect(clockX + 4, y - 8, 18, 14, color % 2); //Base
  206.   display.fillRect(clockX + 9, y - 6, 4, 7, 0x0000); //Wijzer
  207.   display.fillRect(clockX + 13, y - 1, 8, 2, 0x0000); //WijzerKlein
  208.   display.fillRect(clockX + 22, y - 6, 7, 10, color % 2); //Rechterkant
  209.  
  210.   fontTxt();
  211.  
  212.   writeScreen(hrs + ":" + mins + ":" + secs, countPos[0], countPos[1], 2, 0);
  213.  
  214.   display.fillRect(countPos[0] + (index*14-(index%2+1)), countPos[1] - 10, 10, 15, color & 2);
  215.   display.display();
  216. }
RAW Paste Data