Guest User

Tea-Buttler v2.0 by DeKode

a guest
Sep 18th, 2020
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++
  2. //
  3. //  TeaTimer by DeKode (c) 2020
  4. //  http://wiki.77f.de
  5. //  Version 2.0
  6. //
  7. //+++++++++++++++++++++++++++++++++++++++++++++++++++++++
  8.  
  9. //Includes:
  10. #include <Servo.h>
  11.  
  12. //Declares:
  13. Servo liftServo;
  14.  
  15. //Pin-Defs:
  16. const int pinServo = 4;     //Servo-Pin
  17. const int pinBtnSel = 2;    //Select-Button
  18. const int pinBtnGo = 3;     //Start-Button
  19. const int pinLedReady = 13;  //LED-Pin Ready
  20. const int pinLedTime[8] = {5,6,7,8,9,10,11,12}; //LED-Pins for each Minute
  21.  
  22.  
  23. //Global Variables:
  24. int iServoUp = 45;           //Servo Up-Pos
  25. int iServoDown = 8;        //Servo Down-Pos
  26. int iServoDelay = 20;     //Delay for Servo movement (bigger delay = slower movement)
  27. int pos = 0;              //actual-position
  28. long int iTimeSet = 60000;  //Time Set to Wait (1 min) in ms
  29. int iBtnSel = 0, iBtnSelOld = 0;  //Status Select-Button
  30. int iBtnGo = 0;              //Status Start-Button
  31. int iTimeSelection = 0;      //Counter for Time Selection (0=1min, 7=8min)
  32.  
  33.  
  34. void setup() {
  35.   //PinModes:
  36.   pinMode(pinBtnSel, INPUT_PULLUP);  
  37.   pinMode(pinBtnGo, INPUT_PULLUP);
  38.   pinMode(pinLedReady, OUTPUT);  
  39.   for (int i = 0; i < 8; i++){
  40.     pinMode(pinLedTime[i], OUTPUT);
  41.   }
  42.   fLedSwitcher(iTimeSelection); //Switch LEDs
  43.   liftServo.attach(pinServo);
  44.   liftServo.write(iServoUp);
  45. }
  46.  
  47.  
  48. void loop() {
  49.   //Read inputs:
  50.   iBtnSel = !digitalRead(pinBtnSel);
  51.   iBtnGo = !digitalRead(pinBtnGo);
  52.  
  53.   //Select-Button inc Selection-Counter till max-val then resets to 0:
  54.   if(iBtnSel != iBtnSelOld){
  55.     if(iBtnSel == 1){
  56.       if(iTimeSelection < 7){
  57.           iTimeSelection++;
  58.         }else{
  59.           iTimeSelection = 0;
  60.         }
  61.     fLedSwitcher(iTimeSelection); //Switch LEDs back to selected Time
  62.     }
  63.     delay(100);
  64.     iBtnSelOld = iBtnSel;
  65.   }
  66.  
  67.   //Start-Button: Servo Down, Wait Selected Time, Servo Up
  68.   if(iBtnGo){
  69.     digitalWrite(pinLedReady, LOW); //Ready-LED reset
  70.     fServoDown();
  71.  
  72.     for(int i = 0; i <= iTimeSelection  ;i++){  //Cycle 1 min for x times
  73.       int iCounter = (i-iTimeSelection)*(-1); //Reverse Counter
  74.       fLedSwitcher(iCounter); //Switch LEDs with cycle-number
  75.       delay(iTimeSet);  //Wait for 1 min    
  76.     }
  77.    
  78.     fServoUp();
  79.   }
  80.   digitalWrite(pinLedReady, HIGH);  //Ready-LED if Timer finished
  81. }
  82.  
  83. //Function to drive the Servo down
  84. void fServoDown(){
  85.   //Servo in Start-Pos (for double-check)
  86.   liftServo.write(iServoUp);
  87.   //Slowly move Servo
  88.   for(pos = iServoUp; pos >= iServoDown; pos--){
  89.     liftServo.write(pos);
  90.     delay(iServoDelay);
  91.   }
  92.   //Servo in End-Pos
  93.   liftServo.write(iServoDown);  
  94. }
  95.  
  96. void fServoUp(){
  97.   //Servo in Start-Pos (for double-check)
  98.   liftServo.write(iServoDown);
  99.   //Slowly move Servo
  100.   for(pos = iServoDown; pos <= iServoUp; pos++){
  101.     liftServo.write(pos);
  102.     delay(iServoDelay);
  103.   }
  104.   //Servo in End-Pos
  105.   liftServo.write(iServoUp);  
  106.  
  107. }
  108.  
  109. //Function LED-Switcher
  110. void fLedSwitcher(int iTimeSetting){
  111.   //Turn all LED off:
  112.   for (int i = 0; i<8; i++){
  113.     digitalWrite(pinLedTime[i], LOW);
  114.   }
  115.   //Turn LED for iTimeSetting on:
  116.   digitalWrite(pinLedTime[iTimeSetting], HIGH);  
  117. }
  118.  
Add Comment
Please, Sign In to add comment