Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Final Project - Laziness Monitor
- *
- *
- * Once uploaded, take a seat on the couch and move the switch to ON.
- * After 1 minute, the circuit will move to Stage1 in which it will indicate flashing Red lights.
- * After another minute it will move to Stage2 and mute your TV.
- * After an additional minute it will move to Stage3, a tone will be played, TV will turn OFF, webhook trigger activated and sends a message to your phone and saves the timestamp.
- *
- * Features:
- * Throught the Stages, you could tap on the circuit, this will trigger an interupt and jump back to Stage1 - Basically adding you more TV time.
- * If you would like to add more time, you could also click on the Button in the integromat app, this will make an HTTP request and add requested time.
- * If we want to stop monitoring, just move the switch back to OFF.
- * If we receive a new Email, Integromat will send a HTTP Request to Turn OFF the TV.
- *
- * The times are configurable and for demonstration we worked with smaller intervals of time.
- *
- *
- * Enjoy and start monitoring your time to become more productive:)!
- */
- #define BLYNK_PRINT SerialUSB
- #define BLYNK_DEBUG SerialUSB
- #include <ESP8266_Lib.h>
- #include <BlynkSimpleShieldEsp8266.h>
- #include <Adafruit_CircuitPlayground.h>
- //
- #include <Wire.h>
- #include <SPI.h>
- //
- char auth[] = <YOUR_TOKEN_CODE>;
- char ssid[] = <WIFI_NAME>;
- char pass[] = <WIFI_PASS>;
- bool inFirstStage;
- bool tapped = false;
- #define CLICKTHRESHHOLD 120
- #define EspSerial Serial1
- #define ESP8266_BAUD 9600
- ESP8266 wifi(&EspSerial);
- unsigned long previousMillis = 0;
- unsigned long previousMillisLed = 0;
- long intervalLed = 800; // Flashing led that will indicate monitor is on
- const long intervalAlert = 10000; // Every 10 seconds
- const long oneMinuteInterval = 60000; // 1 minute in miliseconds
- const int ledPin = 13;
- int tones[] = {261, 295, 330, 349, 392, 440, 495, 523, 587, 659};
- int turn_off_tone[] = {523, 493, 440, 392, 370, 311, 293, 261};
- int ledState = LOW;
- boolean firstPlay = true;
- // Samsung TV Commands using NECx protocol
- #define MY_PROTOCOL NECX
- #define MY_BITS 32
- #define MY_MUTE 0xE0E0F00F
- #define MY_POWER 0xE0E040BF
- void setup() {
- CircuitPlayground.begin();
- SerialUSB.begin(9600);
- EspSerial.begin(ESP8266_BAUD);
- delay(10);
- Blynk.begin(auth, wifi, ssid, pass);
- pinMode(ledPin, OUTPUT);
- CircuitPlayground.setAccelRange(LIS3DH_RANGE_2_G);
- CircuitPlayground.setAccelTap(1, CLICKTHRESHHOLD);
- attachInterrupt(digitalPinToInterrupt(CPLAY_LIS3DH_INTERRUPT), tappedd, FALLING);
- }
- void loop() {
- Blynk.run();
- while (CircuitPlayground.slideSwitch()){
- // Switch turned 'On' - In order to start tracking time "wasted" and play tone
- if (firstPlay){
- for (int i = 0; i < 7; i ++){
- CircuitPlayground.playTone(tones[i], 100);
- }
- firstPlay = false; // Play only once
- }
- ledIndicator();
- if (millis() > oneMinuteInterval) {
- // After 1 minute -> Stage 1
- Stage1();
- }
- }
- }
- void Stage1(){
- // Stage1 - Red flashing lights indicate when we moved into this stage.
- // Stage preparations
- inFirstStage = true;
- tapped = false;
- unsigned long stageSwitchMillis = millis();
- while (millis() - stageSwitchMillis <= oneMinuteInterval){
- for (int i = 0; i < 10; ++i) {
- CircuitPlayground.setPixelColor(i, 255, 0, 0);
- delay(50);
- CircuitPlayground.setPixelColor(i, 0, 0, 0);
- }
- }
- // Stage preparations
- intervalLed = intervalLed - 300;
- Stage2();
- }
- void Stage2(){
- // Stage2 - Silent mode, mutes TV
- // Stage preparations
- tapped = false;
- inFirstStage = false;
- // Mute TV
- CircuitPlayground.irSend.send(MY_PROTOCOL,MY_MUTE,MY_BITS);
- // Wait one more minute until Stage3 - turn Off
- unsigned long stageSwitchMillis = millis();
- while (millis() - stageSwitchMillis <= oneMinuteInterval){
- // Unmutes the TV and goes back to first stage
- if(!inFirstStage && tapped){
- CircuitPlayground.playTone(523, 25);
- CircuitPlayground.playTone(587, 25);
- CircuitPlayground.irSend.send(MY_PROTOCOL,MY_MUTE,MY_BITS);
- Stage1();
- }
- }
- // Stage preparations
- intervalLed = intervalLed - 300;
- Stage3();
- }
- void Stage3(){
- // Stage3 - 'Finish' tone, turn Off TV and send text to phone via Blynk Webhook + Integromat
- for (int i = 0; i < 8; ++i) {
- CircuitPlayground.playTone(turn_off_tone[i], 50);
- }
- // Turn Off
- CircuitPlayground.irSend.send(MY_PROTOCOL,MY_POWER,MY_BITS);
- // Blynk Webhook triggered activation - Sends notification to phone and saves the timestamp in google sheets
- Blynk.run();
- Blynk.virtualWrite(V2, 100);
- }
- void ledIndicator(){
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillisLed >= intervalLed) {
- previousMillisLed = currentMillis;
- ledState = !ledState;
- digitalWrite(ledPin, ledState);
- }
- }
- // Recognizes tap and gives the user extra TV time (Stage1)
- void tappedd(void) {
- if(!inFirstStage){
- tapped = true;
- }
- }
- // Trigger button from phone, gives extra TV time (moves to Stage1) - Integromat HTTP Request
- BLYNK_WRITE(V2) {
- // Green light indicates adding time
- for (int i = 0; i < 10; ++i) {
- CircuitPlayground.setPixelColor(i, 0, 255, 0);
- }
- // Preparations
- intervalLed = 800;
- Stage1();
- }
- // Triggered when new Email received in Inbox, Turns OFF TV - Integromat HTTP Request
- BLYNK_WRITE(V3) {
- // Red light indicates turning OFF
- for (int i = 0; i < 10; ++i) {
- CircuitPlayground.setPixelColor(i, 255, 0, 0);
- }
- // Turn Off
- CircuitPlayground.irSend.send(MY_PROTOCOL,MY_POWER,MY_BITS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement