#include <Arduino.h>
// GPIO Class for handling GPIO operations
class GPIO {
public:
GPIO(int pin) : pin(pin) {
pinMode(pin, OUTPUT);
}
void setHigh() {
digitalWrite(pin, HIGH);
}
void setLow() {
digitalWrite(pin, LOW);
}
protected:
int getPin() const {
return pin;
}
private:
int pin;
};
// LED Class inherited from GPIO
class LED : public GPIO {
public:
LED(int pin) : GPIO(pin) {}
void turnOn() {
setHigh();
}
void turnOff() {
setLow();
}
};
// RGB LED Class inherited from LED
class RGBLED : public LED {
public:
RGBLED(int redPin, int greenPin, int bluePin)
: LED(redPin), greenPin(greenPin), bluePin(bluePin) {}
void setColor(int red, int green, int blue) {
analogWrite(getPin(), red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
private:
int greenPin, bluePin;
};
// LED Bar Graph Class inherited from LED
class LEDBarGraph : public LED {
public:
LEDBarGraph(int anodePins[10]) : LED(anodePins[0]), anodePins(anodePins) {}
void lightUp(int numberOfLEDs) {
for (int i = 0; i < numberOfLEDs; ++i) {
// Turn on each LED in the bar
digitalWrite(anodePins[i], HIGH);
delay(50); // Adjust delay as needed
digitalWrite(anodePins[i], LOW);
}
}
private:
int* anodePins;
};
// Function to read potentiometer value
int readPotentiometer(int pin) {
return analogRead(pin);
}
// Declare LED, RGBLED, and LEDBarGraph objects outside setup and loop
LED led(32); // LED connected to GPIO pin 32
RGBLED rgbLED(23, 22, 21); // RGB LED connected to GPIO pins 23, 22, 21 (red, green, blue)
int barGraphAnodePins[] = {19, 18, 5, 4, 17, 16, 14, 12, 13, 24}; // LED bar graph anodes connected to GPIO pins
LEDBarGraph ledBarGraph(barGraphAnodePins);
void setup() {
int potentiometerPin = 27; // Potentiometer connected to GPIO pin 27
pinMode(potentiometerPin, INPUT);
// Set up additional pins for LED Bar Graph
for (int i = 0; i < 10; ++i) {
pinMode(barGraphAnodePins[i], OUTPUT);
digitalWrite(barGraphAnodePins[i], LOW);
}
}
void loop() {
// Read potentiometer value
int potValue = readPotentiometer(27); // Potentiometer connected to GPIO pin 27
// Update LED bar graph based on potentiometer value
int ledBarValue = map(potValue, 0, 4095, 0, 10); // Assuming 10 LEDs in the bar graph
ledBarGraph.lightUp(ledBarValue);
// Turn on the LED and RGB LED based on potentiometer value
int ledValue = map(potValue, 0, 4095, 0, 255);
led.turnOn();
rgbLED.setColor(ledValue, ledValue, ledValue);
delay(100); // Adjust delay as needed
}