/*
This code initializes an OLED display (SSD1306) using the Adafruit SSD1306 library,
and displays various text, numbers, and scroll animations on the screen.
Board: ESP32 Development Board
Component: OLED (SSD1306)
Library: https://github.com/adafruit/Adafruit_SSD1306 (Adafruit SSD1306 by Adafruit)
https://github.com/adafruit/Adafruit-GFX-Library (Adafruit GFX Library by Adafruit)
*/
/*
To do
Add segments on the screen for timer and face and other functions
So different functions can only change part of the screen
*/
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for SSD1306 display connected using I2C
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define buttonPullup 19
long currentTime = 0;
bool startStopTimer = false;
long referenceTime;
long referenceTimeForDisplay;
int cursorPosition[][2] =
{
{0,0},
{51,57},
{102,57}
};
// Include the libraries we need
#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(9600);
// initialize the OLED object
if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
;
}
pinMode(buttonPullup, INPUT_PULLUP);
// Clear the buffer.
display.setTextSize(1);
display.clearDisplay();
display.setTextColor(WHITE); // Set text color
display.setCursor(cursorPosition[0][0],cursorPosition[0][1]);
sensors.begin();
}
/*// Display Text
display.setTextSize(1); // Set text size
display.setTextColor(WHITE); // Set text color
display.setCursor(0, 28); // Set cursor position
display.println("Hello world!");
display.display(); // Display the content on the screen
delay(2000);
display.clearDisplay(); // Clear the screen
// Display Inverted Text
display.setTextColor(BLACK, WHITE); // 'inverted' text
display.setCursor(0, 28);
display.println("Hello world!");
display.display();
delay(2000);
display.clearDisplay();
// Changing Font Size
display.setTextColor(WHITE);
display.setCursor(0, 24);
display.setTextSize(2);
display.println("Hello!");
display.display();
delay(2000);
display.clearDisplay();
// Display Numbers
display.setTextSize(1);
display.setCursor(0, 28);
display.println(123456789);
display.display();
delay(2000);
display.clearDisplay();
// Display ASCII Characters
display.setCursor(0, 24);
display.setTextSize(2);
for (int i = 1; i < 8; i++) {
display.write(i);
}
display.display();
delay(2000);
display.clearDisplay();
// Scroll full screen
display.setCursor(0, 0);
display.setTextSize(1);
display.println("Full");
display.println("screen");
display.println("scrolling!");
display.display();
display.startscrollright(0x00, 0x07); // Scroll the screen to the right
delay(5000);
display.stopscroll();
delay(1000);
display.startscrollleft(0x00, 0x07); // Scroll the screen to the left
delay(5000);
display.stopscroll();
delay(1000);
display.clearDisplay();
// Scroll part of the screen
display.setCursor(0, 0);
display.setTextSize(1);
display.println("Scroll");
display.println("some part");
display.println("of the screen.");
display.display();
display.startscrollright(0x00, 0x00); // Scroll the first column of the screen to the right
delay(4000);
display.stopscroll();
display.clearDisplay();
// Display bitmap
display.drawBitmap(32, 0, sunfounderIcon, 64, 64, WHITE);
display.display();*/
void functionToSetTeCursor(int arrayNumber)
{
display.setCursor(cursorPosition[arrayNumber][0],cursorPosition[arrayNumber][1]);
}
void DisplayTemperature()
{
sensors.requestTemperatures(); // Send the command to get temperatures
//Serial.println("DONE");
// After we got the temperatures, we can print them here.
// We use the function ByIndex, and as an example get the temperature from the first sensor only.
float tempC = sensors.getTempCByIndex(0);
//display.setCursor(102, 57);
//functionToSetTeCursor(2);
// Check if reading was successful
if(tempC != DEVICE_DISCONNECTED_C)
{
//Serial.print("Temperature for the device 1 (index 0) is: ");
//Serial.println(tempC);
display.print((int)tempC);
//display.print("°C");
}
else
{
//Serial.println("Error: Could not read temperature data");
display.print("0000");
}
}
void StartTimer()
{
startStopTimer = !startStopTimer;
if(startStopTimer)
{
referenceTime = millis();
referenceTimeForDisplay = (referenceTime + 900000) - millis();
}
else
{
display.clearDisplay();
display.display();
}
}
void DisplayTimer()
{
//long displayNumber = (referenceTime + 900000) - millis();
//display.setTextSize(1);
//display.setCursor(51, 57);
//functionToSetTeCursor(1);
//display.println(displayNumber);
/* int minutes = (displayNumber/60000);
int seconds = (displayNumber % 60000);*/
//int minutes = map(displayNumber, referenceTimeForDisplay, 0, 15, 0);
//float seconds = (displayNumber % 60000);
//seconds = map(seconds, (displayNumber- minutes*1500), 0, 60, 0);
int seconds = 900000 - (millis()-referenceTime);
seconds = seconds/1000;
int minutes = seconds/60;
minutes = (int)minutes;
seconds = seconds - (minutes*60);
display.print(minutes);
display.print(":");
display.print(seconds);
}
void makeScreenAppear()
{
display.clearDisplay();
functionToSetTeCursor(0);
functionToSetTeCursor(1);
if(startStopTimer)
{
DisplayTimer();
}
functionToSetTeCursor(2);
DisplayTemperature();
display.display();
}
void loop() {
if(digitalRead(buttonPullup)==0)
{
while((digitalRead(buttonPullup)==0)){}
delay(50);
//Serial.println("It works");
StartTimer();
}
//display.clearDisplay();
makeScreenAppear();
//display.display();
delay(100);
}Loading
ds18b20
ds18b20