// Tiny4kOLED documentation:
// https://github.com/datacute/Tiny4kOLED/tree/master?tab=readme-ov-file
// DHT22 documentation:
// https://github.com/RobTillaart/DHTlib
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <TinyWireM.h>
#include <Tiny4kOLED.h>
#include <dht.h>
#define PIN_BUTTON 1
#define PIN_DHT 4
dht DHT;
void setup() {
pinMode(PIN_BUTTON, INPUT);
pinMode(PIN_DHT, INPUT);
oled.begin(128, 64, sizeof(tiny4koled_init_128x64br), tiny4koled_init_128x64br);
oled.setRotation(1);
oled.setFont(FONT8X16);
oled.clear();
oled.off();
}
void sleep() {
// The POWER DOWN sleep mode does not seems to be simulated on Wokwi…
GIMSK |= _BV(PCIE); // Enable Pin Change Interrupts
PCMSK |= _BV(PCINT1); // Use PB1 as interrupt pin
ADCSRA &= ~_BV(ADEN); // ADC off
set_sleep_mode(SLEEP_MODE_PWR_DOWN); // replaces above statement
sleep_enable(); // Sets the Sleep Enable bit in the MCUCR Register (SE BIT)
sei(); // Enable interrupts
sleep_cpu(); // sleep
cli(); // Disable interrupts
PCMSK &= ~_BV(PCINT1); // Turn off PB1 as interrupt pin
sleep_disable(); // Clear SE bit
ADCSRA |= _BV(ADEN); // ADC on
sei(); // Enable interrupts
}
void loop() {
if (digitalRead(PIN_BUTTON) == HIGH) {
oled.on();
displayTemperature();
delay(5000);
oled.off();
}
sleep();
}
void displayTemperature() {
DHT.read22(PIN_DHT);
float t = DHT.temperature;
float h = DHT.humidity;
oled.setCursor(0, 0);
oled.print("T: ");
oled.print(t);
oled.setCursor(0, 2);
oled.print("H: ");
oled.print(h);
}
ISR(PCINT0_vect) { }