Components and supplies
Arduino Oplà IoT Kit
Li-Ion Battery 1000mAh
Stepper motor driver board A4988
Jumper wires (generic)
Solderless Breadboard Full Size
Limit Switch, 5 A
DS18B20 Programmable Resolution 1-Wire Digital Thermometer
Project description
Code
Smart Faucet
arduino
1#include "thingProperties.h" 2#include<AccelStepper.h> 3#include <DS18B20.h> 4#include <Arduino_MKRIoTCarrier.h> 5#include <ezButton.h> 6 7float tempCold = 25; 8float prevTempDesired; 9float tempHot = 48; 10 11//~~~~~~~~~~Status Messages~~~~~~~~~~// 12String stringOne = "Desired: "; 13String stringTwo = " Curr: "; 14String stringThree = "Desired Temperaure is out of Scope."; 15String stringFour = "Desired Temp Reached."; 16String stringFive = "No Motion Detected."; 17String stringSix = "°"; 18String stringSeven = " Sensor(s) Missing"; 19String stringEight = " Current Temp: "; 20//~~~~~~<END>Status Messages~~~~~~// 21 22//~~~~Define MKR Carrier Sensors and Hardware~~~~// 23#define PIR A5 24MKRIoTCarrier carrier; 25extern bool CARRIER_CASE; 26int Val = 0; 27//~~<END>Setup MKR Carrier Sensors and Hardware~~// 28 29//~~~~~~~~Create the Motor Instances~~~~~~~~// 30long positions[2]; 31long zeroPos[2]; 32long mid[2] = {0,80}; 33AccelStepper stepperCold(1,1,0); 34AccelStepper stepperHot(1,4,3); 35float Vd; 36bool dirCold = 0; 37bool dirHot = 0; 38float positionCold, positionHot; 39//~~~~~<END>Create the Motor Instances~~~~~// 40 41//~~Create Instances for Temperature Sensors~~// 42DS18B20 Temps(21); 43uint8_t Sensor2Add[8] = {40, 59, 22, 70, 146, 9, 2, 107}; 44uint8_t Sensor3Add[8] = {40, 47, 17, 210, 11, 0, 0, 250}; 45float Uqr = 0; 46float pqr = 0; 47//~~<END>create instances for temperature sensors~~// 48 49//~~~~~~~~~~Define Limit Switches~~~~~~~~~~// 50ezButton ls_hot(7); 51ezButton ls_cold(8); 52int stateHot, stateCold; 53bool fin = false; 54float Th_1 = 0; 55//~~~~~~~<END>Define Limit Switches~~~~~~~~// 56 57//~~~~~~~~~~~~~~~~Display features~~~~~~~~~~~~~~~// 58int ht = 0; 59//~~~~~~~~~~~~~<END> Display Features~~~~~~~~~~~~// 60 61//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// 62//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~SETUP~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// 63//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~// 64void setup() { 65//~~~~~~~Initialize serial~~~~~~// 66 Serial.begin(9600); 67 delay(6000); 68//~~~~<END>Initialize serial~~~~// 69 70// Setup and Initialize Arduino Cloud 71// Defined in thingProperties.h 72 initProperties(); 73 74// Connect to Arduino IoT Cloud 75 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 76 77 /* 78 The following function allows you to obtain more information 79 related to the state of network and IoT Cloud connection and errors 80 the higher number the more granular information you’ll get. 81 The default is 0 (only errors). 82 Maximum is 4 83 */ 84 ArduinoCloud.addCallback(ArduinoIoTCloudEvent::CONNECT, doThisOnConnect); 85 setDebugMessageLevel(2); 86 ArduinoCloud.printDebugInfo(); 87 tempDesired = 36; 88// <END>Setup and Initialize Arduino Cloud 89 90//~~~~~Setup MKR Carrier and Sensors~~~~// 91 CARRIER_CASE = true; 92 carrier.begin(); 93 pinMode (PIR, INPUT); 94//~~<END>Setup MKR Carrier and Sensors~~// 95 96//~~~~~~~~Initialize Motor Settings~~~~~~~~// 97 stepperCold.setAcceleration(80); 98 stepperCold.setMaxSpeed(1000); 99 stepperHot.setAcceleration(80); 100 stepperHot.setMaxSpeed(1000); 101//~~~~~<END>Initialize Motor Settings~~~~~~// 102 103//~~~~~~~~~~Limit Switch Settings~~~~~~~~~~// 104 ls_hot.setDebounceTime(50); 105 ls_cold.setDebounceTime(50); 106//~~~~~~~<END>Limit Switch Settings~~~~~~~~// 107 108//~~~~~~~~Setup Temperature Sensors~~~~~~~~// 109 float Th_1 = 0; 110 int p = 1; 111 int n = Temps.getNumberOfDevices(); 112 Serial.println(n); 113 if(n < 2){//Verify if all snesors are pressent 114 int see = 2 - n; 115 String errr = see+stringSeven; 116 Serial.println(errr); 117 } 118//~~~~~~~<END>Setup Temperature Sensors~~~~~~~// 119} 120//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~LOOP~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 121int check = 1; 122int imp = 0; 123int prevTemp = 0; 124void loop() { 125 ArduinoCloud.update(); 126// carrier.display.setRotation(0); 127//~~~~~~~~~Check Touch Buttons~~~~~~~~~~// 128 TouchButtonsCheck(); 129//~~~~~~~~~<END>Check Touch Buttons~~~~~// 130 131//~~~~~~~~~~~~~~Run Flow Checks~~~~~~~~~~~~~// 132 Val = digitalRead(PIR); 133//~~~~~~~~~~~~No Motion Detected?~~~~~~~~~~~// 134 while(Val == LOW){ 135 ArduinoCloud.update(); 136 for(int r = 0; r < 5; r++){ 137 carrier.leds.setPixelColor(r, 0, 0, 0); 138 carrier.leds.show(); 139 } 140 Status = stringFive; 141 142//~~~~~~~~~~~Check Touch Buttons~~~~~~~~~~~~~// 143 TouchButtonsCheck(); 144//~~~~~~~~~<END>Check Touch Buttons~~~~~~~~~~// 145 motorHot = stepperHot.currentPosition(); 146 motorCold = stepperCold.currentPosition(); 147 Val = digitalRead(PIR); 148 } 149 150//~~~~~~~~~~~~~~~Motion Detected~~~~~~~~~~~~~// 151 152//~~~~~~~~~~~~Check Touch Buttons~~~~~~~~~~~// 153 TouchButtonsCheck(); 154//~~~~~~~~~<END>Check Touch Buttons~~~~~~~~~// 155 156//~~~~~~~~~~<END>Run Flow Checks~~~~~~~~~~~~// 157 158//~~~~~~~~~~Wash Em' Hands Folks~~~~~~~~~~~~// 159 float flT = millis(); 160 while((millis() - flT) <= 10000){ 161 tempOut = getT(2); 162 Status = stringEight + tempOut+stringSix; 163 ArduinoCloud.update(); 164 for(int r = 0; r < 5; r++){ 165 if (r % 2 == 0){carrier.leds.setPixelColor(r, 50, 0, 0);} 166 else{carrier.leds.setPixelColor(r, 0, 50, 0);} 167 carrier.leds.show(); 168 } 169//~~~~~~~~~~~Check Touch Buttons~~~~~~~~~~~~// 170 TouchButtonsCheck(); 171//~~~~~~~~<END>Check Touch Buttons~~~~~~~~~~// 172 173//~~~~~~~~~~~~Run Motor Control~~~~~~~~~~~~~// 174 if(tempDesired != prevTempDesired){ 175 TempDesiredChange(); 176 motors(positions); 177 prevTempDesired = tempDesired; 178 } 179 else{ 180 motors(positions); 181 } 182 motorHot = stepperHot.currentPosition(); 183 motorCold = stepperCold.currentPosition(); 184 } 185//~~~~~~~~<END> Run Motor Control~~~~~~~~~~~// 186 motorsZero(); 187} 188 189float Kalman(float x){ 190 float rq = 0.00001; float qq = 0.000005; float hq = 1; 191 float kq = pqr*hq/(hq*pqr*hq+rq); 192 Uqr = Uqr+kq*(x-hq*Uqr); 193 pqr = (hq-kq*hq)*pqr+qq; 194 return Uqr; 195} 196 197float getT(int num){ 198 float tTheo; 199 switch(num){ 200 case 1: 201// Temps.select(Sensor1Add); 202// return Temps.getTempC(); 203 break; 204 case 2: 205 Temps.select(Sensor2Add); 206 if(stepperCold.currentPosition() != 0 || stepperHot.currentPosition() != 0){ 207 if(stepperCold.currentPosition() != 0 && stepperHot.currentPosition() == 0){ 208 tTheo = tempCold; 209 } 210 if(stepperCold.currentPosition() == 0 && stepperCold.currentPosition() != 0){ 211 tTheo = tempHot; 212 } 213 if(stepperCold.currentPosition() != 0 && stepperHot.currentPosition() != 0){ 214 tTheo = tempCold + (tempHot-tempCold)*(stepperHot.currentPosition()/198)/(stepperCold.currentPosition()/204+positions[1]/198); 215 } 216 return 0.7 * tTheo + 0.3 * Kalman(Temps.getTempC()); 217 break; 218 } 219 case 3: 220 Temps.select(Sensor3Add); 221 return Temps.getTempC(); 222 break; 223 } 224} 225void TempDesiredChange(){ 226 Vd = tempDesired / 100; 227 positions[1] = (long)(Vd * 198); 228 if (Vd <= 0.25){ 229 positions[0] = 200; 230 } 231 if (Vd <= 0.5 && Vd > 0.25){ 232 positions[0] = 140; 233 } 234 if (Vd <= 0.75 && Vd > 0.5){ 235 positions[0] = 90; 236 } 237 if (Vd <= 1 && Vd > 0.75){ 238 positions[0] = 30; 239 } 240 241} 242void TouchButtonsCheck(){ 243 carrier.Buttons.update(); 244 if (carrier.Buttons.onTouchDown(TOUCH0)) { 245 tempDesired = 0; 246 carrier.leds.setPixelColor(0, 0, 50, 0); 247 carrier.leds.show(); 248 } 249 if(carrier.Buttons.onTouchUp(TOUCH0)){ 250 carrier.leds.setPixelColor(0, 0); 251 carrier.leds.show(); 252 } 253 if (carrier.Buttons.onTouchDown(TOUCH1)) { 254 tempDesired = 50; 255 carrier.leds.setPixelColor(1, 0, 50, 0); 256 carrier.leds.show(); 257 } 258 if(carrier.Buttons.onTouchUp(TOUCH1)){ 259 carrier.leds.setPixelColor(1, 0); 260 carrier.leds.show(); 261 } 262 if (carrier.Buttons.onTouchDown(TOUCH2)) { 263 tempDesired = 76; 264 carrier.leds.setPixelColor(2, 0, 50, 0); 265 carrier.leds.show(); 266 } 267 if(carrier.Buttons.onTouchUp(TOUCH2)){ 268 carrier.leds.setPixelColor(2, 0); 269 carrier.leds.show(); 270 } 271 if (carrier.Buttons.onTouchDown(TOUCH3)) { 272 if(tempDesired < 102){ 273 tempDesired += 2; 274 } 275 carrier.leds.setPixelColor(3, 50, 0, 0); 276 carrier.leds.show(); 277 } 278 if (carrier.Buttons.onTouchUp(TOUCH3)) { 279 carrier.leds.setPixelColor(3, 0); 280 carrier.leds.show(); 281 } 282 if (carrier.Buttons.onTouchDown(TOUCH4)) { 283 if(tempDesired > -2){ 284 tempDesired -= 2; 285 } 286 carrier.leds.setPixelColor(4, 0, 0, 50); 287 carrier.leds.show(); 288 } 289 if (carrier.Buttons.onTouchUp(TOUCH4)) { 290 carrier.leds.setPixelColor(4, 0); 291 carrier.leds.show(); 292 } 293} 294void motors(long psn[2]){ 295 int j = 1; int k = 1; 296 stepperCold.moveTo(psn[0]); 297 stepperHot.moveTo(psn[1]); 298 while(j == 1 || k == 1){ 299 ls_cold.loop(); 300 ls_hot.loop(); 301 stateCold = ls_cold.getState(); 302 stateHot = ls_hot.getState(); 303 if(j == 1){ 304 stepperCold.run(); 305 } 306 if(k == 1){ 307 stepperHot.run(); 308 } 309 if(ls_cold.isPressed() || stepperCold.distanceToGo() == 0){ 310 if(j == 1){ 311 stepperCold.stop(); 312 j++; 313 } 314 } 315 if(ls_hot.isPressed() || stepperHot.distanceToGo() == 0){ 316 if(k == 1){ 317 stepperHot.stop(); 318 k++; 319 } 320 } 321 } 322 positionHot = stepperHot.currentPosition(); 323 positionCold = stepperCold.currentPosition(); 324} 325void motorsZero(){ 326 int j = 1; int k = 1; 327 while(j == 1 || k == 1){ 328 ls_cold.loop(); 329 ls_hot.loop(); 330 stateCold = ls_cold.getState(); 331 stateHot = ls_hot.getState(); 332 if(j == 1){ 333 stepperCold.moveTo(stepperCold.currentPosition() - 20); 334 stepperCold.run(); 335 } 336 if(k == 1){ 337 stepperHot.moveTo(stepperHot.currentPosition() - 20); 338 stepperHot.run(); 339 } 340 if(ls_cold.isPressed()){ 341 if(j == 1){ 342 stepperCold.stop(); 343 Serial.println("1 pressed"); 344 stepperCold.setCurrentPosition(0); 345 j++; 346 } 347 } 348 if(ls_hot.isPressed()){ 349 if(k == 1){ 350 stepperHot.stop(); 351 Serial.println("2 pressed"); 352 stepperHot.setCurrentPosition(0); 353 k++; 354 } 355 } 356 } 357 positionHot = stepperHot.currentPosition(); 358 positionCold = stepperCold.currentPosition(); 359}
Downloadable files
Temp sensor DS18B20
Temp sensor DS18B20
Stepper A4988
Stepper A4988
Stepper A4988
Stepper A4988
Temp sensor DS18B20
Temp sensor DS18B20
Comments
Only logged in users can leave comments