This project is to control 3 different LED strips on my bed with 3 different pots and 3 different MOSFETs. Later on, I will integrate an OLED display on the Arduino so it looks more fancy. For now, the issue I am facing is the fact that I can't seem to figure out how to turn them all on at once. They go one by one. The reason that I am keeping all the leds separate (void fadeINfront) is so I can control them individually using the pots. That's the 2nd issue, when all 3 strips fully fade in for some reason I can't adjust the brightness at all. I know why I just don't know how to fix it. Here is my code:
#include <CapacitiveSensor.h>
CapacitiveSensor cs_11_12 = CapacitiveSensor(11, 12);
const int ledPinFront = 5;
const int ledFrontControl = A1;
const int ledPinBack = 3;
const int ledBackControl = A2;
const int ledPinBottom = 6;
const int ledBottomControl = A3;
int ledState = 0; // 0: Off, 1: Fade In, 2: Fade Out
unsigned long lastTouchTime = 0;
unsigned long lightsOffTime = 0;
const unsigned long debounceDelay = 1000;
const unsigned long lightsTimeout = 100000; // 10 minutes in milliseconds
void setup() {
pinMode(ledPinFront, OUTPUT);
pinMode(ledPinBack, OUTPUT);
pinMode(ledPinBottom, OUTPUT);
}
void fadeINfront(int frontBrightness) {
for (int i = 0; i <= frontBrightness; i += 5) {
analogWrite(ledPinFront, i);
delay(30); // Adjust the delay for the fading speed
}
}
void fadeINback(int backBrightness) {
for (int i = 0; i <= backBrightness; i += 5) {
analogWrite(ledPinBack, i);
delay(30); // Adjust the delay for the fading speed
}
}
void fadeINbottom(int bottomBrightness) {
for (int i = 0; i <= bottomBrightness; i += 5) {
analogWrite(ledPinBottom, i);
delay(30); // Adjust the delay for the fading speed
}
}
void fadeOUTfront(int frontBrightness) {
for (int i = frontBrightness; i >= 0; i -= 5) {
analogWrite(ledPinFront, i);
delay(30); // Adjust the delay for the fading speed
}
}
void fadeOUTback(int backBrightness) {
for (int i = backBrightness; i >= 0; i -= 5) {
analogWrite(ledPinBack, i);
delay(30); // Adjust the delay for the fading speed
}
}
void fadeOUTbottom(int bottomBrightness) {
for (int i = bottomBrightness; i >= 0; i -= 5) {
analogWrite(ledPinBottom, i);
delay(30); // Adjust the delay for the fading speed
}
}
void loop() {
int frontValue = analogRead(ledFrontControl);
int backValue = analogRead(ledBackControl);
int bottomValue = analogRead(ledBottomControl);
int frontBrightness = map(frontValue, 0, 1023, 0, 255);
int backBrightness = map(backValue, 0, 1023, 0, 255);
int bottomBrightness = map(bottomValue, 0, 1023, 0, 255);
long touchValue = cs_11_12.capacitiveSensor(30);
unsigned long currentTime = millis();
// If touchValue is above the threshold and debounce delay has passed, toggle the LED state
if (touchValue > 1000 && (currentTime - lastTouchTime) > debounceDelay) {
ledState++;
if (ledState == 1) {
fadeINfront(frontBrightness);
fadeINback(backBrightness);
fadeINbottom(bottomBrightness);
} else if (ledState == 2) {
fadeOUTfront(frontBrightness);
fadeOUTback(backBrightness);
fadeOUTbottom(bottomBrightness);
ledState = 0; // Reset the state to 0 after fading out
}
lastTouchTime = currentTime;
lightsOffTime = currentTime + lightsTimeout; // Update the lights off time
}
// Automatically turn off the lights after 10 minutes
if (currentTime > lightsOffTime && ledState != 0) {
fadeOUTfront(frontBrightness);
fadeOUTback(backBrightness);
fadeOUTbottom(bottomBrightness);
ledState = 0; // Reset the state to 0 after fading out
}
}
Thanks in advance.