//promt Gemini
// arduino nano, rtc ds1307, ssd1306 oled display, a push button with bounce to switch display pages, ntc temperature sensor.
//Create a code that integrates all the components. eliminate bounce for the buttob. the oled shall display the time from the rtc as the first screen.
//After the button is pushed the temperature screen shall be shown. After the next push the time shall be seen
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_BMP085.h>
#include "RTClib.h"
// OLED Display Configuration
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1 // Share Nano reset pin
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// Sensor & RTC Objects
RTC_DS1307 rtc;
Adafruit_BMP085 bmp;
// Button Pin Configuration
const int BUTTON_PIN = 2;
// Display Page State Tracking
int currentPage = 0;
const int TOTAL_PAGES = 3; // Page 0: Time, Page 1: Temperature, Page 2: Pressure
// Debounce Variables
int buttonState = HIGH; // Current reading from input pin
int lastButtonState = HIGH; // Previous reading from input pin
unsigned long lastDebounceTime = 0; // Last time the output pin was toggled
const unsigned long debounceDelay = 50; // Debounce time in milliseconds
void setup() {
Serial.begin(9600);
// Initialize Push Button with Internal Pull-up Resistor
pinMode(BUTTON_PIN, INPUT_PULLUP);
// Initialize OLED Display (Default I2C address is 0x3C)
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
// Initialize RTC DS1307
if (!rtc.begin()) {
Serial.println(F("Couldn't find RTC"));
for (;;);
}
if (!rtc.isrunning()) {
// Automatically sets RTC to the date & time this sketch was compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}
// Initialize BMP180 Sensor
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP180 sensor!"));
for (;;);
}
}
void loop() {
handleButtonDebounce();
updateDisplay();
delay(100); // Gentle refresh rate loop
}
// Function to handle software debouncing and page cycling
void handleButtonDebounce() {
int reading = digitalRead(BUTTON_PIN);
// Check if button state changed due to noise or pressing
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// If state reading has stayed for longer than debounce delay, take it as real
if (reading != buttonState) {
buttonState = reading;
// Only trigger page change on button press down (LOW because of INPUT_PULLUP)
if (buttonState == LOW) {
currentPage = (currentPage + 1) % TOTAL_PAGES; // Cycle through 0 -> 1 -> 2 -> 0
}
}
}
lastButtonState = reading;
}
// Function to render content based on the active page state
void updateDisplay() {
display.clearDisplay();
switch (currentPage) {
case 0:
showTimeScreen();
break;
case 1:
showTemperatureScreen();
break;
case 2:
showPressureScreen();
break;
}
display.display();
}
// Page 0: RTC Time Display
void showTimeScreen() {
DateTime now = rtc.now();
display.setTextSize(1);
display.setCursor(0, 0);
//display.print(F("--- REAL TIME ---"));
// Format Date (YYYY/MM/DD)
/* display.setCursor(10, 20);
display.setTextSize(1);
if (now.year() < 10) display.print('0');
display.print(now.year(), DEC);
display.print('/');
if (now.month() < 10) display.print('0');
display.print(now.month(), DEC);
display.print('/');
if (now.day() < 10) display.print('0');
display.print(now.day(), DEC); */
// Format Time (HH:MM:SS)
display.setCursor(10, 40);
display.setTextSize(2);
if (now.hour() < 10) display.print('0');
display.print(now.hour(), DEC);
display.print(':');
if (now.minute() < 10) display.print('0');
display.print(now.minute(), DEC);
display.print(':');
if (now.second() < 10) display.print('0');
display.print(now.second(), DEC);
}
// Page 1: BMP180 Temperature Display
void showTemperatureScreen() {
float temp = bmp.readTemperature();
display.setTextSize(1);
display.setCursor(0, 0);
display.print(F("Oeltemperatur"));
display.setCursor(15, 30);
display.setTextSize(2);
display.print(temp, 1);
display.setTextSize(1);
display.print(F(" "));
display.setTextSize(2);
display.print((char)247); // Degree symbol
display.print(F("C"));
}
// Page 2: BMP180 Pressure Display
void showPressureScreen() {
float pressurePa = bmp.readPressure();
float pressureHpa = pressurePa / 100.0; // Convert Pa to hPa
display.setTextSize(1);
display.setCursor(0, 0);
display.print(F("Oeldruck"));
display.setCursor(10, 30);
display.setTextSize(2);
display.print(pressureHpa, 1);
display.setTextSize(1);
display.print(F(" hPa"));
}Loading
bmp180
bmp180