Advertisement
Guest User

paste it

a guest
Apr 23rd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. /*
  2. Name: Lighting Project.ino
  3. Created: 4/3/2018 2:28:27 PM
  4. Author: Cameron Orr
  5. */
  6. #define BLYNK_PRINT Serial
  7.  
  8. #include <ESP8266WiFi.h>
  9. #include <BlynkSimpleEsp8266.h>
  10.  
  11. #include <TimeLib.h>
  12. #include <WidgetRTC.h>
  13. WidgetRTC rtc;
  14. BlynkTimer timer;
  15.  
  16. // You should get Auth Token in the Blynk App.
  17. // Go to the Project Settings (nut icon).
  18. char auth[] = "7eb8b088a6584e248b0d6a9c056dcb11";
  19.  
  20. // Your WiFi credentials.
  21. // Set password to "" for open networks.
  22. char ssid[] = "MYHDSB";
  23. char pass[] = "";
  24. int printInfo;
  25. int trigPin1 = 14;
  26. int trigPin2 = 2;
  27. int echoPin1 = 12;
  28. int echoPin2 = 16;
  29. int sequence = 1;
  30. int pplInRoom = 0;
  31. const int light = 0;
  32. int pDistance1 = 0;
  33. int pDistance2 = 0;
  34. long duration, duration2;
  35. bool trigger = false;
  36. bool trigger2 = false;
  37. bool timeStarted = false;
  38. bool timeStarted2 = false;
  39. int timer1 = 0;
  40. int timer2 = 0;
  41. int totalTime = 0;
  42. int totalTime2 = 0;
  43. int distance = 0;
  44. int distance2 = 0;
  45.  
  46.  
  47. BLYNK_CONNECTED() {
  48. rtc.begin();
  49. }
  50.  
  51. BLYNK_WRITE(V2) {
  52. printInfo = param.asInt();
  53. }
  54.  
  55.  
  56.  
  57. void sequence1() {
  58. if (trigger == false) {
  59. digitalWrite(trigPin1, LOW);
  60. digitalWrite(trigPin2, LOW);
  61. digitalWrite(trigPin1, HIGH);
  62. digitalWrite(trigPin2, HIGH);
  63. digitalWrite(trigPin1, LOW);
  64. digitalWrite(trigPin2, LOW);
  65. trigger = true;
  66. }
  67. if (digitalRead(echoPin1) && timeStarted == false) {
  68. timer1 = millis();
  69. timeStarted = true;
  70. }
  71. if (digitalRead(echoPin1) && timeStarted == true) {
  72. totalTime = millis() - timer1;
  73. distance = totalTime * 0.034 / 2;
  74. timeStarted = false;
  75. trigger = false;
  76. Serial.println(distance);
  77. }
  78. if (digitalRead(echoPin2) && timeStarted2 == false) {
  79. timer2 = millis();
  80. timeStarted2 = true;
  81. }
  82. if (digitalRead(echoPin2) && timeStarted2 == true) {
  83. totalTime2 = millis() - timer1;
  84. distance2 = totalTime2 * 0.034 / 2;
  85. timeStarted2 = false;
  86. trigger2 = false;
  87. Serial.println(distance2);
  88. }
  89.  
  90. }
  91. void sequence2() {
  92. pplInRoom += 1;
  93. if (pplInRoom == 1) {
  94. digitalWrite(light, HIGH);
  95. }
  96. Blynk.notify("Person in your room!");
  97. }
  98. void sequence3() {
  99. if (pplInRoom != 0) {
  100. pplInRoom -= 1;
  101. }
  102. if (pplInRoom == 0) {
  103. digitalWrite(light, LOW);
  104. }
  105. }
  106. void setup() {
  107. Serial.begin(9600);
  108. Serial.println("IN SETUP");
  109. Blynk.begin(auth, ssid, pass);
  110. pinMode(trigPin1, OUTPUT);
  111. pinMode(echoPin1, INPUT);
  112. pinMode(trigPin2, OUTPUT);
  113. pinMode(echoPin2, INPUT);
  114. pinMode(light, OUTPUT);
  115. }
  116.  
  117. // the loop function runs over and over again until power down or reset
  118. void loop() {
  119. Blynk.run();
  120. timer.run();
  121. Serial.println("IN LOOP");
  122. Serial.println(String(pplInRoom));
  123. sequence1();
  124.  
  125. if (sequence == 2) {
  126. sequence2();
  127. sequence = 1;
  128. }
  129. else if (sequence == 3) {
  130. sequence3();
  131. sequence = 1;
  132. }
  133. pDistance1 = distance;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement