// Including necessary libraries for OLED display and fonts.
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSansBold12pt7b.h>
#include <Fonts/FreeSansBold9pt7b.h>
// Define the screen width and height
#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);
// Define the pin numbers for the LED, potentiometer, battery, and button
int ledPin1 = 5;
int potPin = A1;
int ledPin2 = 6;
int batPin = A2;
int buttonPin = 2; // Changed to pin 2 to support interrupts
// Variables to store the state and time of the button press
volatile unsigned long buttonPressTime = 0;
volatile bool isButtonPressed = false;
// Variables to store the values from the potentiometer and battery
int potIn;
int batIn;
// Variables to hold the PWM values for the LEDs
int brightness1;
int brightness2;
// Variable to hold the state of the LED
bool led1State = false;
bool led2State = true;
// Variables to control the LED blinking
unsigned long lastLedChange = 0; // Last time the LED state was changed
unsigned long ledOnTime = 150; // LED on time in milliseconds
unsigned long ledOffTime = 2000; // LED off time in milliseconds
// /**
// Function to handle the button press interrupt.
// */
// void handleButtonPress() {
// // Check if the button was just pressed
// if (!isButtonPressed) {
// isButtonPressed = true;
// buttonPressTime = millis();
// }
// }
/**
Setup function that runs once at the start of the program.
*/
void setup()
{
// Initialize the serial communication
Serial.begin(57600);
// Set the LED pins as output
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
// Set the button pin as input with internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// // Attach an interrupt to the button pin
// attachInterrupt(digitalPinToInterrupt(buttonPin), handleButtonPress, FALLING);
// Initialize the SSD1306 display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.display();
display.setTextColor(WHITE);
// Set initial value of ledPin2 to HIGH
digitalWrite(ledPin2, HIGH);
}
/**
Loop function that runs repeatedly after the setup function.
*/
void loop()
{
// Read the value from the potentiometer
potIn = analogRead(potPin);
// Map the potentiometer value to the LED brightness range
brightness1 = map(potIn, 0, 1023, 0, 255);
// Map the brightness value to a percentage range
brightness1 = map(brightness1, 0, 255, 0, 100);
// Delay for stability
delay(10);
// Read the value from the battery
batIn = analogRead(batPin);
// Map the battery value to the LED brightness range
brightness2 = map(batIn, 0, 1023, 0, 255);
// Map the brightness value to a percentage range
brightness2 = map(brightness2, 0, 255, 0, 100);
// Delay for stability
delay(10);
// Check if the button is pressed
if (digitalRead(buttonPin) == LOW) {
// Check if the button was just pressed
if (!isButtonPressed) {
isButtonPressed = true;
buttonPressTime = millis();
}
// Check if the button has been pressed for 5 seconds
else if (millis() - buttonPressTime >= 2000) {
// Blink the LED without delay
if (led1State && millis() - lastLedChange >= ledOnTime) {
led1State = false;
lastLedChange = millis();
digitalWrite(ledPin1, LOW);
} else if (!led1State && millis() - lastLedChange >= ledOffTime) {
led1State = true;
lastLedChange = millis();
digitalWrite(ledPin1, HIGH);
}
display.clearDisplay();
display.display();
// Turn off the LED when the button is pressed
digitalWrite(ledPin2, LOW);
// Set the button state to pressed
isButtonPressed = true;
// Exit the loop
return;
}
} else {
// Set the button state to not pressed
isButtonPressed = false;
}
// Turn on the LED when the button is released
digitalWrite(ledPin2, HIGH);
// Update the display with the brightness values
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("%");
// Update the display
display.display();
}