Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: Lighting Project.ino
- Created: 4/3/2018 2:28:27 PM
- Author: Cameron Orr
- */
- #define BLYNK_PRINT Serial
- #include <ESP8266WiFi.h>
- #include <BlynkSimpleEsp8266.h>
- #include <TimeLib.h>
- #include <WidgetRTC.h>
- WidgetRTC rtc;
- BlynkTimer timer;
- // You should get Auth Token in the Blynk App.
- // Go to the Project Settings (nut icon).
- char auth[] = "7eb8b088a6584e248b0d6a9c056dcb11";
- // Your WiFi credentials.
- // Set password to "" for open networks.
- char ssid[] = "MYHDSB";
- char pass[] = "";
- int printInfo;
- int trigPin1 = 14;
- int trigPin2 = 2;
- int echoPin1 = 12;
- int echoPin2 = 16;
- int sequence = 1;
- int pplInRoom = 0;
- const int light = 0;
- int pDistance1 = 0;
- int pDistance2 = 0;
- long duration, duration2;
- bool trigger = false;
- bool trigger2 = false;
- bool timeStarted = false;
- bool timeStarted2 = false;
- int timer1 = 0;
- int timer2 = 0;
- int totalTime = 0;
- int totalTime2 = 0;
- int distance = 0;
- int distance2 = 0;
- BLYNK_CONNECTED() {
- rtc.begin();
- }
- BLYNK_WRITE(V2) {
- printInfo = param.asInt();
- }
- void sequence1() {
- if (trigger == false) {
- digitalWrite(trigPin1, LOW);
- digitalWrite(trigPin2, LOW);
- digitalWrite(trigPin1, HIGH);
- digitalWrite(trigPin2, HIGH);
- digitalWrite(trigPin1, LOW);
- digitalWrite(trigPin2, LOW);
- trigger = true;
- }
- if (digitalRead(echoPin1) && timeStarted == false) {
- timer1 = millis();
- timeStarted = true;
- }
- if (digitalRead(echoPin1) && timeStarted == true) {
- totalTime = millis() - timer1;
- distance = totalTime * 0.034 / 2;
- timeStarted = false;
- trigger = false;
- Serial.println(distance);
- }
- if (digitalRead(echoPin2) && timeStarted2 == false) {
- timer2 = millis();
- timeStarted2 = true;
- }
- if (digitalRead(echoPin2) && timeStarted2 == true) {
- totalTime2 = millis() - timer1;
- distance2 = totalTime2 * 0.034 / 2;
- timeStarted2 = false;
- trigger2 = false;
- Serial.println(distance2);
- }
- }
- void sequence2() {
- pplInRoom += 1;
- if (pplInRoom == 1) {
- digitalWrite(light, HIGH);
- }
- Blynk.notify("Person in your room!");
- }
- void sequence3() {
- if (pplInRoom != 0) {
- pplInRoom -= 1;
- }
- if (pplInRoom == 0) {
- digitalWrite(light, LOW);
- }
- }
- void setup() {
- Serial.begin(9600);
- Serial.println("IN SETUP");
- Blynk.begin(auth, ssid, pass);
- pinMode(trigPin1, OUTPUT);
- pinMode(echoPin1, INPUT);
- pinMode(trigPin2, OUTPUT);
- pinMode(echoPin2, INPUT);
- pinMode(light, OUTPUT);
- }
- // the loop function runs over and over again until power down or reset
- void loop() {
- Blynk.run();
- timer.run();
- Serial.println("IN LOOP");
- Serial.println(String(pplInRoom));
- sequence1();
- if (sequence == 2) {
- sequence2();
- sequence = 1;
- }
- else if (sequence == 3) {
- sequence3();
- sequence = 1;
- }
- pDistance1 = distance;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement