WiFi-enabled friendship lamps
WiFi-connected lamps that sync in real time over Arduino Cloud, so a tap on one lights up the other, no matter the distance
Devices & Components
1
Arduino® Nano ESP32
Software & Tools
Arduino IDE
Project description
Code
cpp
1/* 2 3 Arduino IoT Cloud Variables description 4 5 The following variables are automatically generated and updated when changes are made to the Thing 6 7 bool lamp1touched; 8 bool lamp2touched; 9 10 Variables which are marked as READ/WRITE in the Cloud Thing will also have functions 11 which are called when their values are changed from the Dashboard. 12 These functions are generated with the Thing and added at the end of this sketch. 13*/ 14#include "thingProperties.h" 15#include <ESP32Servo.h> 16 17Servo myservo1, myservo2; 18 19const int TOUCH_PIN = D12; 20 21const int TRANS_LED = D2; 22 23const int SERVO1_PIN = D10; 24const int SERVO2_PIN = D11; 25 26// Servo angles (calibrate to your box) 27const int OPEN1_ANGLE = 2; 28const int OPEN2_ANGLE = 178; 29const int CLOSE1_ANGLE = 80; 30const int CLOSE2_ANGLE = 101; 31 32const uint32_t WINDOW_MS = 10000UL; // time to wait for the other lamp 33const uint32_t SEQUENCE_MS = 10000UL; // how long doors stay open 34 35enum LampState { IDLE, WAITING, SEQUENCE }; 36LampState state = IDLE; 37 38uint32_t windowStart = 0; 39uint32_t doorsOpenStart = 0; 40 41bool prevLamp1 = false; 42bool prevLamp2 = false; 43bool startedByLamp1 = false; // remember which lamp opened the window 44 45void setup() { 46 Serial.begin(115200); 47 delay(1500); 48 49 pinMode(TOUCH_PIN, INPUT); 50 51 pinMode(TRANS_LED, OUTPUT); 52 53 analogWrite(TRANS_LED, 0); 54 55 ESP32PWM::allocateTimer(3); 56 myservo1.setPeriodHertz(50); 57 myservo2.setPeriodHertz(50); 58 myservo1.attach(SERVO1_PIN); 59 myservo2.attach(SERVO2_PIN); 60 61 Serial.println("servo test"); 62 myservo1.write(OPEN1_ANGLE); 63 myservo2.write(OPEN2_ANGLE); 64 delay(5000); 65 myservo1.write(CLOSE1_ANGLE); 66 myservo2.write(CLOSE2_ANGLE); 67 delay(500); 68 69 70 71 Serial.println("starting cloud"); 72 initProperties(); 73 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 74 setDebugMessageLevel(2); 75 ArduinoCloud.printDebugInfo(); 76 Serial.println("setup done"); 77 78} 79 80void loop() { 81 ArduinoCloud.update(); 82 83 84 // local touch sensor -> this lamp's synced var 85 bool touched = digitalRead(TOUCH_PIN) == HIGH; 86 lamp2touched = touched; 87 88 // rising-edge detection on both synced vars 89 bool lamp1Rising = lamp1touched && !prevLamp1; 90 bool lamp2Rising = lamp2touched && !prevLamp2; 91 prevLamp1 = lamp1touched; 92 prevLamp2 = lamp2touched; 93 94 switch (state) { 95 case IDLE: 96 if (lamp1Rising || lamp2Rising) { 97 state = WAITING; 98 startedByLamp1 = lamp1Rising; // NEW 99 windowStart = millis(); 100 analogWrite(TRANS_LED, 100); 101 } 102 break; 103 104 case WAITING: { 105 bool otherLampRising = startedByLamp1 ? lamp2Rising : lamp1Rising; // NEW 106 if (otherLampRising) { 107 state = SEQUENCE; 108 doorsOpenStart = millis(); 109 myservo1.write(OPEN1_ANGLE); 110 myservo2.write(OPEN2_ANGLE); 111 } else if (millis() - windowStart >= WINDOW_MS) { 112 state = IDLE; 113 analogWrite(TRANS_LED, 0); 114 } 115 break; 116 } 117 118 case SEQUENCE: 119 if (millis() - doorsOpenStart >= SEQUENCE_MS) { 120 myservo1.write(CLOSE1_ANGLE); 121 myservo2.write(CLOSE2_ANGLE); 122 analogWrite(TRANS_LED, 0); 123 state = IDLE; 124 } 125 break; 126 } 127} 128 129 130 131void onLamp1TouchedChange() { 132 133} 134 135void onLamp2TouchedChange() { 136 137} 138 139 140void onServo1Change() { 141 // for servo testing 142 // myservo1.write(servo1); 143 144} 145 146void onServo2Change() { 147 // for servo testing 148 //myservo2.write(servo2); 149}
Comments
Only logged in users can leave comments