#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define BATTERY_PIN 34
float feg;
float sen;
float cels;
#define TEMP_PIN 35
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int previousBatteryLevel = 0;
void setup() {
Serial.begin(115200);
pinMode(BATTERY_PIN, INPUT);
pinMode(TEMP_PIN, INPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay(); // Clear the display on startup
display.display(); // Show the cleared display
}
float convertToVoltage(int adcValue) {
// Assuming ESP32 uses 12-bit ADC, so 2^12 = 4096
return (adcValue / 4096.0) * 3.3;
}
int calculateBatteryPercentage(float voltage) {
// Assuming 0V is 0% and 3.3V is 100%
return map(voltage, 0, 3.3, 0, 100);
}
void loop() {
sen = analogRead(TEMP_PIN);
feg = (sen * 9.0 / 5.0) + 32.0;
Serial.println("feg");
Serial.println(feg);
cels = (feg - 32.0) * 5.0 / 9.0;
Serial.println("cel");
Serial.println(cels);
int batteryLevel = analogRead(BATTERY_PIN);
float batteryVoltage = convertToVoltage(batteryLevel);
int batteryPercentage = calculateBatteryPercentage(batteryVoltage);
// Ensure the battery percentage is within the range 0-100
batteryPercentage = constrain(batteryPercentage, 0, 100);
display.clearDisplay(); // Clear the display before updating
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Temperature:");
display.setCursor(0, 10);
display.println(cels);
display.setCursor(37, 10);
display.print("C");
display.setCursor(0, 30);
display.println("Temperature:");
display.setCursor(0, 40);
display.println(feg);
display.setCursor(37, 40);
display.print("F");
int batteryHeight = map(batteryPercentage, 0, 100, 0, 28);
int batteryX = SCREEN_WIDTH - 18;
int batteryY = 8;
int batteryWidth = 13;
int batteryTopHeight = 2;
int batteryBottomHeight = 30;
int batteryFillHeight = map(batteryPercentage, 0, 100, 0, batteryBottomHeight);
// Ensure that the battery fill height doesn't exceed the battery block's height
batteryFillHeight = min(batteryFillHeight, batteryBottomHeight);
display.drawRect(batteryX, batteryY + batteryTopHeight, batteryWidth, batteryBottomHeight, SSD1306_WHITE);
display.fillRect(batteryX + 1, batteryY + batteryTopHeight + (batteryBottomHeight - batteryFillHeight), batteryWidth - 2, batteryFillHeight, SSD1306_WHITE);
// Display battery percentage with limited space
display.setCursor(110 - (batteryPercentage >= 100 ? 6 : 0), 0); // Adjusted cursor position
display.print(String(batteryPercentage) + "%");
// Display battery voltage for debugging purposes
display.setCursor(0, 50);
display.print("Voltage: ");
display.print(batteryVoltage, 2);
display.print("V");
display.display(); // Show the updated display
delay(2000);
}
/*#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define BATTERY_PIN 34
float feg;
float sen;
float cels;
#define TEMP_PIN 35
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int previousBatteryLevel = 0;
void setup() {
Serial.begin(115200);
pinMode(BATTERY_PIN, INPUT);
pinMode(TEMP_PIN, INPUT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay(); // Clear the display on startup
display.display(); // Show the cleared display
}
void loop() {
sen = analogRead(TEMP_PIN);
feg = (sen * 9.0 / 5.0) + 32.0;
Serial.println("feg");
Serial.println(feg);
cels = (feg - 32.0) * 5.0 / 9.0;
Serial.println("cel");
Serial.println(cels);
int batteryLevel = analogRead(BATTERY_PIN);
int batteryPercentage = map(batteryLevel, 0, 1023, 0, 100);
// Ensure the battery percentage is within the range 0-100
batteryPercentage = constrain(batteryPercentage, 0, 100);
display.clearDisplay(); // Clear the display before updating
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.print("Temperature:");
display.setCursor(0, 10);
display.println(cels);
display.setCursor(37, 10);
display.print("C");
display.setCursor(0, 30);
display.println("Temperature:");
display.setCursor(0, 40);
display.println(feg);
display.setCursor(37, 40);
display.print("F");
int batteryHeight = map(batteryPercentage, 0, 100, 0, 28);
int batteryX = SCREEN_WIDTH - 18;
int batteryY = 8;
int batteryWidth = 13;
int batteryTopHeight = 2;
int batteryBottomHeight = 30;
display.drawRect(batteryX, batteryY + batteryTopHeight, batteryWidth, batteryBottomHeight, SSD1306_WHITE);
int batteryFillHeight = map(batteryPercentage, 0, 100, 0, batteryBottomHeight);
display.fillRect(batteryX + 1, batteryY + batteryTopHeight + (batteryBottomHeight - batteryFillHeight), batteryWidth - 2, batteryFillHeight, SSD1306_WHITE);
display.setCursor(110, 0);
display.print(String(batteryPercentage) + "%");
if (batteryLevel > previousBatteryLevel) {
display.setCursor(110, 40);
} else if (batteryLevel < previousBatteryLevel) {
display.setCursor(110, 40);
} else {
display.setCursor(110, 40);
}
display.display(); // Show the updated display
previousBatteryLevel = batteryLevel;
delay(2000);
}*/
/*#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
#define BATTERY_PIN A0 // Analog pin connected to the battery level
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
int previousBatteryLevel = 0;
void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
// Display static text
display.print("Temperature:");
display.setCursor(0,10);
display.print("28.81");
display.setCursor(37,10);
display.print("C");
display.display();
}
void loop() {
int batteryLevel = analogRead(BATTERY_PIN); // Read battery level (0-1023)
int batteryPercentage = map(batteryLevel, 0, 1023, 0, 100); // Map to percentage
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
// Display static text
display.print("Temperature:");
display.setCursor(0,10);
display.print("28.81");
display.setCursor(37,10);
display.print("C");
display.setCursor(0,30);
// Display static text
display.println("Temperature:");
display.setCursor(0,40);
display.println("83.86");
display.setCursor(37,40);
display.print("F");
// Calculate battery level height based on batteryPercentage
int batteryHeight = map(batteryPercentage, 0, 100, 0, 28);
// Draw the battery shape on the right side
int batteryX = SCREEN_WIDTH - 18;
int batteryY = 8;
int batteryWidth = 13;
int batteryTopHeight = 2;
int batteryBottomHeight = 30;
display.drawRect(batteryX, batteryY + batteryTopHeight, batteryWidth, batteryBottomHeight, SSD1306_WHITE);
// Fill the battery level indicator on the right side
int batteryFillHeight = map(batteryPercentage, 0, 100, 0, batteryBottomHeight);
display.fillRect(batteryX + 1, batteryY + batteryTopHeight + (batteryBottomHeight - batteryFillHeight), batteryWidth - 2, batteryFillHeight, SSD1306_WHITE);
// Display battery percentage
display.setCursor(110, 0);
display.print(String(batteryPercentage) + "%");
// Check if battery level is increasing or decreasing
if (batteryLevel > previousBatteryLevel) {
display.setCursor(110, 40);
//display.print("Charging");
} else if (batteryLevel < previousBatteryLevel) {
display.setCursor(110, 40);
//display.print("Discharging");
} else {
display.setCursor(110, 40);
display.print("Stable");
}
display.display();
previousBatteryLevel = batteryLevel;
delay(2000); // Update every 2 seconds or as needed
}*/