#include <Arduino.h>
// Pin Definitions
const uint8_t RedPIN = 15;
const uint8_t GreenPIN = 13;
const uint8_t YellowPIN = 12;
const uint8_t BluePIN = 14;
// Brightness Values
int RedValue = 0;
int GreenValue = 0;
int YellowValue = 0;
int BlueValue = 0;
// Calibration Factors (based on efficiency, range: 0.0 to 1.0)
const float RedCalibration = 0.8; // Reduce brightness for Red
const float GreenCalibration = 1.0; // Green is at ideal efficiency
const float YellowCalibration = 0.9; // Slightly reduce brightness for Yellow
const float BlueCalibration = 0.7; // Reduce brightness for Blue
// Timing Variables
unsigned long previousMillis = 0;
const int interval = 30; // Time between brightness updates (ms)
// Mode and States
volatile int currentMode = 0; // The current mode (0, 1, etc.)
bool RedIncreasing = true; // True if fading in, false if fading out
bool GreenIncreasing = true;
bool YellowIncreasing = true;
bool BlueIncreasing = true;
void setup() {
// Pin Modes
pinMode(RedPIN, OUTPUT);
pinMode(GreenPIN, OUTPUT);
pinMode(YellowPIN, OUTPUT);
pinMode(BluePIN, OUTPUT);
// Interrupt for Mode Switching
attachInterrupt(digitalPinToInterrupt(2), changeMode, FALLING);
}
void loop() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
switch (currentMode) {
case 0:
// Mode 0: Fade Red in and out
updateBrightness(RedPIN, RedValue, RedIncreasing, RedCalibration);
break;
case 1:
// Mode 1: Fade Blue and Green alternately
updateBrightness(BluePIN, BlueValue, BlueIncreasing, BlueCalibration);
updateBrightness(GreenPIN, GreenValue, GreenIncreasing, GreenCalibration);
break;
case 2:
// Mode 2: Fade all LEDs together
updateBrightness(RedPIN, RedValue, RedIncreasing, RedCalibration);
updateBrightness(GreenPIN, GreenValue, GreenIncreasing, GreenCalibration);
updateBrightness(YellowPIN, YellowValue, YellowIncreasing, YellowCalibration);
updateBrightness(BluePIN, BlueValue, BlueIncreasing, BlueCalibration);
break;
default:
// Default: Turn all LEDs off
analogWrite(RedPIN, 0);
analogWrite(GreenPIN, 0);
analogWrite(YellowPIN, 0);
analogWrite(BluePIN, 0);
break;
}
}
}
void updateBrightness(uint8_t pin, int &value, bool &increasing, float calibration) {
if (increasing) {
value++;
if (value >= 255) increasing = false; // Reverse direction
} else {
value--;
if (value <= 0) increasing = true; // Reverse direction
}
// Apply calibration factor
int calibratedValue = value * calibration;
analogWrite(pin, calibratedValue);
}
void changeMode() {
currentMode = (currentMode + 1) % 3; // Cycle through modes (0, 1, 2)
}