#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSansBold12pt7b.h>
#include <Fonts/FreeSansBold9pt7b.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)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
int ledPin1 = 5;
int potPin = A1;
int ledPin2 = 6;
int batPin = A2;
int buttonPin = 7; // Button pin
unsigned long buttonPressTime = 0; // Time when the button was pressed
bool isButtonPressed = false; // Whether the button is pressed
int potIn; // variable to store the value coming from the potentiometer
int batIn; // variable to store the value coming from the battery
int brightness1; // variable to hold the pwm value1
int brightness2; // variable to hold the pwm value2
void setup() {
Serial.begin(57600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Set button pin as INPUT_PULLUP
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
}
void loop()
{
potIn = analogRead(potPin); //reading from potentiometer
brightness1 = map(potIn, 0, 1023, 0, 255); //mapping the Values between 0 to 255
analogWrite(ledPin1, brightness1);
brightness1 = map(brightness1, 0, 253, 0, 100); //mapping the Values between 0 to 100
delay (10);
batIn = analogRead(batPin); //reading from battery
brightness2 = map(batIn, 0, 1023, 0, 255); //mapping the Values between 0 to 255
analogWrite(ledPin2, brightness2);
brightness2 = map(brightness2, 0, 253, 0, 100); //mapping the Values between 0 to 100
delay (10);
if (digitalRead(buttonPin) == LOW) {
if (!isButtonPressed) {
isButtonPressed = true;
buttonPressTime = millis();
} else if (millis() - buttonPressTime >= 5000) { // If button is pressed for 5 seconds
display.clearDisplay();
display.display();
isButtonPressed = true;
return;
}
} else {
isButtonPressed = false;
}
display.clearDisplay();
display.setFont(&FreeSansBold9pt7b);
display.invertDisplay(false);
display.setCursor(0, 20);
display.print("Output:");
display.setFont(&FreeSansBold12pt7b);
display.setCursor(68, 22);
display.print(brightness1);
display.print("%");
display.setFont(&FreeSansBold9pt7b);
display.setCursor(0, 54);
display.print("Battery:");
display.setFont(&FreeSansBold12pt7b);
display.setCursor(68, 56);
display.print(brightness2);
display.print("%");
display.display();
}