SHARE
TWEET

Pomoduino

a guest Jul 25th, 2015 224 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int green = 13;
  2. int yellow = 7;
  3. int blue = 5;
  4.  
  5. int ledStateGreen = LOW;
  6. int ledStateBlue = LOW;
  7. long previousMillis = 0;
  8. long interval;
  9.  
  10. int buttonPin = 2;
  11. int buttonState = 1;
  12. bool pressed = 0;
  13. long pressTime = 0;
  14.  
  15. int phase = 0;
  16.  
  17. void setup() {
  18.  pinMode(green, OUTPUT);
  19.  pinMode(yellow, OUTPUT);
  20.  pinMode(blue, OUTPUT);
  21.  pinMode(buttonPin, INPUT_PULLUP);
  22.  digitalWrite(buttonPin, HIGH);
  23. }
  24.  
  25. void loop() {
  26.  
  27. // update current time and state of button
  28. unsigned long currentMillis = millis();
  29. int buttonState = digitalRead(buttonPin);
  30.  
  31. // measure time since last button press
  32. long progress = currentMillis - previousMillis;
  33.  
  34. // check to see if button has been pressed
  35. // over 2 seconds since last press
  36. // (to prevent multiple presses registering)
  37. if ((pressTime - currentMillis) > 2000){
  38.  if(buttonState == 0){
  39.    pressed = 1;
  40.    pressTime = currentMillis;}
  41.  else{
  42.     pressed = 0;}
  43. }
  44.  
  45. // phase 0 is "work" phase
  46. // if button has been pressed, add 2 minutes to work timer
  47. if (phase == 0){
  48.  if (pressed == 1){
  49.    interval = 1620000;}
  50.  
  51.  // if interval is over, record current
  52.  // time for measuring next interval
  53.  if(currentMillis - previousMillis > interval) {
  54.    previousMillis = currentMillis;
  55.  
  56.  // set green and blue LED states
  57.  if (ledStateGreen == LOW){
  58.    ledStateGreen = HIGH;
  59.    ledStateBlue = LOW;}
  60.  else {
  61.    ledStateGreen = LOW;}
  62.  
  63.  // apply LED states to LEDs
  64.  // reset interval and switch to "break" phase
  65.  digitalWrite(green, ledStateGreen);
  66.  digitalWrite(blue, ledStateBlue);
  67.  interval = 1500000;
  68.  buttonState = 1;
  69.  phase = 1;
  70.  }
  71. }
  72.  
  73. else {
  74.  
  75.  // if button has been pressed, add 2 minutes to break timer
  76.  if (pressed == 1){
  77.  interval = 420000;}
  78.  
  79.    // if interval is over, record current
  80.    // time for measuring next interval
  81.    if(currentMillis - previousMillis > interval) {
  82.      previousMillis = currentMillis;
  83.  
  84.    // set blue and green LED states
  85.    if (ledStateBlue == HIGH){;
  86.      ledStateBlue = LOW; }
  87.    else {
  88.      ledStateBlue = HIGH;
  89.      ledStateGreen = LOW;}
  90.  
  91.  // apply LED states to LEDs
  92.  // reset interval and set to "work" phase
  93.  digitalWrite(green, ledStateGreen);
  94.  digitalWrite(blue, ledStateBlue);
  95.  interval = 300000;
  96.  buttonState = 1;
  97.  phase = 0;
  98.  }
  99. }
  100.  
  101. // calculate time left in interval
  102. unsigned long timeLeft = (interval - progress);
  103.  
  104. // if there are less than two minutes left, light up yellow LED
  105. if (timeLeft < 120000) {
  106.   digitalWrite(yellow, HIGH); }
  107. else {
  108.   digitalWrite(yellow, LOW); }
  109.  
  110. // reset pressed variable
  111.  pressed = 0;
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top