Stap 24: Upload de Code
Verwijder de arduino uit de schild of het verwijderen van de rx en tx draden van de bluetooth-module. Kopieer de onderstaande code en plak deze op uw arduino ide. De code uploaden naar je arduino.
/*The Bedroom automation box Made by- Saiaym Agrawal */ #include "DHT.h" // add all the libraries #include <LiquidCrystal.h><br>#include <NewPing.h> String state = "OFF"; // initialize all the values int temppin = A0; int pir = A1; int mic = A5; int ldr = A6; int incoming = 0; int count = 1; int thres1 = 0; int thres2 = 0; int thres3 = 0; int thres4 = 0; int thres5 = 0; int state1 = 0; int state2 = 0; int state3 = 0; int light = 0; int distance = 0; int sound = 0; int minute = 0; int hour = 0; float temp = 1; float humidity = 0; DHT dht(A4, DHT11); // tell the dht pin and the type of sensor LiquidCrystal lcd(8, 7, 6, 5, 4, 3); // tell the lc pins NewPing sonar(A2, A3, 400); // tell the sensor pins and the maximum distance</p><p>void setup() { pinMode(13, OUTPUT); // set the pins as output pinMode(12, OUTPUT); lcd.begin(16,2); // start up the lcd dht.begin(); Serial.begin(9600); Serial.println("Bedroom Home Automation"); lcd.clear(); lcd.print(" Bedroom Home "); lcd.setCursor(0,1); lcd.print(" Automation "); delay(3000); }</p><p>void loop() { lcd.clear(); lcd.print("Select A Mode"); Serial.println("Select a mode through buttons"); delay(2000); while(digitalRead(11) == LOW) // exit the loop when second button is pressed { if(digitalRead(10) == HIGH) { delay(500); count++; // count the number of times the button is pressed } lcd.clear(); lcd.print("Choice:"); lcd.print(count); delay(100); }</p><p>switch(count) // Enter the mode according to the number selected earlier {</p><p> case 1: // Toggle mode { delay(1000); Serial.println("Toggle mode selected"); Serial.println("Press the button to switch the outlet on/off"); int a8 = 0; while(a8 == 0) { lcd.clear(); lcd.print("Curr. State: "); lcd.print(state); lcd.setCursor(0,1); lcd.print("Press Button"); if(digitalRead(10) == HIGH && state3 == 0) // if button pressed and outlet is off, turn it on { digitalWrite(13, HIGH); state = "ON"; state3++; Serial.println("Switched on"); delay(1000); } if(digitalRead(10) == HIGH && state3 == 1) // if button pressed and outlet is on, turn it off { digitalWrite(13, LOW); state = "OFF"; state3--; Serial.println("Switched off"); delay(1000); } delay(100); } } case 2: // temperature sensor mode { delay(1000); Serial.println("Temperature mode selected"); Serial.println("Set Threshold"); lcd.clear(); lcd.print("Set Threshold"); delay(2000); while(digitalRead(11) == LOW) // set threshold through buttons { if(digitalRead(10) == HIGH) { delay(500); thres1++; } lcd.clear(); lcd.print("Threshold:"); lcd.print(thres1); delay(100); } Serial.print("Threshold Temperature: "); Serial.print(thres1); int a = 0; while(a == 0) { temp = analogRead(temppin); temp = temp * 0.48828125; lcd.clear(); lcd.print("Curr. State: "); lcd.print(state); lcd.setCursor(0,1); lcd.print("Temp:"); lcd.print(temp); lcd.print("*C"); if(temp >= thres1) { digitalWrite(13, HIGH); state = "ON"; } if(temp < thres1) { digitalWrite(13, LOW); state = "OFF"; } delay(1000); } } break; case 3: // Humidity sensor mode delay(1000); Serial.println("Humidity sensor mode selected"); Serial.println("Set Threshold"); lcd.clear(); lcd.print("Set Threshold"); { while(digitalRead(11) == LOW) { if(digitalRead(10) == HIGH) { delay(500); thres2++; } lcd.clear(); lcd.print("Threshold:"); lcd.print(thres2); delay(100); } Serial.print("Threshold Humidity %: "); Serial.print(thres2); Serial.print("%"); int a1 = 0; while(a1 == 0) { humidity = dht.readHumidity(); lcd.clear(); lcd.print("Curr. State: "); lcd.print(state); lcd.setCursor(0,1); lcd.print("Humid:"); lcd.print(humidity); lcd.print("%"); if(humidity <= thres2) { digitalWrite(13, HIGH); state = "ON"; } if(humidity > thres2) { digitalWrite(13, LOW); state = "OFF"; } delay(1000); } } break; case 4: // light sensor mode { delay(1000); Serial.println("Light sensor selected"); Serial.println("Set Threshold"); lcd.clear(); lcd.print("Set Threshold"); while(digitalRead(11) == LOW) { if(digitalRead(10) == HIGH) { delay(500); thres3+=10; } lcd.clear(); lcd.print("Threshold:"); lcd.print(thres3); delay(100); } Serial.print("Threshold intensity: "); Serial.print(thres3); Serial.print("units of 999"); int a2 = 0; while(a2 == 0) { light = analogRead(ldr); light = map(light, 0, 1023, 999, 0); lcd.clear(); lcd.print("Curr. State: "); lcd.print(state); lcd.setCursor(0,1); lcd.print("Light:"); lcd.print(light); lcd.print("un."); if(light <= thres3) { digitalWrite(13, HIGH); state = "ON"; } if(light > thres3) { digitalWrite(13, LOW); state = "OFF"; } delay(1000); } } break;</p><p> case 5: // clap sensor mode { Serial.println("Clap sensor mode selected"); Serial.println("Clap to turn on/off"); int a3 = 0; while(a3 == 0) { lcd.clear(); lcd.print("Curr. State: "); lcd.print(state); lcd.setCursor(0,1); lcd.print("Clap Clap!"); if(analogRead(mic) > 5 && state1 == 0) { digitalWrite(13, HIGH); state = "ON"; state1++; delay(1000); } if(analogRead(mic) > 5 && state1 == 1) { digitalWrite(13, LOW); state = "OFF"; state1--; delay(1000); } delay(100); } } break; case 6: // motion sensor mode { Serial.println("Motion sensor mode selected"); for(int b=30; b>0; b--) { Serial.println("Preparing Sensor....."); lcd.clear(); lcd.print("Preparing"); lcd.setCursor(0,1); lcd.print("Sensor..."); lcd.print(b); delay(1000); } int a4 = 0; while(a4 == 0) { lcd.clear(); lcd.print("Curr. State: "); lcd.print(state); lcd.setCursor(0,1); lcd.print("Motion Sensor"); if(digitalRead(pir) == HIGH && state2 == 0) { digitalWrite(13, HIGH); state = "ON"; state2++; delay(4100); } if(digitalRead(pir) == HIGH && state2 == 1) { digitalWrite(13, LOW); state = "OFF"; state2--; delay(4100); } delay(100); } } break;</p><p>case 7: // presence sensor mode { delay(1000); Serial.println("Presence sensor mode selected"); Serial.println("Set Threshold"); lcd.clear(); lcd.print("Set Threshold"); while(digitalRead(11) == LOW) { if(digitalRead(10) == HIGH) { delay(500); thres4+=10; } lcd.clear(); lcd.print("Threshold:"); lcd.print(thres4); delay(100); } int a5 = 0; while(a5 == 0) { delay(50); distance = sonar.ping(); distance = distance / US_ROUNDTRIP_CM; lcd.clear(); lcd.print("Curr. State: "); lcd.print(state); lcd.setCursor(0,1); lcd.print("Dist."); lcd.print(distance); lcd.print("cm"); if(distance <= thres4) { digitalWrite(13, HIGH); state = "ON"; } if(distance > thres4) { digitalWrite(13, LOW); state = "OFF"; } delay(1000); } } break;</p><p>case 8: // security mode { Serial.println("Security mode selected"); Serial.println("Preparing sensor....."); delay(1000); for(int d = 30; d > 0; d--) { lcd.clear(); lcd.print("Preparing"); lcd.setCursor(0,1); lcd.print("Sensor..."); lcd.print(d); delay(1000); } lcd.clear(); lcd.print("Security Mode"); delay(10000); int a6 = 0; while(a6 == 0) { if(digitalRead(pir) == HIGH) { digitalWrite(13, HIGH); Serial.println("Thief detected"); lcd.clear(); lcd.print("Thief Detected"); lcd.setCursor(0,1); lcd.print("!!!!!!!!!!!!!!!!"); int c = 0; while(c == 0) { digitalWrite(12, HIGH); delay(1000); digitalWrite(12, LOW); delay(1000); } } delay(1000); } } break; </p><p>case 9: // timer mode { delay(1000); Serial.println("Timer Mode selected"); lcd.clear(); lcd.print("Enter Hours"); delay(2000); while(digitalRead(11) == LOW) { if(digitalRead(10) == HIGH) { delay(500); hour++; } lcd.clear(); lcd.print(hour); delay(100); } lcd.clear(); lcd.print("Enter Minutes"); delay(2000); while(digitalRead(11) == LOW) { if(digitalRead(10) == HIGH) { delay(500); minute++; } lcd.clear(); lcd.print(minute); delay(100); } delay(1000); int e2 = minute; int e3 = 0; for(int e1 = hour; e1 >= 0; e1--) { for(e2; e2 >=0; e2--) { for(e3; e3 >= 0; e3--) { digitalWrite(13, HIGH); lcd.clear(); lcd.print("Time Left:"); lcd.setCursor(0,1); lcd.print(e1); lcd.print(":"); lcd.print(e2); lcd.print(":"); lcd.print(e3); delay(1000); } e3 = 59; } e2 = 59; } int a9 = 0; while(a9 == 0) { digitalWrite(13, LOW); lcd.print("Time Finished"); lcd.clear(); delay(100); } } break; case 10: // weather display mode { int a7 = 0; while(a7 == 0) { float t = dht.readTemperature(); float f = dht.readTemperature(true); humidity = dht.readHumidity(); Serial.print("Temperature: "); Serial.print(t); Serial.print("*C"); Serial.print(" "); Serial.print(f); Serial.print("*F"); Serial.println() ; Serial.print("Humidity: "); Serial.print(humidity); Serial.print("%"); Serial.println(); lcd.clear(); lcd.print("T:"); lcd.print(t); lcd.print("C "); lcd.print(f); lcd.print("F"); lcd.setCursor(0,1); lcd.print("H:"); lcd.print(humidity); lcd.print("%"); delay(1000); } } break; } }</p>