// Including necessary libraries for OLED display and fonts.
#include <Wire.h>
#include <U8g2lib.h>
#include <SPI.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)
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);
// 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 percentage1;
int percentage2;
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 INPUT FOR THE INTERUPT---------//
void handleButtonPress() {
if (!isButtonPressed) { // Check if the button was just pressed
isButtonPressed = true;
buttonPressTime = millis();
}
}
//------------ FUNCTION TO HANDLE PEDAL OUTPUT--------------//
void pedaloutPut(){
// 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
percentage1 = map(brightness1, 0, 255, 0, 100);
}
//------------ FUNCTION TO SIMULATE BATTERY STATE--------------//
void batteryState(){
// 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
percentage2 = map(brightness2, 0, 255, 0, 100);
}
// ---------------FUNCTION TO HANDLE TIMER AND CLEAR OLED--------------//
// -------------AND SWITCH POWER ON AND OFF TO TRANSMIT MCU------------//
void timerOledclear(){
// 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 >= 5000) {
// 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);
}
u8g2.clearBuffer();
u8g2.sendBuffer();
// 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 {
isButtonPressed = false;// Set the button state to not pressed
}
digitalWrite(ledPin2, HIGH);// Turn on the LED when the button is released
u8g2.clearBuffer();// Update the buffer with the percentage values
u8g2.setFont(u8g2_font_ncenB10_tr);
u8g2.setCursor(0, 20);
u8g2.print("Output:");
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.setCursor(68, 22);
u8g2.print(percentage1);
u8g2.print("%");
u8g2.setFont(u8g2_font_ncenB10_tr);
u8g2.setCursor(0, 54);
u8g2.print("Battery:");
u8g2.setFont(u8g2_font_ncenB14_tr);
u8g2.setCursor(68, 56);
u8g2.print(percentage2);
u8g2.print("%");
// Update the display
u8g2.sendBuffer();
}
/**
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
u8g2.begin();
u8g2.clearBuffer();
u8g2.sendBuffer();
// Set initial value of ledPin2 to HIGH
digitalWrite(ledPin2, HIGH);
}
/**
Loop function that runs repeatedly after the setup function.
*/
void loop(){
handleButtonPress();
pedaloutPut();
batteryState();
timerOledclear();
}