Advertisement
pleasedontcode

Aa rev_02

Dec 4th, 2023
57
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: Aa
  13.     - Source Code compiled for: Arduino Nano
  14.     - Source Code created on: 2023-12-04 14:25:51
  15.     - Source Code generated by: Neeraj
  16.  
  17. ********* Pleasedontcode.com **********/
  18.  
  19. /****** SYSTEM REQUIREMENTS *****/
  20. /****** SYSTEM REQUIREMENT 1 *****/
  21.     /* Lcd show current temperature and set temperature */
  22.     /* set button to select set temperature for desired */
  23.     /* temperature by up down button and led work on set */
  24.     /* temperature and off Inc temperature */
  25. /****** SYSTEM REQUIREMENT 2 *****/
  26.     /* Lcd show current temperature */
  27. /****** END SYSTEM REQUIREMENTS *****/
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Arduino.h>
  31. #include <Wire.h>
  32. #include <DS18B20.h>
  33. #include <LiquidCrystal_I2C.h>
  34.  
  35. /**
  36.  * FUNCTION PROTOTYPES
  37.  */
  38. void setup(void);
  39. void loop(void);
  40. void setTemperatureButtonInterrupt(void);
  41. float setTemperatureButtonLogic(float currentTemp);
  42. void displayTemperature(float currentTemp, float targetTemp);
  43.  
  44. /**
  45.  * DEFINITION OF DIGITAL INPUT PINS
  46.  */
  47. const uint8_t Temp_DS18B20_DQ_PIN_D2 = 2;
  48. const uint8_t Set_Temperature_Button_PIN = 3;
  49.  
  50. /**
  51.  * DEFINITION OF I2C PINS
  52.  */
  53. const uint8_t Lcd_LCD1602I2C_I2C_PIN_SDA_A4 = A4;
  54. const uint8_t Lcd_LCD1602I2C_I2C_PIN_SCL_A5 = A5;
  55. const uint8_t Lcd_LCD1602I2C_I2C_SLAVE_ADDRESS = 0x39;
  56.  
  57. /**
  58.  * GLOBAL VARIABLES
  59.  */
  60. float currentTemperature = 0;
  61. float setTemperature = 25;
  62. bool isSetMode = false;
  63.  
  64. /**
  65.  * DEFINITION OF LIBRARIES CLASS INSTANCES
  66.  */
  67. DS18B20 tempSensor(Temp_DS18B20_DQ_PIN_D2);
  68. LiquidCrystal_I2C lcd(Lcd_LCD1602I2C_I2C_SLAVE_ADDRESS, 16, 2);
  69.  
  70. void setup(void)
  71. {
  72.     // Initialize digital input pins
  73.     pinMode(Temp_DS18B20_DQ_PIN_D2, INPUT);
  74.     pinMode(Set_Temperature_Button_PIN, INPUT_PULLUP);
  75.    
  76.     // Begin LCD communication and display initial message
  77.     lcd.begin(16, 2);
  78.     lcd.backlight();
  79.     lcd.print("DS18B20 Test");
  80.  
  81.     // Attach interrupt to the set temperature button pin
  82.     attachInterrupt(digitalPinToInterrupt(Set_Temperature_Button_PIN), setTemperatureButtonInterrupt, FALLING);
  83. }
  84.  
  85. void setTemperatureButtonInterrupt(void)
  86. {
  87.   // Toggle the set temperature mode
  88.   isSetMode = !isSetMode;
  89.  
  90.   if (isSetMode)
  91.   {
  92.       lcd.setCursor(0, 1);
  93.       lcd.print("SET MODE       ");   // Update LCD to show set mode
  94.   }
  95.   else
  96.   {
  97.       lcd.clear();                  // Clear the LCD
  98.   }
  99. }
  100.  
  101. void loop(void)
  102. {
  103.     if (isSetMode)
  104.     {
  105.         setTemperature = setTemperatureButtonLogic(setTemperature); // Handle set temperature button logic when in set mode
  106.     }
  107.        
  108.     displayTemperature(currentTemperature, setTemperature); // Wait for temperature updates
  109.  
  110.     delay(1000);
  111. }
  112.  
  113.  
  114. float setTemperatureButtonLogic(float currentTemp)
  115. {
  116.     // Increment or decrement the set temperature based on the affected button press
  117.     if (digitalRead(Set_Temperature_Button_PIN) == HIGH)
  118.     {
  119.         setTemperature++;
  120.          if (setTemperature >= 50)   // Restrict the maximum set temperature to 50
  121.         {
  122.             setTemperature = 50;
  123.         }
  124.     }
  125.     else
  126.     {
  127.         setTemperature--;
  128.         if (setTemperature <= 0)    // Restrict the minimum set temperature to 0
  129.         {
  130.             setTemperature = 0;
  131.         }
  132.     }
  133.    
  134.     return setTemperature;
  135. }
  136.  
  137.  
  138. void displayTemperature(float currentTemp, float targetTemp)
  139. {
  140.     lcd.setCursor(0, 0);
  141.     lcd.print("Current: ");
  142.     lcd.print(currentTemp);
  143.     lcd.write(223);                    // Display degree symbol
  144.     lcd.print("C");
  145.     lcd.setCursor(0, 1);
  146.     lcd.print("Target : ");
  147.     lcd.print(targetTemp);
  148.     lcd.write(223);                   // Display degree symbol
  149.     lcd.print("C");
  150. }
  151.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement