2017年3月25日 星期六

Down Count

http://community.blynk.cc/t/solved-how-to-countdown/10673/6





#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>

SimpleTimer timer;

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "461366bb2e7d46b7b0a0d4d785da7c3b";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "74170287";
char pass[] = "24063173";

int CountdownRemainReset;
int CountdownRemain;
int CountdownTimer;


void CountdownTimerFunction() {
  CountdownRemain--; // remove 1 every second
  CountdownShowFormatted(CountdownRemain);
  if (!CountdownRemain) { // check if CountdownRemain == 0/FALSE/LOW
    timer.disable(CountdownTimer); // if 0 stop timer
    Blynk.virtualWrite(V1, LOW); // reset START/STOP button status
    Blynk.virtualWrite(V0, "TIMER COMPLETE");
    Blynk.virtualWrite(V6, 255); // LED for timer completed
    Blynk.virtualWrite(V5, 0); // Timer LED status light off
  } else {
    Blynk.virtualWrite(V6, 0); // LED for timer completed
  }
}

// Button Widget (Switch Type): Start/Pause Timer
BLYNK_WRITE(V1) {
  if (param.asInt()) {
    if (CountdownRemain) { // check if there is a time set or not
      timer.enable(CountdownTimer);
      Blynk.virtualWrite(V5, 255); // Timer LED status light on
    } else {
      Blynk.virtualWrite(V1, LOW); // if CountdownRemain is set to 0, then dont start hte timer.
      Blynk.virtualWrite(V0, "COUNTDOWN TIME NOT SET"); // if CountdownRemain is set to 0, then tell the user
    }
  } else {
    timer.disable(CountdownTimer);
    Blynk.virtualWrite(V5, 0); // Timer LED status light off
  }
}

// Button Widget (Momentary): Reset Timer
BLYNK_WRITE(V2) {
  CountdownRemain = CountdownRemainReset; // reset to original start time
}

// Slider Widget (60-180): Set Timer (mins)
BLYNK_WRITE(V3) {
  if (timer.isEnabled(CountdownTimer)) { // only update if timer not running
    Blynk.virtualWrite(V3, param.asInt() ); // if running, refuse to let use change slider
  } else {
    CountdownRemainReset = param.asInt() * 60 + 1; // + 1 set the timer to 1:00:00 instead of 00:59:59
    CountdownRemain = param.asInt() * 60;
    CountdownShowFormatted(CountdownRemain);
  }
}

void CountdownShowFormatted(int seconds) {
  long days = 0;
  long hours = 0;
  long mins = 0;
  long secs = 0;
  String secs_o = ":";
  String mins_o = ":";
  String hours_o = ":";
  secs = seconds; // set the seconds remaining
  mins = secs / 60; //convert seconds to minutes
  hours = mins / 60; //convert minutes to hours
  days = hours / 24; //convert hours to days
  secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
  mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
  hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
  if (secs < 10) {
    secs_o = ":0";
  }
  if (mins < 10) {
    mins_o = ":0";
  }
  if (hours < 10) {
    hours_o = ":0";
  }
  Blynk.virtualWrite(V0, days + hours_o + hours + mins_o + mins + secs_o + secs +"\r\n");
}


void setup() {
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  while (Blynk.connect() == false) {}
  CountdownTimer = timer.setInterval(1000, CountdownTimerFunction);
  timer.disable(CountdownTimer); // disable it on boot
}

void loop() {
  Blynk.run();
  timer.run();
}

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...