Advertisement
pleasedontcode

Temperature Control rev_01

Jan 1st, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: Temperature Control
  13.     - Source Code compiled for: Arduino Uno
  14.     - Source Code created on: 2024-01-01 11:54:09
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Measures temperature     If temperature is greater */
  21.     /* than 27 degrees celsius print "high temp" and turn */
  22.     /* on red LED    if temperature is less than 25 */
  23.     /* degrees celsius print "low temp" and turn on blue */
  24.     /* LED    create this in a function called water_temp */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Arduino.h>
  29. #include <DHT.h>
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34. void water_temp(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t mydht_DHT22_DOUT_PIN_D2 = 2;
  38. const uint8_t red_LED_PIN = 3;
  39. const uint8_t blue_LED_PIN = 4;
  40.  
  41. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  42. DHT dht(mydht_DHT22_DOUT_PIN_D2, DHT22);
  43.  
  44. void setup(void)
  45. {
  46.     // put your setup code here, to run once:
  47.     pinMode(mydht_DHT22_DOUT_PIN_D2, INPUT_PULLUP);
  48.     pinMode(red_LED_PIN, OUTPUT);
  49.     pinMode(blue_LED_PIN, OUTPUT);
  50.     dht.begin();
  51.     Serial.begin(9600);
  52. }
  53.  
  54. void loop(void)
  55. {
  56.     // put your main code here, to run repeatedly:
  57.     water_temp();
  58.     delay(2000);
  59. }
  60.  
  61. void water_temp(void)
  62. {
  63.     float temperature = dht.readTemperature();
  64.  
  65.     // Check if the temperature reading is valid
  66.     if (isnan(temperature)) {
  67.         Serial.println(F("Failed to read from DHT sensor!"));
  68.         return;
  69.     }
  70.  
  71.     Serial.print(F("Temperature: "));
  72.     Serial.print(temperature);
  73.     Serial.println(F("°C"));
  74.  
  75.     // Check if temperature is greater than 27 degrees celsius
  76.     if (temperature > 27) {
  77.         Serial.println(F("High temperature"));
  78.         digitalWrite(red_LED_PIN, HIGH);
  79.         digitalWrite(blue_LED_PIN, LOW);
  80.     }
  81.     // Check if temperature is less than 25 degrees celsius
  82.     else if (temperature < 25) {
  83.         Serial.println(F("Low temperature"));
  84.         digitalWrite(red_LED_PIN, LOW);
  85.         digitalWrite(blue_LED_PIN, HIGH);
  86.     }
  87.     else {
  88.         digitalWrite(red_LED_PIN, LOW);
  89.         digitalWrite(blue_LED_PIN, LOW);
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement