pleasedontcode

Device Management rev_01

Aug 13th, 2025
468
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: Device Management
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2025-08-13 12:36:48
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* esp32 with i2c display,one relay out,one swich for */
  21.     /* start, one swich for stop, one swich for time */
  22.     /* setting up,one swich for time setting down,one */
  23.     /* ds18b20 temperature sensor. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <Wire.h>
  29. #include <Adafruit_SSD1306.h>
  30. #include <OneWire.h>
  31. #include <DallasTemperature.h>
  32.  
  33. // Define display parameters
  34. #define SCREEN_WIDTH 128 // OLED display width, in pixels
  35. #define SCREEN_HEIGHT 64 // OLED display height, in pixels
  36. #define OLED_RESET    -1 // Reset pin # (or -1 if sharing Arduino reset pin)
  37.  
  38. // Instantiate display object
  39. Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
  40.  
  41. // Define pins for switches
  42. const int startSwitchPin = 2;   // Start button
  43. const int stopSwitchPin = 3;    // Stop button
  44. const int timeUpSwitchPin = 4;  // Time setting up
  45. const int timeDownSwitchPin = 5; // Time setting down
  46.  
  47. // Define pin for relay
  48. const int relayPin = 6;
  49.  
  50. // Define pin for DS18B20 temperature sensor
  51. const int oneWireBusPin = 7;
  52.  
  53. // Create OneWire and DallasTemperature objects
  54. OneWire oneWire(oneWireBusPin);
  55. DallasTemperature sensors(&oneWire);
  56.  
  57. // Variables for system state
  58. bool systemRunning = false;
  59. unsigned long lastDebounceTime = 0;
  60. unsigned long debounceDelay = 50; // milliseconds
  61. int temperature = 0;
  62. int setTimeMinutes = 0;
  63.  
  64. void setup() {
  65.   // Initialize serial communication
  66.   Serial.begin(115200);
  67.  
  68.   // Initialize display
  69.   if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
  70.     Serial.println(F("SSD1306 allocation failed"));
  71.     for(;;); // Loop forever
  72.   }
  73.   display.clearDisplay();
  74.   display.setTextSize(1);
  75.   display.setTextColor(SSD1306_WHITE);
  76.   display.setCursor(0,0);
  77.   display.println("Initializing...");
  78.   display.display();
  79.  
  80.   // Initialize pins
  81.   pinMode(startSwitchPin, INPUT_PULLUP);
  82.   pinMode(stopSwitchPin, INPUT_PULLUP);
  83.   pinMode(timeUpSwitchPin, INPUT_PULLUP);
  84.   pinMode(timeDownSwitchPin, INPUT_PULLUP);
  85.   pinMode(relayPin, OUTPUT);
  86.  
  87.   // Initialize relay
  88.   digitalWrite(relayPin, LOW); // Relay off
  89.  
  90.   // Initialize temperature sensor
  91.   sensors.begin();
  92.  
  93.   // Initial display update
  94.   updateDisplay();
  95. }
  96.  
  97. void loop() {
  98.   handleSwitches();
  99.   readTemperature();
  100.   updateDisplay();
  101.   delay(200); // Loop delay
  102. }
  103.  
  104. void handleSwitches() {
  105.   // Read switch states
  106.   bool startState = digitalRead(startSwitchPin) == LOW;
  107.   bool stopState = digitalRead(stopSwitchPin) == LOW;
  108.   bool timeUpState = digitalRead(timeUpSwitchPin) == LOW;
  109.   bool timeDownState = digitalRead(timeDownSwitchPin) == LOW;
  110.  
  111.   unsigned long currentTime = millis();
  112.  
  113.   // Debounce logic can be added here if needed
  114.  
  115.   if (startState) {
  116.     systemRunning = true;
  117.     digitalWrite(relayPin, HIGH); // Turn relay on
  118.   }
  119.   if (stopState) {
  120.     systemRunning = false;
  121.     digitalWrite(relayPin, LOW); // Turn relay off
  122.   }
  123.   if (timeUpState) {
  124.     setTimeMinutes++;
  125.   }
  126.   if (timeDownState) {
  127.     if (setTimeMinutes > 0) {
  128.       setTimeMinutes--;
  129.     }
  130.   }
  131. }
  132.  
  133. void readTemperature() {
  134.   sensors.requestTemperatures();
  135.   temperature = sensors.getTempCByIndex(0);
  136. }
  137.  
  138. void updateDisplay() {
  139.   display.clearDisplay();
  140.   display.setCursor(0,0);
  141.   display.println("ESP32 I2C Display");
  142.   display.println("-----------------");
  143.   display.print("Temp: ");
  144.   display.print(temperature);
  145.   display.println(" C");
  146.   display.print("Set Time: ");
  147.   display.print(setTimeMinutes);
  148.   display.println(" min");
  149.   display.println(systemRunning ? "Status: RUNNING" : "Status: STOPPED");
  150.   display.display();
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment