Arduino_Temperature_with_Di.../main/main.ino

72 lines
2 KiB
Arduino
Raw Normal View History

2025-09-13 12:29:32 +02:00
// for sensor
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// for the display
#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 an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Other global variables
bool fillCircle = false;
// Draw a small circle, change between emtpy and full to indicate that there is an update on the display and not stock
void drawCircle() {
if(fillCircle){
display.fillCircle(5, 5, 5, WHITE);
fillCircle = !fillCircle;
} else {
display.drawCircle(5, 5, 5, WHITE);
fillCircle = !fillCircle;
}
}
// small function to write text on the display with option for changing font
void textToDisplay(char* text = "default", int fontSize = 1) {
display.clearDisplay();
drawCircle();
display.setTextSize(fontSize);
display.setTextColor(WHITE);
display.setCursor(20, 0);
// Display static text
display.println(text);
display.display();
}
void setup() {
Serial.begin(9600);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
// Start up the library
sensors.begin();
textToDisplay("Finished setup...");
delay(1000);
}
void loop() {
// Call sensors.requestTemperatures() to issue a global temperature and Requests to all devices on the bus
sensors.requestTemperatures();
float temp = sensors.getTempCByIndex(0);
String message = String(temp) + " C"; // Convert float to String, then concatenate
textToDisplay(message.c_str(),2);
Serial.println(message.c_str());
delay(1000);
}