Could someone please help me fix my LED code?

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.

not sure this is what you mean, but in the code above, front, back and bottom are each sequentially turned-on.

if you want them all to be simultaneously turn-ed on there could be one loop that increases the level and sets the output if that level has not been reached

possibly


void fade ()
{
    for (int i = 0; i <= 255; i += 5)  {
        if (i < frontBrightness)
            analogWrite(ledPinFront, i);

        if (i < backBrightness)
            analogWrite(ledPinBack, i);

        if (i < bottomBrightness)
            analogWrite(ledPinBottom, i);

        delay(30);
    }
}

That was extremely helpful thanks a lot! I have another problem however… I can’t control the pot because the function isn’t in the for loop. How would I do it?

put it in the loop

void fade ()
{
    for (int i = 0; i <= 255; i += 5)  {
        int frontBrightness = map (analogRead(ledFrontControl), 0, 1023, 0, 255);
        if (i < frontBrightness)
            analogWrite(ledPinFront, i);

        int backBrightness = map (analogRead(ledBackControl), 0, 1023, 0, 255);
        if (i < backBrightness)
            analogWrite(ledPinBack, i);

        int bottomBrightness = map (analogRead(ledBottomControl), 0, 1023, 0, 255);
        if (i < bottomBrightness)
            analogWrite(ledPinBottom, i);

        delay(30);
    }
}

I am a little bit confused about this line of code:

    for (int i = 0; i <= 255; i += 5)  {

When i = 255 wouldnt it just keep gaining? Because you wrote if i is greater than or equal to 255, i += 5 but its gonna keep adding up when its past 255??

You know, neoPixels, although probably more expensive. Would probably be a lot easier to do. And you could do more with them. Patterns n such.

-jim lee

No

What was written, expressed in English, is while i is smaller than or equal to 255, add 5 to i

Ohhhhh that makes more sense now, thanks!

I have used them before on my RC car project and it works really well it’s just that my bed is a king size and I would require like 100+ neo pixels but the wiring would look amazing. Thanks for the suggestion tho

How would I make the fade out? It’s a lil confusing to me

Running a bunch of 12V LEDs?

I'd start with one of these : Adafruit DRV8871 DC Motor Driver Breakout Board - 3.6A Max : ID 3190 : $7.50 : Adafruit Industries, Unique & fun DIY electronics and kits
Per strip. These let you drive a 12V load using one IO pin. Get one of these per strip.

Then the code? Just a variable duty cycle mapped to the POT value. Like this : variable dutycycle. - Wokwi ESP32, STM32, Arduino Simulator

-jim lee

do above in reverse, reduce the value from 255 to zero

I’m guessing I also have to change the += 5 to -=5 right?

and reverse the end points

    for (int i = 255; i > 0; i -= 5)

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.