Components and supplies
DHT22 Temperature Sensor
CHIHAI DC Motor - 12V 200rpm Encoder with Mounting Bracket
Arduino MKR1000
Photo resistor
Geekcreit L298N Dual H Bridge Stepper Motor Driver Board For Arduino
Apps and platforms
Blynk
Project description
Code
Chicken Coop Code - V1
arduino
- Including Blynk reconnect logic - Reduced data exchange interval
1 2/* Code developed by BMic on 21 May 2018 3 * Used to connect with Blynk IOT service and to control a chicken coop: 4 * - Door control based on encoder value 5 * - Environmental monitoring: Temperature, Humidity and light 6 * 7 * Change V1: 8 * - Decrease amount of values sent to Blynk only upon Change or every minute for historical values 9 * - Split data exchanges in Pull and Push mode 10 * - Include input button for Open/Close commands and Reed position switches 11 * - Reconnection logic to Blynk in case it's down 12 * 13*/ 14#define BLYNK_PRINT Serial 15//#define BLYNK_DEBUG // Optional, this enables more detailed prints 16 17#include "DHT.h" 18#include <SPI.h> 19#include <WiFi101.h> 20#include <BlynkSimpleMKR1000.h> 21 22//========================================================== // 23//============== IO & Variables definition ================= // 24//========================================================== // 25 26//--- Digital inputs ---// 27#define encoderPinA 0 // Encoder input Channel A 28#define encoderPinB 1 // Encoder input Channel A 29#define I_CloseDoorSwitch 2 // Reed switch for Closed position 30#define I_OpenDoorSwitch 3 // Reed switch for Open position 31#define I_CloseDoorButton 4 // Button to send Close command 32#define I_OpenDoorButton 5 // Button to send Open command 33#define O_MotorDriverOut1 7 // Motor Output to Close the Door 34#define O_MotorDriverOut2 8 // Motor Output to Open the Door 35 36//--- Analog inputs ---// 37#define I_LightSensor A0 // Analog input to measure Photo resistor 0 - 1023 38 39//--- DHT Sensor input ---// 40#define DHTPIN 9 41#define DHTTYPE DHT22 42DHT dht(DHTPIN, DHTTYPE); 43 44float hum; //Stores humidity value 45float temp; //Stores temperature value 46 47//--- Light sensor ---// 48int LightValue = 0; // Actual analog input measurement 49int deltaLight = 0; // Difference between Light threshold and actual value 50int deltaLightOld = 0; // Previous value to trigger a value change 51int deltaDark = 0; // Difference between Dark threshold and actual value 52int deltaDarkOld = 0; // Previous value to trigger a value change 53int ThresHoldGettingDark = 25; // Treshold value to indicate it's getting Dark and door should close 54int ThresHoldGettingLight = 650; // Treshold value to indicate it's getting Light and door should open 55 56//--- Encoder input ---// 57volatile int encoderPos = 0; // Volatile int should be used for interrupt values 58int encoderClosePos = 0; // Variable to store the encoder value for Closed position 59int encoderPosOld = 0; // Previous value to trigger a value change 60 61//--- Blynk command variables ---/ 62int buttonOpenValue; // Virtual Button to send Open command from Blynk 63int buttonCloseValue; // Virtual Button to send Close command from Blynk 64int switchOpenValue; // Virtual Button to send Open position from Blynk in case no reed switch is used 65int switchCloseValue; // Virtual Button to send Close position from Blynk in case no reed switch is used 66int operatingModeValue; // Set mode to work in automatic mode using Photo resistor or manual operation 67 68//--- Used for sequential programming steps during initialize calibration procedure --- 69int step = 0; // Squencer in void Initializing() 70 71//--- The different states of the system --- 72enum states {INITIALIZE, DOOR_ERROR, DOOR_IS_OPEN, CLOSING_DOOR, DOOR_IS_CLOSED, OPENING_DOOR}; 73String OldState; 74 75//--- Holds the initial state of the system --- 76states state; 77 78//========================================================== // 79//=========== Blynk connection with Read/Write ============== // 80//========================================================== // 81// You should get Auth Token in the Blynk App. 82// Go to the Project Settings (nut icon). 83char auth[] = "xxxxxxxxxxxxxxxxx"; 84 85// Your WiFi credentials. 86// Set password to "" for open networks. 87int StatusWifi = WL_IDLE_STATUS; // the Wifi radio's status 88char ssid[] = "WiFi-SSID"; 89char pass[] = "Password"; 90 91int DisconnectCount = 0; // Count number of disconnects 92int ReCnctFlag; // Reconnection Flag 93int ReCnctCount = 0; // Reconnection counter 94 95//========================================================== // 96//============== Initial setup at Startup ================= // 97//========================================================== // 98 99BlynkTimer timer; // Start Timer based on SimpleTimer supporting to schedule 16 timers 100 101void EncoderTimerEvent() // Fast timer of 1 second to Push values to Blynk 102{ 103 //--- Encoder ---// 104 if(encoderPosOld != encoderPos){ // Check if encoder value has changed. If yes, send to Blynk. 105 Blynk.virtualWrite(V2, encoderPos); 106 encoderPosOld = encoderPos; 107 Serial.print("Previous encoder position was: "); 108 Serial.println(encoderPosOld); 109 Serial.print("Actual encoder position is: "); 110 Serial.println(encoderPos); 111 } 112} 113 114void BlynkTimerEvent() // Slow timer of 1 minute to Push values to Blynk 115{ 116 //--- DHT22 ---// 117 hum = dht.readHumidity(); 118 temp= dht.readTemperature(); 119 Blynk.virtualWrite(V3, temp); 120 Blynk.virtualWrite(V4, hum); 121 122 //--- LUX ---// 123 LightValue = analogRead(I_LightSensor); 124 Blynk.virtualWrite(V1, LightValue); 125 126 //--- Encoder ---// 127 Blynk.virtualWrite(V2, encoderPos); // Only upon change (EncoderTimerEvent) messes up the graph in Blynk 128 129 //--- Reconnect counter ---// 130 Blynk.virtualWrite(V0, ReCnctCount); 131} 132 133// Pull requests from Blynk --> Only Pull when app is open since we don't need those as historical values!! 134// Light Treshold -> Frequency is set in Blynk app and will only work when app is active in foreground!! 135 136 BLYNK_READ(V11){ // When Blynk requests a new value, calculate value and write back 137 deltaLight = ThresHoldGettingLight - LightValue; 138 Blynk.virtualWrite(V11, deltaLight); 139 } 140 141 BLYNK_READ(V12){ // When Blynk requests a new value, calculate value and write back 142 deltaDark = ThresHoldGettingDark - LightValue; 143 Blynk.virtualWrite(V12, deltaDark); 144 } 145 146//--- Command's from Blynk to MKR1000 ---// 147//--- These functions will be called every time a blynk Widget value is changed ---// 148 149 BLYNK_WRITE(V21) 150{ 151 ThresHoldGettingDark = param.asInt(); 152} 153 154 BLYNK_WRITE(V22) 155{ 156 ThresHoldGettingLight = param.asInt(); 157} 158 159 BLYNK_WRITE(V7) 160{ 161 buttonOpenValue = param.asInt(); 162} 163 164 BLYNK_WRITE(V8) 165{ 166 buttonCloseValue = param.asInt(); 167} 168 169 BLYNK_WRITE(V9) 170{ 171 switchOpenValue = param.asInt(); 172} 173 174 BLYNK_WRITE(V10) 175{ 176 switchCloseValue = param.asInt(); 177} 178 179 BLYNK_WRITE(V20) 180{ 181 operatingModeValue = param.asInt(); 182} 183 184//========================================================== // 185//================== Initial Setup LOOP ==================== // 186//========================================================== // 187 188void setup() 189{ 190 //Initialize serial and wait for port to open: 191 Serial.begin(9600); 192 while (!Serial) { 193 ; // wait for serial port to connect. Needed for native USB port only 194 } 195 Serial.println(); 196 197 //Check for the presence of the shield: 198 /**************************************************************************************/ 199 if (WiFi.status() == WL_NO_SHIELD) { 200 Serial.println("WiFi shield not present"); 201 /*Don't continue:*/ 202 while (true); 203 } 204 /**************************************************************************************/ 205 206//--- initialize digital pin LED_BUILTIN as an output --- 207 pinMode(I_CloseDoorSwitch, INPUT_PULLUP); 208 pinMode(I_OpenDoorSwitch, INPUT_PULLUP); 209 pinMode(I_CloseDoorButton, INPUT_PULLUP); 210 pinMode(I_OpenDoorButton, INPUT_PULLUP); 211 pinMode(O_MotorDriverOut1, OUTPUT); 212 pinMode(O_MotorDriverOut2, OUTPUT); 213 pinMode(encoderPinA, INPUT_PULLUP); 214 pinMode(encoderPinB, INPUT_PULLUP); 215 216 state = INITIALIZE; 217 218 attachInterrupt(0, doEncoder, CHANGE); // Use Interrupt to count reliable every encoder input 219 220 Blynk.begin(auth, ssid, pass); 221 dht.begin(); 222 223 timer.setInterval(1000L, EncoderTimerEvent); // One second update timer for Blynk 224 timer.setInterval(60000L, BlynkTimerEvent); // One minute update timer for Blynk 225 226} 227 228 BLYNK_CONNECTED() { 229 Serial.println("Connected"); 230 ReCnctCount = 0; 231} 232 233 234//========================================================== // 235//========== Finite State Machine - Main LOOP ============== // 236//========================================================== // 237void loop() 238{ 239 240 timer.run(); // Starts Blynk Timer 241 242// Reconnect routine tried from: https://community.blynk.cc/t/mega-esp-will-freeze-if-there-is-a-connection-problem/25124/2 243// Not sure if this is actually needed or even working 244if (Blynk.connected()) { 245 Blynk.run(); // Starts Blynk connection 246} 247else if (ReCnctFlag == 0) { 248 ReCnctFlag = 1; // Set reconnection Flag 249 Serial.println("Starting reconnection timer in 30 seconds..."); 250 timer.setTimeout(30000L, []() { // Lambda Reconnection Timer Function 251 ReCnctFlag = 0; // Reset reconnection Flag 252 ReCnctCount++; // Increment reconnection Counter 253 Serial.print("Attempting reconnection #"); 254 Serial.println(ReCnctCount); 255 Blynk.connect(); // Try to reconnect to the server 256 }); 257} 258 259// --- Sequencer logic to track the State of the door and specific monitor events ---// 260 switch (state) 261 { 262 case INITIALIZE: 263 Initializing(); 264 break; 265 266 case DOOR_IS_OPEN: 267 StopDoorMotor(); 268 CheckDownButton(); 269 CheckGettingDark(); 270 ChangeState("Door is OPEN"); 271 break; 272 273 case CLOSING_DOOR: 274 ClosingDoor(); 275 CheckCloseSwitch(); 276 CheckEncoderCloseValue(); 277 ChangeState("Closing door"); 278 break; 279 280 case DOOR_IS_CLOSED: 281 StopDoorMotor(); 282 CheckUpButton(); 283 CheckGettingLight(); 284 ChangeState("Door is CLOSED"); 285 break; 286 287 case OPENING_DOOR: 288 OpeningDoor(); 289 CheckOpenSwitch(); 290 CheckEncoderOpenValue(); 291 ChangeState("Opening door"); 292 break; 293 294 case DOOR_ERROR: 295 ChangeState("Door Error"); 296 break; 297 } 298} 299 300//--- Not sure if the previous routine within the Main LOOP is needed or not, so I use both for now ---// 301BLYNK_DISCONNECTED() 302{ 303 Serial.println("Blynk is disconnected!"); 304 305 if(Blynk.connected()!=true) 306 { 307 DisconnectCount++; 308 Serial.println("Blynk not conected counter = "); 309 Serial.println(DisconnectCount); 310 Blynk.connect(); 311 } 312} 313 314//========================================================== // 315//============== Finite State - Functions ================= // 316//========================================================== // 317 318// --------------- Initializing Function --------------- 319void Initializing() 320 321/* Still to Do: 322 * 1. Record Time to Close 323 * 2. Save the time in a variable and use it during Opening/Closing 324 * 3. If time > preset value and encoder value is not reached -> ERROR 325 * 4. When error, retry to open/close 2..3..?? times and then return critical error 326*/ 327/* Initializing Function: 328 * - After rebooting or resetting Arduino, first step will be to calibrate open/close position based on encoder value 329 * case 0: Sequencer will wait for your Open command to open the door or skip the step if the door is already in the Open position 330 * case 1: Door will open until Open switch is triggered or Virtual Open position is confirmed and EncoderPos value is set to 0 331 * case 2: Sequencer will wait for your Close command to close the door 332 * case 3: Door will close until Close switch is triggered or Virtual Open position is confirmed and encoderClosePos value is set 333 */ 334 335{ 336 switch(step) 337 { 338 case 0: 339 ChangeState("Give Open Command"); 340 if(buttonOpenValue == HIGH or digitalRead(I_OpenDoorButton) == LOW) 341 { 342 OpeningDoor(); 343 step = 1; 344 } 345 else if(switchOpenValue == HIGH or digitalRead(I_OpenDoorSwitch) == LOW) 346 { 347 StopDoorMotor(); 348 encoderPos = 0; 349 step = 2; 350 } 351 break; 352 353 case 1: 354 ChangeState("Opening - Stop when Open"); 355 if(switchOpenValue == HIGH or digitalRead(I_OpenDoorSwitch) == LOW) 356 { 357 StopDoorMotor(); 358 encoderPos = 0; 359 step = 2; 360 } 361 break; 362 363 case 2: 364 ChangeState("Give Close Command"); 365 if(buttonCloseValue == HIGH or digitalRead(I_CloseDoorButton) == LOW) 366 { 367 ClosingDoor(); 368 step = 3; 369 } 370 break; 371 372 case 3: 373 ChangeState("Closing - Stop when Closed"); 374 if(switchCloseValue == HIGH or digitalRead(I_CloseDoorSwitch) == LOW) 375 { 376 StopDoorMotor(); 377 encoderClosePos = encoderPos; 378 state = DOOR_IS_CLOSED; 379 } 380 break; 381 } 382 383} 384 385// --------------- MOTOR Functions --------------- // 386void StopDoorMotor() 387{ 388 digitalWrite(O_MotorDriverOut1, LOW); 389 digitalWrite(O_MotorDriverOut2, LOW); 390} 391 392void ClosingDoor() 393{ 394 digitalWrite(O_MotorDriverOut1, HIGH); 395 digitalWrite(O_MotorDriverOut2, LOW); 396} 397 398void OpeningDoor() 399{ 400 digitalWrite(O_MotorDriverOut1, LOW); 401 digitalWrite(O_MotorDriverOut2, HIGH); 402} 403 404// --------------- Open/Close command Buttons --------------- // 405void CheckDownButton() 406{ 407 if(buttonCloseValue == HIGH or digitalRead(I_CloseDoorButton) == LOW) // Virtual value from Blynk or input button 408 { 409 state = CLOSING_DOOR; 410 } 411} 412 413void CheckUpButton() 414{ 415 if(buttonOpenValue == HIGH or digitalRead(I_OpenDoorButton) == LOW) // Virtual value from Blynk or input button 416 { 417 state = OPENING_DOOR; 418 } 419} 420 421// --------------- Open/Close Input Switches --------------- // 422void CheckCloseSwitch() 423{ 424 if(switchCloseValue == HIGH or digitalRead(I_CloseDoorSwitch) == LOW) // Virtual value from Blynk or input switch 425 { 426 state = DOOR_IS_CLOSED; 427 } 428} 429 430void CheckOpenSwitch() 431{ 432 if(switchOpenValue == HIGH or digitalRead(I_OpenDoorSwitch) == LOW) // Virtual value from Blynk or input switch 433 { 434 state = DOOR_IS_OPEN; 435 } 436} 437 438// --- Stop Closing Door if encoder value >= Closed value registered during Initializing loop --- // 439void CheckEncoderCloseValue() 440{ 441 if(encoderPos >= encoderClosePos) 442 { 443 StopDoorMotor(); 444 state = DOOR_IS_CLOSED; 445 } 446} 447 448// --- Stop Opening Door if encoder value <= 0 which is calibrated during Initializing loop --- // 449void CheckEncoderOpenValue() 450{ 451 if(encoderPos <= 0) 452 { 453 StopDoorMotor(); 454 state = DOOR_IS_OPEN; 455 } 456} 457 458// --------------- Light Sensor --------------- // 459void CheckGettingDark() 460{ 461 if(operatingModeValue && LightValue < ThresHoldGettingDark) 462 { 463 state = CLOSING_DOOR; 464 } 465} 466 467void CheckGettingLight() 468{ 469 if(operatingModeValue && LightValue > ThresHoldGettingLight) 470 { 471 state = OPENING_DOOR; 472 } 473} 474 475// --------------- Status String --------------- // 476void ChangeState(String Status) 477{ 478 if(OldState != Status) 479 { 480 Blynk.virtualWrite(V6, Status); // Only push to Blynk if value is changed 481 OldState = Status; 482 } 483} 484 485//========================================================== // 486//=========== Interrupts - Detect fast changes ============== // 487//========================================================== // 488void doEncoder() 489{ 490 if(digitalRead(encoderPinA) == digitalRead(encoderPinB)) 491 { 492 encoderPos++; 493 } 494 else 495 { 496 encoderPos--; 497 } 498}
Chicken Coop Code
arduino
1/* Code developed by BMic on 19 March 2018 2 * Used to connect with Blynk IOT service and to control a chicken coop: 3 * - Door control based on encoder value 4 * - Environmental monitoring: Temperature, Humidity and light 5*/ 6 7#include "DHT.h" 8#include <SPI.h> 9#include <WiFi101.h> 10#include <BlynkSimpleMKR1000.h> 11 12 13//========================================================== // 14//============== IO & Variables definition ================= // 15//========================================================== // 16 17#define encoderPinA 0 18#define encoderPinB 1 19#define O_MotorDriverOut1 7 20#define O_MotorDriverOut2 8 21 22#define I_LightSensor A0 23 24#define DHTPIN 9 25#define DHTTYPE DHT22 26DHT dht(DHTPIN, DHTTYPE); 27 28float hum; //Stores humidity value 29float temp; //Stores temperature value 30 31int LightValue = 0; 32int deltaLight = 0; 33int deltaDark = 0; 34 35volatile int encoderPos = 0; 36int encoderClosePos = 0; 37 38//--- Used for sequential programming steps during initialize procedure --- 39int step = 0; 40 41//--- The different states of the system --- 42enum states {INITIALIZE, DOOR_ERROR, DOOR_IS_OPEN, CLOSING_DOOR, DOOR_IS_CLOSED, OPENING_DOOR}; 43String OldState; 44 45states state; 46 47//========================================================== // 48//=========== Blynk connection with Read/Write ============== // 49//========================================================== // 50// You should get Auth Token in the Blynk App. 51// Go to the Project Settings (nut icon). 52char auth[] = "xxxxxxxxxxxxxxxxx"; 53 54// Your WiFi credentials. 55// Set password to "" for open networks. 56char ssid[] = "WiFi-SSID"; 57char pass[] = "password"; 58 59//========================================================== // 60//============== Initial setup at Startup ================= // 61//========================================================== // 62 63BlynkTimer timer; 64 65void setup() 66{ 67 68//--- initialize digital pin LED_BUILTIN as an output --- 69 pinMode(O_MotorDriverOut1, OUTPUT); 70 pinMode(O_MotorDriverOut2, OUTPUT); 71 pinMode(encoderPinA, INPUT_PULLUP); 72 pinMode(encoderPinB, INPUT_PULLUP); 73 74 state = INITIALIZE; 75 76 attachInterrupt(0, doEncoder, CHANGE); 77 78 dht.begin(); 79 timer.setInterval(5000L, dhtTimerEvent); 80 81 Blynk.begin(auth, ssid, pass); 82 timer.setInterval(1000L, BlynkTimerEvent); 83} 84 85int ThresHoldGettingDark = 25; 86 BLYNK_WRITE(V21) 87{ 88 ThresHoldGettingDark = param.asInt(); 89} 90 91int ThresHoldGettingLight = 650; 92 BLYNK_WRITE(V22) 93{ 94 ThresHoldGettingLight = param.asInt(); 95} 96 97void BlynkTimerEvent() 98{ 99 Blynk.virtualWrite(V2, encoderPos); 100} 101 102void dhtTimerEvent() 103{ 104 hum = dht.readHumidity(); 105 temp= dht.readTemperature(); 106 Blynk.virtualWrite(V3, temp); 107 Blynk.virtualWrite(V4, hum); 108 109 LightValue = analogRead(I_LightSensor); 110 deltaLight = ThresHoldGettingLight - LightValue; 111 deltaDark = ThresHoldGettingDark - LightValue; 112 Blynk.virtualWrite(V1, LightValue); 113 Blynk.virtualWrite(V11, deltaLight); 114 Blynk.virtualWrite(V12, deltaDark); 115} 116 117int buttonOpenValue; 118 BLYNK_WRITE(V7) 119{ 120 buttonOpenValue = param.asInt(); 121} 122 123int buttonCloseValue; 124 BLYNK_WRITE(V8) 125{ 126 buttonCloseValue = param.asInt(); 127} 128 129int switchOpenValue; 130 BLYNK_WRITE(V9) 131{ 132 switchOpenValue = param.asInt(); 133} 134 135int switchCloseValue; 136 BLYNK_WRITE(V10) 137{ 138 switchCloseValue = param.asInt(); 139} 140 141int operatingModeValue = 0; 142 BLYNK_WRITE(V20) 143{ 144 operatingModeValue = param.asInt(); 145} 146 147//========================================================== // 148//============== Finite State Machine Loop ================= // 149//========================================================== // 150void loop() 151{ 152 153 Blynk.run(); 154 timer.run(); 155 156 switch (state) 157 { 158 case INITIALIZE: 159 Initializing(); 160 break; 161 162 case DOOR_IS_OPEN: 163 StopDoorMotor(); 164 CheckDownButton(); 165 CheckGettingDark(); 166 ChangeState("Door is OPEN"); 167 break; 168 169 case CLOSING_DOOR: 170 ClosingDoor(); 171 CheckCloseSwitch(); 172 CheckEncoderCloseValue(); 173 ChangeState("Closing door"); 174 break; 175 176 case DOOR_IS_CLOSED: 177 StopDoorMotor(); 178 CheckUpButton(); 179 CheckGettingLight(); 180 ChangeState("Door is CLOSED"); 181 break; 182 183 case OPENING_DOOR: 184 OpeningDoor(); 185 CheckOpenSwitch(); 186 CheckEncoderOpenValue(); 187 ChangeState("Opening door"); 188 break; 189 190 case DOOR_ERROR: 191 ChangeState("Door Error"); 192 break; 193 } 194 195 196} 197 198//========================================================== // 199//============== Finite State - Functions ================= // 200//========================================================== // 201 202// --------------- Initializing Function --------------- 203void Initializing() //Calibrate Encoder values for Open & Closed position 204 205{ 206 switch(step) 207 { 208 case 0: 209 Blynk.virtualWrite(V6, "Give Open Command"); 210 if(buttonOpenValue == HIGH) 211 { 212 OpeningDoor(); 213 step = 1; 214 } 215 else if(switchOpenValue == HIGH) 216 { 217 StopDoorMotor(); 218 encoderPos = 0; 219 step = 2; 220 } 221 break; 222 223 case 1: 224 Blynk.virtualWrite(V6, "Opening - Stop when Open"); 225 if(switchOpenValue == HIGH) 226 { 227 StopDoorMotor(); 228 encoderPos = 0; 229 step = 2; 230 } 231 break; 232 233 case 2: 234 Blynk.virtualWrite(V6, "Give Close Command"); 235 if(buttonCloseValue == HIGH) 236 { 237 ClosingDoor(); 238 step = 3; 239 } 240 break; 241 242 case 3: 243 Blynk.virtualWrite(V6, "Closing - Stop when Closed"); 244 if(switchCloseValue == HIGH) 245 { 246 StopDoorMotor(); 247 encoderClosePos = encoderPos; 248 state = DOOR_IS_CLOSED; 249 } 250 break; 251 } 252 253} 254 255// --------------- MOTOR Functions --------------- 256void StopDoorMotor() 257{ 258 digitalWrite(O_MotorDriverOut1, LOW); 259 digitalWrite(O_MotorDriverOut2, LOW); 260} 261 262void ClosingDoor() 263{ 264 digitalWrite(O_MotorDriverOut1, HIGH); 265 digitalWrite(O_MotorDriverOut2, LOW); 266} 267 268void OpeningDoor() 269{ 270 digitalWrite(O_MotorDriverOut1, LOW); 271 digitalWrite(O_MotorDriverOut2, HIGH); 272} 273 274// --------------- Input Buttons --------------- 275void CheckDownButton() 276{ 277 if(buttonCloseValue == HIGH) // Virtual value from Blynk 278 { 279 state = CLOSING_DOOR; 280 } 281} 282 283void CheckUpButton() 284{ 285 if(buttonOpenValue == HIGH) // Virtual value from Blynk 286 { 287 state = OPENING_DOOR; 288 } 289} 290 291// --------------- Input Switches --------------- 292void CheckCloseSwitch() 293{ 294 if(switchCloseValue == HIGH) // Virtual value from Blynk 295 { 296 state = DOOR_IS_CLOSED; 297 } 298} 299 300void CheckOpenSwitch() 301{ 302 if(switchOpenValue == HIGH) // Virtual value from Blynk 303 { 304 state = DOOR_IS_OPEN; 305 } 306} 307 308void CheckEncoderCloseValue() 309{ 310 if(encoderPos >= encoderClosePos) 311 { 312 StopDoorMotor(); 313 state = DOOR_IS_CLOSED; 314 } 315} 316 317void CheckEncoderOpenValue() 318{ 319 if(encoderPos <= 0) 320 { 321 StopDoorMotor(); 322 state = DOOR_IS_OPEN; 323 } 324} 325 326// --------------- Light Sensor --------------- 327void CheckGettingDark() 328{ 329 if(operatingModeValue && LightValue < ThresHoldGettingDark) 330 { 331 state = CLOSING_DOOR; 332 } 333} 334 335void CheckGettingLight() 336{ 337 if(operatingModeValue && LightValue > ThresHoldGettingLight) 338 { 339 state = OPENING_DOOR; 340 } 341} 342 343// --------------- Status String --------------- 344void ChangeState(String Status) 345{ 346 if(OldState != Status) 347 { 348 Blynk.virtualWrite(V6, Status); 349 OldState = Status; 350 } 351} 352 353//========================================================== // 354//=========== Interrupts - Detect fast changes ============== // 355//========================================================== // 356void doEncoder() 357{ 358 if(digitalRead(encoderPinA) == digitalRead(encoderPinB)) 359 { 360 encoderPos++; 361 } 362 else 363 { 364 encoderPos--; 365 } 366} 367
Chicken Coop Code
arduino
1/* Code developed by BMic on 19 March 2018 2 * Used to connect with 3 Blynk IOT service and to control a chicken coop: 4 * - Door control based on 5 encoder value 6 * - Environmental monitoring: Temperature, Humidity and light 7*/ 8 9#include 10 "DHT.h" 11#include <SPI.h> 12#include <WiFi101.h> 13#include <BlynkSimpleMKR1000.h> 14 15 16//========================================================== 17 // 18//============== IO & Variables definition ================= // 19//========================================================== 20 // 21 22#define encoderPinA 0 23#define encoderPinB 1 24#define O_MotorDriverOut1 25 7 26#define O_MotorDriverOut2 8 27 28#define I_LightSensor A0 29 30#define 31 DHTPIN 9 32#define DHTTYPE DHT22 33DHT dht(DHTPIN, DHTTYPE); 34 35float hum; 36 //Stores humidity value 37float temp; //Stores temperature value 38 39int LightValue 40 = 0; 41int deltaLight = 0; 42int deltaDark = 0; 43 44volatile int encoderPos 45 = 0; 46int encoderClosePos = 0; 47 48//--- Used for sequential programming steps 49 during initialize procedure --- 50int step = 0; 51 52//--- The different states 53 of the system --- 54enum states {INITIALIZE, DOOR_ERROR, DOOR_IS_OPEN, CLOSING_DOOR, 55 DOOR_IS_CLOSED, OPENING_DOOR}; 56String OldState; 57 58states state; 59 60//========================================================== 61 // 62//=========== Blynk connection with Read/Write ============== // 63//========================================================== 64 // 65// You should get Auth Token in the Blynk App. 66// Go to the Project Settings 67 (nut icon). 68char auth[] = "xxxxxxxxxxxxxxxxx"; 69 70// Your WiFi credentials. 71// 72 Set password to "" for open networks. 73char ssid[] = "WiFi-SSID"; 74char 75 pass[] = "password"; 76 77//========================================================== 78 // 79//============== Initial setup at Startup ================= // 80//========================================================== 81 // 82 83BlynkTimer timer; 84 85void setup() 86{ 87 88//--- initialize 89 digital pin LED_BUILTIN as an output --- 90 pinMode(O_MotorDriverOut1, OUTPUT); 91 92 pinMode(O_MotorDriverOut2, OUTPUT); 93 pinMode(encoderPinA, INPUT_PULLUP); 94 95 pinMode(encoderPinB, INPUT_PULLUP); 96 97 state = INITIALIZE; 98 99 attachInterrupt(0, 100 doEncoder, CHANGE); 101 102 dht.begin(); 103 timer.setInterval(5000L, dhtTimerEvent); 104 105 106 Blynk.begin(auth, ssid, pass); 107 timer.setInterval(1000L, BlynkTimerEvent); 108} 109 110int 111 ThresHoldGettingDark = 25; 112 BLYNK_WRITE(V21) 113{ 114 ThresHoldGettingDark 115 = param.asInt(); 116} 117 118int ThresHoldGettingLight = 650; 119 BLYNK_WRITE(V22) 120{ 121 122 ThresHoldGettingLight = param.asInt(); 123} 124 125void BlynkTimerEvent() 126{ 127 128 Blynk.virtualWrite(V2, encoderPos); 129} 130 131void dhtTimerEvent() 132{ 133 134 hum = dht.readHumidity(); 135 temp= dht.readTemperature(); 136 Blynk.virtualWrite(V3, 137 temp); 138 Blynk.virtualWrite(V4, hum); 139 140 LightValue = analogRead(I_LightSensor); 141 142 deltaLight = ThresHoldGettingLight - LightValue; 143 deltaDark = ThresHoldGettingDark 144 - LightValue; 145 Blynk.virtualWrite(V1, LightValue); 146 Blynk.virtualWrite(V11, 147 deltaLight); 148 Blynk.virtualWrite(V12, deltaDark); 149} 150 151int buttonOpenValue; 152 153 BLYNK_WRITE(V7) 154{ 155 buttonOpenValue = param.asInt(); 156} 157 158int buttonCloseValue; 159 160 BLYNK_WRITE(V8) 161{ 162 buttonCloseValue = param.asInt(); 163} 164 165int switchOpenValue; 166 167 BLYNK_WRITE(V9) 168{ 169 switchOpenValue = param.asInt(); 170} 171 172int switchCloseValue; 173 174 BLYNK_WRITE(V10) 175{ 176 switchCloseValue = param.asInt(); 177} 178 179int operatingModeValue 180 = 0; 181 BLYNK_WRITE(V20) 182{ 183 operatingModeValue = param.asInt(); 184} 185 186//========================================================== 187 // 188//============== Finite State Machine Loop ================= // 189//========================================================== 190 // 191void loop() 192{ 193 194 Blynk.run(); 195 timer.run(); 196 197 switch 198 (state) 199 { 200 case INITIALIZE: 201 Initializing(); 202 break; 203 204 205 case DOOR_IS_OPEN: 206 StopDoorMotor(); 207 CheckDownButton(); 208 209 CheckGettingDark(); 210 ChangeState("Door is OPEN"); 211 break; 212 213 214 case CLOSING_DOOR: 215 ClosingDoor(); 216 CheckCloseSwitch(); 217 218 CheckEncoderCloseValue(); 219 ChangeState("Closing door"); 220 break; 221 222 223 case DOOR_IS_CLOSED: 224 StopDoorMotor(); 225 CheckUpButton(); 226 227 CheckGettingLight(); 228 ChangeState("Door is CLOSED"); 229 break; 230 231 232 case OPENING_DOOR: 233 OpeningDoor(); 234 CheckOpenSwitch(); 235 236 CheckEncoderOpenValue(); 237 ChangeState("Opening door"); 238 break; 239 240 241 case DOOR_ERROR: 242 ChangeState("Door Error"); 243 break; 244 245 } 246 247 248} 249 250//========================================================== 251 // 252//============== Finite State - Functions ================= // 253//========================================================== 254 // 255 256// --------------- Initializing Function --------------- 257void Initializing() 258 //Calibrate Encoder values for Open & Closed position 259 260{ 261 switch(step) 262 263 { 264 case 0: 265 Blynk.virtualWrite(V6, "Give Open Command"); 266 267 if(buttonOpenValue == HIGH) 268 { 269 OpeningDoor(); 270 step 271 = 1; 272 } 273 else if(switchOpenValue == HIGH) 274 { 275 StopDoorMotor(); 276 277 encoderPos = 0; 278 step = 2; 279 } 280 break; 281 282 283 case 1: 284 Blynk.virtualWrite(V6, "Opening - Stop when Open"); 285 if(switchOpenValue 286 == HIGH) 287 { 288 StopDoorMotor(); 289 encoderPos = 0; 290 step 291 = 2; 292 } 293 break; 294 295 case 2: 296 Blynk.virtualWrite(V6, 297 "Give Close Command"); 298 if(buttonCloseValue == HIGH) 299 { 300 ClosingDoor(); 301 302 step = 3; 303 } 304 break; 305 306 case 3: 307 Blynk.virtualWrite(V6, 308 "Closing - Stop when Closed"); 309 if(switchCloseValue == HIGH) 310 { 311 312 StopDoorMotor(); 313 encoderClosePos = encoderPos; 314 state 315 = DOOR_IS_CLOSED; 316 } 317 break; 318 } 319 320} 321 322// 323 --------------- MOTOR Functions --------------- 324void StopDoorMotor() 325{ 326 327 digitalWrite(O_MotorDriverOut1, LOW); 328 digitalWrite(O_MotorDriverOut2, LOW); 329} 330 331void 332 ClosingDoor() 333{ 334 digitalWrite(O_MotorDriverOut1, HIGH); 335 digitalWrite(O_MotorDriverOut2, 336 LOW); 337} 338 339void OpeningDoor() 340{ 341 digitalWrite(O_MotorDriverOut1, LOW); 342 343 digitalWrite(O_MotorDriverOut2, HIGH); 344} 345 346// --------------- Input Buttons 347 --------------- 348void CheckDownButton() 349{ 350 if(buttonCloseValue == HIGH) 351 // Virtual value from Blynk 352 { 353 state = CLOSING_DOOR; 354 } 355} 356 357void 358 CheckUpButton() 359{ 360 if(buttonOpenValue == HIGH) // Virtual value from Blynk 361 362 { 363 state = OPENING_DOOR; 364 } 365} 366 367// --------------- Input Switches 368 --------------- 369void CheckCloseSwitch() 370{ 371 if(switchCloseValue == HIGH) 372 // Virtual value from Blynk 373 { 374 state = DOOR_IS_CLOSED; 375 } 376} 377 378void 379 CheckOpenSwitch() 380{ 381 if(switchOpenValue == HIGH) // Virtual value from Blynk 382 383 { 384 state = DOOR_IS_OPEN; 385 } 386} 387 388void CheckEncoderCloseValue() 389{ 390 391 if(encoderPos >= encoderClosePos) 392 { 393 StopDoorMotor(); 394 state 395 = DOOR_IS_CLOSED; 396 } 397} 398 399void CheckEncoderOpenValue() 400{ 401 if(encoderPos 402 <= 0) 403 { 404 StopDoorMotor(); 405 state = DOOR_IS_OPEN; 406 } 407} 408 409// 410 --------------- Light Sensor --------------- 411void CheckGettingDark() 412{ 413 414 if(operatingModeValue && LightValue < ThresHoldGettingDark) 415 { 416 state 417 = CLOSING_DOOR; 418 } 419} 420 421void CheckGettingLight() 422{ 423 if(operatingModeValue 424 && LightValue > ThresHoldGettingLight) 425 { 426 state = OPENING_DOOR; 427 } 428} 429 430// 431 --------------- Status String --------------- 432void ChangeState(String Status) 433{ 434 435 if(OldState != Status) 436 { 437 Blynk.virtualWrite(V6, Status); 438 OldState 439 = Status; 440 } 441} 442 443//========================================================== 444 // 445//=========== Interrupts - Detect fast changes ============== // 446//========================================================== 447 // 448void doEncoder() 449{ 450 if(digitalRead(encoderPinA) == digitalRead(encoderPinB)) 451 452 { 453 encoderPos++; 454 } 455 else 456 { 457 encoderPos--; 458 } 459} 460
Chicken Coop Code - V1
arduino
- Including Blynk reconnect logic - Reduced data exchange interval
1 2/* Code developed by BMic on 21 May 2018 3 * Used to connect with Blynk IOT service and to control a chicken coop: 4 * - Door control based on encoder value 5 * - Environmental monitoring: Temperature, Humidity and light 6 * 7 * Change V1: 8 * - Decrease amount of values sent to Blynk only upon Change or every minute for historical values 9 * - Split data exchanges in Pull and Push mode 10 * - Include input button for Open/Close commands and Reed position switches 11 * - Reconnection logic to Blynk in case it's down 12 * 13*/ 14#define BLYNK_PRINT Serial 15//#define BLYNK_DEBUG // Optional, this enables more detailed prints 16 17#include "DHT.h" 18#include <SPI.h> 19#include <WiFi101.h> 20#include <BlynkSimpleMKR1000.h> 21 22//========================================================== // 23//============== IO & Variables definition ================= // 24//========================================================== // 25 26//--- Digital inputs ---// 27#define encoderPinA 0 // Encoder input Channel A 28#define encoderPinB 1 // Encoder input Channel A 29#define I_CloseDoorSwitch 2 // Reed switch for Closed position 30#define I_OpenDoorSwitch 3 // Reed switch for Open position 31#define I_CloseDoorButton 4 // Button to send Close command 32#define I_OpenDoorButton 5 // Button to send Open command 33#define O_MotorDriverOut1 7 // Motor Output to Close the Door 34#define O_MotorDriverOut2 8 // Motor Output to Open the Door 35 36//--- Analog inputs ---// 37#define I_LightSensor A0 // Analog input to measure Photo resistor 0 - 1023 38 39//--- DHT Sensor input ---// 40#define DHTPIN 9 41#define DHTTYPE DHT22 42DHT dht(DHTPIN, DHTTYPE); 43 44float hum; //Stores humidity value 45float temp; //Stores temperature value 46 47//--- Light sensor ---// 48int LightValue = 0; // Actual analog input measurement 49int deltaLight = 0; // Difference between Light threshold and actual value 50int deltaLightOld = 0; // Previous value to trigger a value change 51int deltaDark = 0; // Difference between Dark threshold and actual value 52int deltaDarkOld = 0; // Previous value to trigger a value change 53int ThresHoldGettingDark = 25; // Treshold value to indicate it's getting Dark and door should close 54int ThresHoldGettingLight = 650; // Treshold value to indicate it's getting Light and door should open 55 56//--- Encoder input ---// 57volatile int encoderPos = 0; // Volatile int should be used for interrupt values 58int encoderClosePos = 0; // Variable to store the encoder value for Closed position 59int encoderPosOld = 0; // Previous value to trigger a value change 60 61//--- Blynk command variables ---/ 62int buttonOpenValue; // Virtual Button to send Open command from Blynk 63int buttonCloseValue; // Virtual Button to send Close command from Blynk 64int switchOpenValue; // Virtual Button to send Open position from Blynk in case no reed switch is used 65int switchCloseValue; // Virtual Button to send Close position from Blynk in case no reed switch is used 66int operatingModeValue; // Set mode to work in automatic mode using Photo resistor or manual operation 67 68//--- Used for sequential programming steps during initialize calibration procedure --- 69int step = 0; // Squencer in void Initializing() 70 71//--- The different states of the system --- 72enum states {INITIALIZE, DOOR_ERROR, DOOR_IS_OPEN, CLOSING_DOOR, DOOR_IS_CLOSED, OPENING_DOOR}; 73String OldState; 74 75//--- Holds the initial state of the system --- 76states state; 77 78//========================================================== // 79//=========== Blynk connection with Read/Write ============== // 80//========================================================== // 81// You should get Auth Token in the Blynk App. 82// Go to the Project Settings (nut icon). 83char auth[] = "xxxxxxxxxxxxxxxxx"; 84 85// Your WiFi credentials. 86// Set password to "" for open networks. 87int StatusWifi = WL_IDLE_STATUS; // the Wifi radio's status 88char ssid[] = "WiFi-SSID"; 89char pass[] = "Password"; 90 91int DisconnectCount = 0; // Count number of disconnects 92int ReCnctFlag; // Reconnection Flag 93int ReCnctCount = 0; // Reconnection counter 94 95//========================================================== // 96//============== Initial setup at Startup ================= // 97//========================================================== // 98 99BlynkTimer timer; // Start Timer based on SimpleTimer supporting to schedule 16 timers 100 101void EncoderTimerEvent() // Fast timer of 1 second to Push values to Blynk 102{ 103 //--- Encoder ---// 104 if(encoderPosOld != encoderPos){ // Check if encoder value has changed. If yes, send to Blynk. 105 Blynk.virtualWrite(V2, encoderPos); 106 encoderPosOld = encoderPos; 107 Serial.print("Previous encoder position was: "); 108 Serial.println(encoderPosOld); 109 Serial.print("Actual encoder position is: "); 110 Serial.println(encoderPos); 111 } 112} 113 114void BlynkTimerEvent() // Slow timer of 1 minute to Push values to Blynk 115{ 116 //--- DHT22 ---// 117 hum = dht.readHumidity(); 118 temp= dht.readTemperature(); 119 Blynk.virtualWrite(V3, temp); 120 Blynk.virtualWrite(V4, hum); 121 122 //--- LUX ---// 123 LightValue = analogRead(I_LightSensor); 124 Blynk.virtualWrite(V1, LightValue); 125 126 //--- Encoder ---// 127 Blynk.virtualWrite(V2, encoderPos); // Only upon change (EncoderTimerEvent) messes up the graph in Blynk 128 129 //--- Reconnect counter ---// 130 Blynk.virtualWrite(V0, ReCnctCount); 131} 132 133// Pull requests from Blynk --> Only Pull when app is open since we don't need those as historical values!! 134// Light Treshold -> Frequency is set in Blynk app and will only work when app is active in foreground!! 135 136 BLYNK_READ(V11){ // When Blynk requests a new value, calculate value and write back 137 deltaLight = ThresHoldGettingLight - LightValue; 138 Blynk.virtualWrite(V11, deltaLight); 139 } 140 141 BLYNK_READ(V12){ // When Blynk requests a new value, calculate value and write back 142 deltaDark = ThresHoldGettingDark - LightValue; 143 Blynk.virtualWrite(V12, deltaDark); 144 } 145 146//--- Command's from Blynk to MKR1000 ---// 147//--- These functions will be called every time a blynk Widget value is changed ---// 148 149 BLYNK_WRITE(V21) 150{ 151 ThresHoldGettingDark = param.asInt(); 152} 153 154 BLYNK_WRITE(V22) 155{ 156 ThresHoldGettingLight = param.asInt(); 157} 158 159 BLYNK_WRITE(V7) 160{ 161 buttonOpenValue = param.asInt(); 162} 163 164 BLYNK_WRITE(V8) 165{ 166 buttonCloseValue = param.asInt(); 167} 168 169 BLYNK_WRITE(V9) 170{ 171 switchOpenValue = param.asInt(); 172} 173 174 BLYNK_WRITE(V10) 175{ 176 switchCloseValue = param.asInt(); 177} 178 179 BLYNK_WRITE(V20) 180{ 181 operatingModeValue = param.asInt(); 182} 183 184//========================================================== // 185//================== Initial Setup LOOP ==================== // 186//========================================================== // 187 188void setup() 189{ 190 //Initialize serial and wait for port to open: 191 Serial.begin(9600); 192 while (!Serial) { 193 ; // wait for serial port to connect. Needed for native USB port only 194 } 195 Serial.println(); 196 197 //Check for the presence of the shield: 198 /**************************************************************************************/ 199 if (WiFi.status() == WL_NO_SHIELD) { 200 Serial.println("WiFi shield not present"); 201 /*Don't continue:*/ 202 while (true); 203 } 204 /**************************************************************************************/ 205 206//--- initialize digital pin LED_BUILTIN as an output --- 207 pinMode(I_CloseDoorSwitch, INPUT_PULLUP); 208 pinMode(I_OpenDoorSwitch, INPUT_PULLUP); 209 pinMode(I_CloseDoorButton, INPUT_PULLUP); 210 pinMode(I_OpenDoorButton, INPUT_PULLUP); 211 pinMode(O_MotorDriverOut1, OUTPUT); 212 pinMode(O_MotorDriverOut2, OUTPUT); 213 pinMode(encoderPinA, INPUT_PULLUP); 214 pinMode(encoderPinB, INPUT_PULLUP); 215 216 state = INITIALIZE; 217 218 attachInterrupt(0, doEncoder, CHANGE); // Use Interrupt to count reliable every encoder input 219 220 Blynk.begin(auth, ssid, pass); 221 dht.begin(); 222 223 timer.setInterval(1000L, EncoderTimerEvent); // One second update timer for Blynk 224 timer.setInterval(60000L, BlynkTimerEvent); // One minute update timer for Blynk 225 226} 227 228 BLYNK_CONNECTED() { 229 Serial.println("Connected"); 230 ReCnctCount = 0; 231} 232 233 234//========================================================== // 235//========== Finite State Machine - Main LOOP ============== // 236//========================================================== // 237void loop() 238{ 239 240 timer.run(); // Starts Blynk Timer 241 242// Reconnect routine tried from: https://community.blynk.cc/t/mega-esp-will-freeze-if-there-is-a-connection-problem/25124/2 243// Not sure if this is actually needed or even working 244if (Blynk.connected()) { 245 Blynk.run(); // Starts Blynk connection 246} 247else if (ReCnctFlag == 0) { 248 ReCnctFlag = 1; // Set reconnection Flag 249 Serial.println("Starting reconnection timer in 30 seconds..."); 250 timer.setTimeout(30000L, []() { // Lambda Reconnection Timer Function 251 ReCnctFlag = 0; // Reset reconnection Flag 252 ReCnctCount++; // Increment reconnection Counter 253 Serial.print("Attempting reconnection #"); 254 Serial.println(ReCnctCount); 255 Blynk.connect(); // Try to reconnect to the server 256 }); 257} 258 259// --- Sequencer logic to track the State of the door and specific monitor events ---// 260 switch (state) 261 { 262 case INITIALIZE: 263 Initializing(); 264 break; 265 266 case DOOR_IS_OPEN: 267 StopDoorMotor(); 268 CheckDownButton(); 269 CheckGettingDark(); 270 ChangeState("Door is OPEN"); 271 break; 272 273 case CLOSING_DOOR: 274 ClosingDoor(); 275 CheckCloseSwitch(); 276 CheckEncoderCloseValue(); 277 ChangeState("Closing door"); 278 break; 279 280 case DOOR_IS_CLOSED: 281 StopDoorMotor(); 282 CheckUpButton(); 283 CheckGettingLight(); 284 ChangeState("Door is CLOSED"); 285 break; 286 287 case OPENING_DOOR: 288 OpeningDoor(); 289 CheckOpenSwitch(); 290 CheckEncoderOpenValue(); 291 ChangeState("Opening door"); 292 break; 293 294 case DOOR_ERROR: 295 ChangeState("Door Error"); 296 break; 297 } 298} 299 300//--- Not sure if the previous routine within the Main LOOP is needed or not, so I use both for now ---// 301BLYNK_DISCONNECTED() 302{ 303 Serial.println("Blynk is disconnected!"); 304 305 if(Blynk.connected()!=true) 306 { 307 DisconnectCount++; 308 Serial.println("Blynk not conected counter = "); 309 Serial.println(DisconnectCount); 310 Blynk.connect(); 311 } 312} 313 314//========================================================== // 315//============== Finite State - Functions ================= // 316//========================================================== // 317 318// --------------- Initializing Function --------------- 319void Initializing() 320 321/* Still to Do: 322 * 1. Record Time to Close 323 * 2. Save the time in a variable and use it during Opening/Closing 324 * 3. If time > preset value and encoder value is not reached -> ERROR 325 * 4. When error, retry to open/close 2..3..?? times and then return critical error 326*/ 327/* Initializing Function: 328 * - After rebooting or resetting Arduino, first step will be to calibrate open/close position based on encoder value 329 * case 0: Sequencer will wait for your Open command to open the door or skip the step if the door is already in the Open position 330 * case 1: Door will open until Open switch is triggered or Virtual Open position is confirmed and EncoderPos value is set to 0 331 * case 2: Sequencer will wait for your Close command to close the door 332 * case 3: Door will close until Close switch is triggered or Virtual Open position is confirmed and encoderClosePos value is set 333 */ 334 335{ 336 switch(step) 337 { 338 case 0: 339 ChangeState("Give Open Command"); 340 if(buttonOpenValue == HIGH or digitalRead(I_OpenDoorButton) == LOW) 341 { 342 OpeningDoor(); 343 step = 1; 344 } 345 else if(switchOpenValue == HIGH or digitalRead(I_OpenDoorSwitch) == LOW) 346 { 347 StopDoorMotor(); 348 encoderPos = 0; 349 step = 2; 350 } 351 break; 352 353 case 1: 354 ChangeState("Opening - Stop when Open"); 355 if(switchOpenValue == HIGH or digitalRead(I_OpenDoorSwitch) == LOW) 356 { 357 StopDoorMotor(); 358 encoderPos = 0; 359 step = 2; 360 } 361 break; 362 363 case 2: 364 ChangeState("Give Close Command"); 365 if(buttonCloseValue == HIGH or digitalRead(I_CloseDoorButton) == LOW) 366 { 367 ClosingDoor(); 368 step = 3; 369 } 370 break; 371 372 case 3: 373 ChangeState("Closing - Stop when Closed"); 374 if(switchCloseValue == HIGH or digitalRead(I_CloseDoorSwitch) == LOW) 375 { 376 StopDoorMotor(); 377 encoderClosePos = encoderPos; 378 state = DOOR_IS_CLOSED; 379 } 380 break; 381 } 382 383} 384 385// --------------- MOTOR Functions --------------- // 386void StopDoorMotor() 387{ 388 digitalWrite(O_MotorDriverOut1, LOW); 389 digitalWrite(O_MotorDriverOut2, LOW); 390} 391 392void ClosingDoor() 393{ 394 digitalWrite(O_MotorDriverOut1, HIGH); 395 digitalWrite(O_MotorDriverOut2, LOW); 396} 397 398void OpeningDoor() 399{ 400 digitalWrite(O_MotorDriverOut1, LOW); 401 digitalWrite(O_MotorDriverOut2, HIGH); 402} 403 404// --------------- Open/Close command Buttons --------------- // 405void CheckDownButton() 406{ 407 if(buttonCloseValue == HIGH or digitalRead(I_CloseDoorButton) == LOW) // Virtual value from Blynk or input button 408 { 409 state = CLOSING_DOOR; 410 } 411} 412 413void CheckUpButton() 414{ 415 if(buttonOpenValue == HIGH or digitalRead(I_OpenDoorButton) == LOW) // Virtual value from Blynk or input button 416 { 417 state = OPENING_DOOR; 418 } 419} 420 421// --------------- Open/Close Input Switches --------------- // 422void CheckCloseSwitch() 423{ 424 if(switchCloseValue == HIGH or digitalRead(I_CloseDoorSwitch) == LOW) // Virtual value from Blynk or input switch 425 { 426 state = DOOR_IS_CLOSED; 427 } 428} 429 430void CheckOpenSwitch() 431{ 432 if(switchOpenValue == HIGH or digitalRead(I_OpenDoorSwitch) == LOW) // Virtual value from Blynk or input switch 433 { 434 state = DOOR_IS_OPEN; 435 } 436} 437 438// --- Stop Closing Door if encoder value >= Closed value registered during Initializing loop --- // 439void CheckEncoderCloseValue() 440{ 441 if(encoderPos >= encoderClosePos) 442 { 443 StopDoorMotor(); 444 state = DOOR_IS_CLOSED; 445 } 446} 447 448// --- Stop Opening Door if encoder value <= 0 which is calibrated during Initializing loop --- // 449void CheckEncoderOpenValue() 450{ 451 if(encoderPos <= 0) 452 { 453 StopDoorMotor(); 454 state = DOOR_IS_OPEN; 455 } 456} 457 458// --------------- Light Sensor --------------- // 459void CheckGettingDark() 460{ 461 if(operatingModeValue && LightValue < ThresHoldGettingDark) 462 { 463 state = CLOSING_DOOR; 464 } 465} 466 467void CheckGettingLight() 468{ 469 if(operatingModeValue && LightValue > ThresHoldGettingLight) 470 { 471 state = OPENING_DOOR; 472 } 473} 474 475// --------------- Status String --------------- // 476void ChangeState(String Status) 477{ 478 if(OldState != Status) 479 { 480 Blynk.virtualWrite(V6, Status); // Only push to Blynk if value is changed 481 OldState = Status; 482 } 483} 484 485//========================================================== // 486//=========== Interrupts - Detect fast changes ============== // 487//========================================================== // 488void doEncoder() 489{ 490 if(digitalRead(encoderPinA) == digitalRead(encoderPinB)) 491 { 492 encoderPos++; 493 } 494 else 495 { 496 encoderPos--; 497 } 498}
Downloadable files
untitled
untitled
untitled
untitled
kippendeur_Ndx0y55bgl.fzz
kippendeur_Ndx0y55bgl.fzz
Comments
Only logged in users can leave comments
BMic
0 Followers
•0 Projects
Table of contents
Intro
26
0