// 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, hallsensor, and button
int potPin = A1;
int greenLedPin = 6;
int outMain = 3;
int outHall = 8;
int outAnalogSw = 9; // To control Analog Switch select pin
int hallinput = A2;
int buttonPin = 2; // Changed to pin 2 to support interrupts
// Define the variables for the HallSensor Trigger //
int hallValue;
int hThreshold = 20;
int lThreshold = 5;
// 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 hallIn;
// 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 ledState = false;
// 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 HALL SENSOR RE-BOOT OFF MAIN MCU------------//
void hallTreshholdSwitch() {
hallIn = analogRead(hallinput);
hallValue = map(hallIn, 0, 1023, 0, 100);
if (hallValue > hThreshold) {
digitalWrite(outAnalogSw, HIGH);
} else if (hallValue < lThreshold) {
digitalWrite(outAnalogSw, LOW);
}
Serial.print( "%");
Serial.println(hallValue);
delay(10);
}
//-------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 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 (ledState && millis() - lastLedChange >= ledOnTime) {
ledState = false;
lastLedChange = millis();
digitalWrite(greenLedPin, LOW);
} else if (!ledState && millis() - lastLedChange >= ledOffTime) {
ledState = true;
lastLedChange = millis();
digitalWrite(greenLedPin, HIGH);
}
display.clearDisplay();
display.display();
// Turn on the LED when 5 seconds have elapsed after the button is pressed
digitalWrite(outMain, HIGH);
digitalWrite(outHall, HIGH);
// Set the button state to pressed
isButtonPressed = true;
// Exit the loop
return;
}
} else {
isButtonPressed = false;// Set the button state to not pressed
}
digitalWrite(greenLedPin, HIGH);
digitalWrite(outMain, LOW);// Turn on the LED when the button is released
digitalWrite(outHall, LOW);
display.clearDisplay();// Update the buffer with the percentage values
display.setFont(&FreeSansBold9pt7b);
display.invertDisplay(false);
display.setCursor(0, 20);
display.print("Output:");
display.setFont(&FreeSansBold12pt7b);
display.setCursor(68, 22);
display.print(percentage1);
display.print("%");
display.setFont(&FreeSansBold9pt7b);
display.setCursor(0, 54);
display.print("Battery:");
display.setFont(&FreeSansBold12pt7b);
display.setCursor(68, 56);
display.print(hallValue);
display.print("%");
// Update the display
display.display();
}
/**
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(greenLedPin, OUTPUT);
pinMode(outMain, OUTPUT);
pinMode(outHall, OUTPUT);
pinMode(outAnalogSw, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Set the button pin as input
digitalWrite(greenLedPin, HIGH); // Sets Green power led on as soon as bootup
// Attach an interrupt to the button pin
attachInterrupt(digitalPinToInterrupt(buttonPin), handleButtonPress, FALLING);
// Initialize the SSD1306 display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
//delay(2000);
static const unsigned char PROGMEM image_data_LOGOarray[] =
{
// Add Splash Screen LOGO C-File Array here
// 'Screenshot RussRessto's 128x64, 128x64px
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x08, 0x18, 0x1c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xcc, 0x19, 0x98, 0x33, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x07, 0xf8, 0x1f, 0xe0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x10, 0x00, 0x00, 0x04, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x0f, 0xf0, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x83, 0x03, 0x80, 0x01, 0xc0, 0xc1, 0xc0, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x06, 0x7c, 0x18, 0x00, 0x00, 0x18, 0x3e, 0x60, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x60, 0x00, 0x00, 0x06, 0x00, 0x30, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x18, 0x01, 0x80, 0x00, 0x00, 0x01, 0x80, 0x18, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x30, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x08, 0x7f, 0xff, 0xf0, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x3f, 0xff, 0xff, 0xf0, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xfe, 0x00, 0x3f, 0xff, 0xff, 0xf0, 0x00, 0xff, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3f, 0x80, 0x3e, 0x7c, 0xfb, 0xf0, 0x03, 0xe1, 0xe0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0x9f, 0x3e, 0x7c, 0xf9, 0xf3, 0xf9, 0xc0, 0xf0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1f, 0xbf, 0x3e, 0x7c, 0xf9, 0xf3, 0xfd, 0xc0, 0xf0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x3f, 0x80, 0x3e, 0x7c, 0xf9, 0xf0, 0x01, 0xe3, 0xe0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xfe, 0x00, 0x3e, 0x7c, 0xf9, 0xf0, 0x00, 0xff, 0x80, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x38, 0x60, 0xc6, 0x30, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0xff, 0xfe, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x7f, 0xfc, 0xc2, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x18, 0x01, 0x80, 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x0c, 0x00, 0x60, 0x00, 0x00, 0x06, 0x18, 0x30, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x06, 0xfc, 0x0c, 0x00, 0x00, 0x30, 0x77, 0x60, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x03, 0x03, 0x01, 0xe0, 0x07, 0x80, 0xc0, 0x80, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x03, 0x00, 0x00, 0xc0, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x06, 0x06, 0x78, 0x1e, 0x60, 0x60, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xec, 0x18, 0x18, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x18, 0x18, 0x18, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x1f, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x80, 0x00,
0x1c, 0x70, 0x00, 0x00, 0x00, 0x00, 0x0e, 0x38, 0x00, 0x00, 0x00, 0x30, 0x00, 0x03, 0x80, 0x00,
0x18, 0x70, 0xe3, 0x83, 0xe0, 0x7e, 0x0e, 0x18, 0x1f, 0x81, 0xf8, 0x7c, 0x1f, 0x83, 0x83, 0xe0,
0x18, 0x70, 0xe3, 0x87, 0x70, 0xe7, 0x0e, 0x18, 0x79, 0xc3, 0x9c, 0x78, 0x39, 0xc1, 0x0e, 0x70,
0x1c, 0x70, 0xe3, 0x8e, 0x00, 0xe0, 0x0e, 0x38, 0x70, 0xc3, 0x00, 0x30, 0x70, 0xe0, 0x0e, 0x00,
0x1f, 0xe0, 0xe3, 0x87, 0x00, 0xe0, 0x0f, 0xf8, 0x79, 0xc3, 0x80, 0x30, 0x70, 0xe0, 0x0f, 0x00,
0x19, 0xc0, 0xe3, 0x83, 0xf0, 0x7f, 0x0e, 0x70, 0x7f, 0xc1, 0xfc, 0x30, 0x70, 0xe0, 0x03, 0xf0,
0x18, 0xe0, 0xe3, 0x80, 0x38, 0x07, 0x0e, 0x30, 0x70, 0x00, 0x1c, 0x30, 0x70, 0xe0, 0x00, 0x38,
0x18, 0x70, 0xe3, 0x8e, 0x38, 0xe7, 0x0e, 0x38, 0x71, 0xc3, 0x9c, 0x38, 0x70, 0xe0, 0x0e, 0x38,
0x18, 0x38, 0x7f, 0x87, 0xf0, 0x7e, 0x0e, 0x1c, 0x3f, 0x83, 0xf8, 0x3e, 0x3f, 0xc0, 0x07, 0xf0,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x02, 0xa4, 0x42, 0x92, 0x94, 0x12, 0x90, 0xa5, 0x33, 0x25, 0x29, 0x40, 0x00, 0x00,
0x00, 0x00, 0x03, 0xc6, 0x42, 0x93, 0x90, 0x13, 0x9c, 0x93, 0x30, 0x25, 0x39, 0x80, 0x00, 0x00,
0x00, 0x00, 0x01, 0x44, 0x42, 0x92, 0x94, 0x12, 0x94, 0x82, 0x70, 0x25, 0x29, 0x40, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
delay(10);
display.clearDisplay();
display.drawBitmap(0, 0, image_data_LOGOarray, 128, 64, 1);
display.display();
display.setTextColor(WHITE);
delay(5000); // Keeps LOGOarray on OLED 5 seconds before Looping
}
/**
Loop function that runs repeatedly after the setup function.
*/
void loop() {
hallTreshholdSwitch();
handleButtonPress();
pedaloutPut();
timerOledclear();
}