Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: ESP32 Essentials
- - Source Code NOT compiled for: Arduino Nano ESP32
- - Source Code created on: 2025-08-13 12:39:59
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* esp32 with i2c display,one relay out,one swich for */
- /* start, one swich for stop, one swich for time */
- /* setting up,one swich for time setting down,one */
- /* ds18b20 temperature sensor. */
- /****** END SYSTEM REQUIREMENTS *****/
- /********* User code review feedback **********
- #### Feedback 1 ####
- - not working
- ********* User code review feedback **********/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <Adafruit_SSD1306.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- // Define display parameters
- #define SCREEN_WIDTH 128 // OLED display width, in pixels
- #define SCREEN_HEIGHT 64 // OLED display height, in pixels
- #define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
- // Instantiate display object
- Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
- // Define pins for switches
- const int startSwitchPin = 2; // Start button
- const int stopSwitchPin = 3; // Stop button
- const int timeUpSwitchPin = 4; // Time setting up
- const int timeDownSwitchPin = 5; // Time setting down
- // Define pin for relay
- const int relayPin = 6;
- // Define pin for DS18B20 temperature sensor
- const int oneWireBusPin = 7;
- // Create OneWire and DallasTemperature objects
- OneWire oneWire(oneWireBusPin);
- DallasTemperature sensors(&oneWire);
- // Variables for system state
- bool systemRunning = false;
- int temperature = 0;
- int setTimeMinutes = 0;
- void setup() {
- // Initialize serial communication
- Serial.begin(115200);
- // Initialize display
- if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
- Serial.println(F("SSD1306 allocation failed"));
- for(;;); // Loop forever
- }
- display.clearDisplay();
- display.setTextSize(1);
- display.setTextColor(SSD1306_WHITE);
- display.setCursor(0,0);
- display.println("Initializing...");
- display.display();
- // Initialize pins
- pinMode(startSwitchPin, INPUT_PULLUP);
- pinMode(stopSwitchPin, INPUT_PULLUP);
- pinMode(timeUpSwitchPin, INPUT_PULLUP);
- pinMode(timeDownSwitchPin, INPUT_PULLUP);
- pinMode(relayPin, OUTPUT);
- // Initialize relay
- digitalWrite(relayPin, LOW); // Relay off
- // Initialize temperature sensor
- sensors.begin();
- // Initial display update
- updateDisplay();
- }
- void loop() {
- handleSwitches();
- readTemperature();
- updateDisplay();
- delay(200); // Loop delay
- }
- void handleSwitches() {
- // Read switch states
- bool startState = digitalRead(startSwitchPin) == LOW;
- bool stopState = digitalRead(stopSwitchPin) == LOW;
- bool timeUpState = digitalRead(timeUpSwitchPin) == LOW;
- bool timeDownState = digitalRead(timeDownSwitchPin) == LOW;
- if (startState) {
- systemRunning = true;
- digitalWrite(relayPin, HIGH); // Turn relay on
- }
- if (stopState) {
- systemRunning = false;
- digitalWrite(relayPin, LOW); // Turn relay off
- }
- if (timeUpState) {
- setTimeMinutes++;
- }
- if (timeDownState) {
- if (setTimeMinutes > 0) {
- setTimeMinutes--;
- }
- }
- }
- void readTemperature() {
- sensors.requestTemperatures();
- temperature = sensors.getTempCByIndex(0);
- }
- void updateDisplay() {
- display.clearDisplay();
- display.setCursor(0,0);
- display.println("ESP32 I2C Display");
- display.println("-----------------");
- display.print("Temp: ");
- display.print(temperature);
- display.println(" C");
- display.print("Set Time: ");
- display.print(setTimeMinutes);
- display.println(" min");
- display.println(systemRunning ? "Status: RUNNING" : "Status: STOPPED");
- display.display();
- }
Advertisement
Add Comment
Please, Sign In to add comment