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

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.