CPP 242
Mohamadi Homework Guest on 22nd December 2023 02:06:36 PM
  1. #include <OneWire.h>
  2. #include <DallasTemperature.h>
  3.  
  4. #define LDR A0
  5. #define heater 4
  6. #define fan 5
  7. // if the value falls lower than the definition below, then we assume it's night and vice versa
  8. #define NightOrDay 400
  9. #define HotOrCold 30
  10. #define ONE_WIRE_BUS 2
  11.  
  12. int i;
  13. bool flag = 0;
  14.  
  15. OneWire oneWire(ONE_WIRE_BUS);
  16. DallasTemperature sensors(&oneWire);
  17.  
  18. void setup() {
  19.   Serial.begin(9600);
  20.   pinMode(heater, OUTPUT);
  21.   pinMode(fan, OUTPUT);
  22.   pinMode(7, OUTPUT);
  23.   pinMode(8, OUTPUT);
  24.   pinMode(9, OUTPUT);
  25.   pinMode(10, OUTPUT);
  26.   sensors.begin();
  27. }
  28.  
  29. void loop() {
  30.   // this section is used to pick a random LED that's constantly powered throughout the night
  31.   int LDR_val = analogRead(LDR);
  32.   if(LDR_val <= NightOrDay){
  33.     if(flag == 0){
  34.       int random_led = random(7, 11);
  35.       digitalWrite(random_led, HIGH);
  36.       for(i=7; i<11; i++){
  37.         if(i!=random_led){
  38.           digitalWrite(i, LOW);
  39.         }
  40.       }
  41.       flag = 1;
  42.     }
  43.   }
  44.   else {
  45.     if(flag == 1){
  46.       flag = 0;
  47.     }
  48.     for(i=7;i<11;i++){
  49.       digitalWrite(i, LOW);
  50.     }
  51.   }
  52.   // this section turns the fan or the heater on depending on the temperature
  53.   sensors.requestTemperatures();
  54.   float temp = sensors.getTempCByIndex(0);
  55.   if (temp != DEVICE_DISCONNECTED_C) {
  56.     if(temp <= HotOrCold){
  57.       //if the temp drops below 30 then we assume it's cold and we turn the heater on
  58.       digitalWrite(heater, HIGH);
  59.       digitalWrite(fan, LOW);
  60.     }
  61.     else{
  62.       digitalWrite(fan, HIGH);
  63.       digitalWrite(heater, LOW);
  64.     }
  65.   }
  66.   else {
  67.     Serial.println("Error");
  68.   }
  69.   delay(1000);
  70. }

Hightechrobo bin is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.