Advertisement
Guest User

Untitled

a guest
Feb 24th, 2021
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. /*
  2. * Final Project - Laziness Monitor
  3. *
  4. *
  5. * Once uploaded, take a seat on the couch and move the switch to ON.
  6. * After 1 minute, the circuit will move to Stage1 in which it will indicate flashing Red lights.
  7. * After another minute it will move to Stage2 and mute your TV.
  8. * 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.
  9. *
  10. * Features:
  11. * 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.
  12. * 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.
  13. * If we want to stop monitoring, just move the switch back to OFF.
  14. * If we receive a new Email, Integromat will send a HTTP Request to Turn OFF the TV.
  15. *
  16. * The times are configurable and for demonstration we worked with smaller intervals of time.
  17. *
  18. *
  19. * Enjoy and start monitoring your time to become more productive:)!
  20. */
  21.  
  22.  
  23. #define BLYNK_PRINT SerialUSB
  24. #define BLYNK_DEBUG SerialUSB
  25.  
  26. #include <ESP8266_Lib.h>
  27. #include <BlynkSimpleShieldEsp8266.h>
  28. #include <Adafruit_CircuitPlayground.h>
  29.  
  30. //
  31. #include <Wire.h>
  32. #include <SPI.h>
  33. //
  34.  
  35.  
  36. char auth[] = <YOUR_TOKEN_CODE>;
  37. char ssid[] = <WIFI_NAME>;
  38. char pass[] = <WIFI_PASS>;
  39.  
  40. bool inFirstStage;
  41. bool tapped = false;
  42. #define CLICKTHRESHHOLD 120
  43.  
  44. #define EspSerial Serial1
  45. #define ESP8266_BAUD 9600
  46.  
  47. ESP8266 wifi(&EspSerial);
  48.  
  49. unsigned long previousMillis = 0;
  50. unsigned long previousMillisLed = 0;
  51. long intervalLed = 800; // Flashing led that will indicate monitor is on
  52. const long intervalAlert = 10000; // Every 10 seconds
  53. const long oneMinuteInterval = 60000; // 1 minute in miliseconds
  54. const int ledPin = 13;
  55.  
  56. int tones[] = {261, 295, 330, 349, 392, 440, 495, 523, 587, 659};
  57. int turn_off_tone[] = {523, 493, 440, 392, 370, 311, 293, 261};
  58. int ledState = LOW;
  59. boolean firstPlay = true;
  60.  
  61. // Samsung TV Commands using NECx protocol
  62. #define MY_PROTOCOL NECX
  63. #define MY_BITS 32
  64. #define MY_MUTE 0xE0E0F00F
  65. #define MY_POWER 0xE0E040BF
  66.  
  67. void setup() {
  68. CircuitPlayground.begin();
  69.  
  70. SerialUSB.begin(9600);
  71. EspSerial.begin(ESP8266_BAUD);
  72. delay(10);
  73. Blynk.begin(auth, wifi, ssid, pass);
  74.  
  75. pinMode(ledPin, OUTPUT);
  76.  
  77. CircuitPlayground.setAccelRange(LIS3DH_RANGE_2_G);
  78. CircuitPlayground.setAccelTap(1, CLICKTHRESHHOLD);
  79. attachInterrupt(digitalPinToInterrupt(CPLAY_LIS3DH_INTERRUPT), tappedd, FALLING);
  80. }
  81.  
  82.  
  83. void loop() {
  84.  
  85. Blynk.run();
  86.  
  87. while (CircuitPlayground.slideSwitch()){
  88. // Switch turned 'On' - In order to start tracking time "wasted" and play tone
  89. if (firstPlay){
  90. for (int i = 0; i < 7; i ++){
  91. CircuitPlayground.playTone(tones[i], 100);
  92. }
  93. firstPlay = false; // Play only once
  94. }
  95. ledIndicator();
  96.  
  97. if (millis() > oneMinuteInterval) {
  98. // After 1 minute -> Stage 1
  99. Stage1();
  100. }
  101. }
  102. }
  103.  
  104.  
  105. void Stage1(){
  106. // Stage1 - Red flashing lights indicate when we moved into this stage.
  107. // Stage preparations
  108. inFirstStage = true;
  109. tapped = false;
  110.  
  111. unsigned long stageSwitchMillis = millis();
  112. while (millis() - stageSwitchMillis <= oneMinuteInterval){
  113. for (int i = 0; i < 10; ++i) {
  114. CircuitPlayground.setPixelColor(i, 255, 0, 0);
  115. delay(50);
  116. CircuitPlayground.setPixelColor(i, 0, 0, 0);
  117. }
  118. }
  119.  
  120. // Stage preparations
  121. intervalLed = intervalLed - 300;
  122. Stage2();
  123. }
  124.  
  125.  
  126. void Stage2(){
  127. // Stage2 - Silent mode, mutes TV
  128. // Stage preparations
  129. tapped = false;
  130. inFirstStage = false;
  131.  
  132. // Mute TV
  133. CircuitPlayground.irSend.send(MY_PROTOCOL,MY_MUTE,MY_BITS);
  134.  
  135. // Wait one more minute until Stage3 - turn Off
  136. unsigned long stageSwitchMillis = millis();
  137. while (millis() - stageSwitchMillis <= oneMinuteInterval){
  138.  
  139. // Unmutes the TV and goes back to first stage
  140. if(!inFirstStage && tapped){
  141. CircuitPlayground.playTone(523, 25);
  142. CircuitPlayground.playTone(587, 25);
  143. CircuitPlayground.irSend.send(MY_PROTOCOL,MY_MUTE,MY_BITS);
  144. Stage1();
  145. }
  146. }
  147.  
  148. // Stage preparations
  149. intervalLed = intervalLed - 300;
  150. Stage3();
  151. }
  152.  
  153.  
  154. void Stage3(){
  155. // Stage3 - 'Finish' tone, turn Off TV and send text to phone via Blynk Webhook + Integromat
  156. for (int i = 0; i < 8; ++i) {
  157. CircuitPlayground.playTone(turn_off_tone[i], 50);
  158. }
  159.  
  160. // Turn Off
  161. CircuitPlayground.irSend.send(MY_PROTOCOL,MY_POWER,MY_BITS);
  162.  
  163. // Blynk Webhook triggered activation - Sends notification to phone and saves the timestamp in google sheets
  164. Blynk.run();
  165. Blynk.virtualWrite(V2, 100);
  166. }
  167.  
  168.  
  169. void ledIndicator(){
  170. unsigned long currentMillis = millis();
  171. if (currentMillis - previousMillisLed >= intervalLed) {
  172. previousMillisLed = currentMillis;
  173. ledState = !ledState;
  174. digitalWrite(ledPin, ledState);
  175. }
  176. }
  177.  
  178.  
  179. // Recognizes tap and gives the user extra TV time (Stage1)
  180. void tappedd(void) {
  181. if(!inFirstStage){
  182. tapped = true;
  183. }
  184. }
  185.  
  186.  
  187. // Trigger button from phone, gives extra TV time (moves to Stage1) - Integromat HTTP Request
  188. BLYNK_WRITE(V2) {
  189. // Green light indicates adding time
  190. for (int i = 0; i < 10; ++i) {
  191. CircuitPlayground.setPixelColor(i, 0, 255, 0);
  192. }
  193.  
  194. // Preparations
  195. intervalLed = 800;
  196. Stage1();
  197. }
  198.  
  199.  
  200. // Triggered when new Email received in Inbox, Turns OFF TV - Integromat HTTP Request
  201. BLYNK_WRITE(V3) {
  202. // Red light indicates turning OFF
  203. for (int i = 0; i < 10; ++i) {
  204. CircuitPlayground.setPixelColor(i, 255, 0, 0);
  205. }
  206. // Turn Off
  207. CircuitPlayground.irSend.send(MY_PROTOCOL,MY_POWER,MY_BITS);
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement