- #include <OneWire.h>
- #include <DallasTemperature.h>
- #define LDR A0
- #define heater 4
- #define fan 5
- // if the value falls lower than the definition below, then we assume it's night and vice versa
- #define NightOrDay 400
- #define HotOrCold 30
- #define ONE_WIRE_BUS 2
- int i;
- bool flag = 0;
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- void setup() {
- Serial.begin(9600);
- pinMode(heater, OUTPUT);
- pinMode(fan, OUTPUT);
- pinMode(7, OUTPUT);
- pinMode(8, OUTPUT);
- pinMode(9, OUTPUT);
- pinMode(10, OUTPUT);
- sensors.begin();
- }
- void loop() {
- // this section is used to pick a random LED that's constantly powered throughout the night
- int LDR_val = analogRead(LDR);
- if(LDR_val <= NightOrDay){
- if(flag == 0){
- int random_led = random(7, 11);
- digitalWrite(random_led, HIGH);
- for(i=7; i<11; i++){
- if(i!=random_led){
- digitalWrite(i, LOW);
- }
- }
- flag = 1;
- }
- }
- else {
- if(flag == 1){
- flag = 0;
- }
- for(i=7;i<11;i++){
- digitalWrite(i, LOW);
- }
- }
- // this section turns the fan or the heater on depending on the temperature
- sensors.requestTemperatures();
- float temp = sensors.getTempCByIndex(0);
- if (temp != DEVICE_DISCONNECTED_C) {
- if(temp <= HotOrCold){
- //if the temp drops below 30 then we assume it's cold and we turn the heater on
- digitalWrite(heater, HIGH);
- digitalWrite(fan, LOW);
- }
- else{
- digitalWrite(fan, HIGH);
- digitalWrite(heater, LOW);
- }
- }
- else {
- Serial.println("Error");
- }
- delay(1000);
- }
Recent Pastes