Advertisement
pleasedontcode

"Sensor Control" rev_02

Jun 29th, 2024
517
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: "Sensor Control"
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-06-30 02:41:56
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* sensor DHT11 untuk suhu dan kelembapan, dan LED */
  21.     /* RGB. Gunakan tombol tekan untuk beralih antara */
  22.     /* menampilkan suhu dan kelembapan pada LCD. Kontrol */
  23.     /* LED RGB berdasarkan ambang batas suhu. jika rendah */
  24.     /* berwarna hijau dan jika tinggi warna merah */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <LiquidCrystal_I2C.h>  // https://github.com/marcoschwartz/LiquidCrystal_I2C
  30. #include <DHT.h>  // https://github.com/adafruit/DHT-sensor-library
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35. void updateOutputs(void);
  36. void displayTemperature(float temperature);
  37. void displayHumidity(float humidity);
  38. void controlLED(float temperature);
  39.  
  40. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  41. const uint8_t PushButton_PIN_D2 = 2;
  42. const uint8_t DHT22_DOUT_PIN_D5 = 5;
  43.  
  44. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  45. const uint8_t led1_LEDRGB_Red_PIN_D3 = 3;
  46. const uint8_t led1_LEDRGB_Green_PIN_D4 = 4;
  47.  
  48. /***** DEFINITION OF I2C PINS *****/
  49. const uint8_t lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  50. const uint8_t lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  51. const uint8_t lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x27;  // Corrected to 0x27 based on example
  52.  
  53. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  54. /***** used to store raw data *****/
  55. bool led1_LEDRGB_Red_PIN_D3_rawData = 0;
  56. bool led1_LEDRGB_Green_PIN_D4_rawData = 0;
  57.  
  58. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  59. /***** used to store data after characteristic curve transformation *****/
  60. float led1_LEDRGB_Red_PIN_D3_phyData = 0.0;
  61. float led1_LEDRGB_Green_PIN_D4_phyData = 0.0;
  62.  
  63. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  64. LiquidCrystal_I2C lcd(lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);  // Initialize LCD with address and size
  65.  
  66. #define DHTTYPE DHT22  // Define the type of DHT sensor
  67. DHT dht(DHT22_DOUT_PIN_D5, DHTTYPE);  // Initialize DHT sensor with pin and type
  68.  
  69. /***** GLOBAL VARIABLES *****/
  70. bool displayTemperatureFlag = true;  // Flag to toggle between temperature and humidity display
  71.  
  72. void setup(void)
  73. {
  74.     // put your setup code here, to run once:
  75.  
  76.     pinMode(PushButton_PIN_D2, INPUT_PULLUP);
  77.     pinMode(DHT22_DOUT_PIN_D5, INPUT_PULLUP);
  78.  
  79.     pinMode(led1_LEDRGB_Red_PIN_D3, OUTPUT);
  80.     pinMode(led1_LEDRGB_Green_PIN_D4, OUTPUT);
  81.  
  82.     lcd.init();  // Initialize the LCD
  83.     lcd.backlight();  // Turn on the backlight
  84.  
  85.     dht.begin();  // Initialize the DHT sensor
  86.  
  87.     Serial.begin(9600);  // Initialize serial communication for debugging
  88. }
  89.  
  90. void loop(void)
  91. {
  92.     // put your main code here, to run repeatedly:
  93.  
  94.     updateOutputs(); // Refresh output data
  95.  
  96.     delay(2000);  // Delay for 2 seconds
  97.  
  98.     float h = dht.readHumidity();  // Read humidity
  99.     float t = dht.readTemperature();  // Read temperature in Celsius
  100.  
  101.     if (isnan(h) || isnan(t)) {
  102.         Serial.println(F("Failed to read from DHT sensor!"));
  103.         return;
  104.     }
  105.  
  106.     // Check if the push button is pressed
  107.     if (digitalRead(PushButton_PIN_D2) == LOW) {
  108.         displayTemperatureFlag = !displayTemperatureFlag;  // Toggle the display flag
  109.         delay(200);  // Debounce delay
  110.     }
  111.  
  112.     // Display temperature or humidity based on the flag
  113.     if (displayTemperatureFlag) {
  114.         displayTemperature(t);
  115.     } else {
  116.         displayHumidity(h);
  117.     }
  118.  
  119.     // Control the RGB LED based on temperature
  120.     controlLED(t);
  121. }
  122.  
  123. void updateOutputs()
  124. {
  125.     digitalWrite(led1_LEDRGB_Red_PIN_D3, led1_LEDRGB_Red_PIN_D3_rawData);
  126.     digitalWrite(led1_LEDRGB_Green_PIN_D4, led1_LEDRGB_Green_PIN_D4_rawData);
  127. }
  128.  
  129. void displayTemperature(float temperature)
  130. {
  131.     lcd.clear();
  132.     lcd.setCursor(0, 0);
  133.     lcd.print("Temperature:");
  134.     lcd.setCursor(0, 1);
  135.     lcd.print(temperature);
  136.     lcd.print(" C");
  137. }
  138.  
  139. void displayHumidity(float humidity)
  140. {
  141.     lcd.clear();
  142.     lcd.setCursor(0, 0);
  143.     lcd.print("Humidity:");
  144.     lcd.setCursor(0, 1);
  145.     lcd.print(humidity);
  146.     lcd.print(" %");
  147. }
  148.  
  149. void controlLED(float temperature)
  150. {
  151.     if (temperature < 25.0) {
  152.         // Temperature is low, turn on green LED
  153.         led1_LEDRGB_Green_PIN_D4_rawData = HIGH;
  154.         led1_LEDRGB_Red_PIN_D3_rawData = LOW;
  155.     } else {
  156.         // Temperature is high, turn on red LED
  157.         led1_LEDRGB_Green_PIN_D4_rawData = LOW;
  158.         led1_LEDRGB_Red_PIN_D3_rawData = HIGH;
  159.     }
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement