Random pulse stops

Hi All ,
Looking for some help with my code please .

What I Want It To Do :
Nano powers up
4 LEDs fade in
1 red LED stays at full brightness for 2 seconds
3 LEDs pulse dim to bright randomly for 2 seconds after fade in
All LEDs fade out
A blue LED fades in
Blue LED stays at full brightness for 2 seconds
Blue LED fades out
Sketch loops again until power is turned off

What It Does :
Nano powers up
4 LEDs fade in
1 red LED stays at full brightness for 2 seconds

*Here is the problem *
The 3 LEDs pulse randomly until they reach full brightness then they stay on for 2 seconds instead of pulsing dim to bright randomly for 2 seconds after fade in

Everything after the above step does what is expected
All LEDs fade out
A blue LED fades in
Blue LED stays at full brightness for 2 seconds
Blue LED fades to black
Code loops again until power is turned off

I think the problem is the “ if ( ledLevel == 255 ) “ but I don’t know what to do to separate the pulsing LEDs from the static blue and red LEDs .
Code attached below , thanks for reading ,
Tom

[code]
// Hot / Cold try continuous LED pulsing during HOT state

enum
{
  FADE_IN_HOT,
  WAIT_HOT ,
  FADE_OUT_HOT,

  FADE_IN_COOL,
  WAIT_COOL ,
  FADE_OUT_COOL,

} state;

unsigned long previousMillis;
unsigned long currentMillis;
unsigned long fadeInTime = 15;
unsigned long fadeOutTime = 15;
unsigned long waitHot = 2000;
unsigned long waitCool = 2000;
unsigned int ledLevel = 0;

const int ember1Pin = 11;
const int ember2Pin = 10;
const int ember3Pin = 9;
const int redPin = 5;
const int bluePin = 3;

void setup() {
  // put your setup code here, to run once:
  pinMode( ember1Pin, OUTPUT);
  pinMode( ember2Pin, OUTPUT);
  pinMode( ember3Pin, OUTPUT);
  pinMode( redPin, OUTPUT);
  pinMode( bluePin, OUTPUT);
}

void loop() {

  unsigned long currentMillis = millis();

  switch (state)
  {
    case FADE_IN_HOT:
      if (currentMillis - previousMillis >= fadeInTime)//time to change LED level
      {
        ledLevel++;
        analogWrite(redPin, ledLevel);
        analogWrite(ember1Pin, random(120) + 135);
        analogWrite(ember2Pin, random(120) + 135);
        analogWrite(ember3Pin, random(120) + 135);

        if (ledLevel == 255)
        {
          state = WAIT_HOT;
        }
        previousMillis = currentMillis;
      }
      break;

    case WAIT_HOT:
      if (currentMillis - previousMillis >= waitHot)
      {
        state = FADE_OUT_HOT;
        previousMillis = currentMillis;
      }
      break;

    case FADE_OUT_HOT:
      if (currentMillis - previousMillis >= fadeOutTime)//time to change LED level
      {
        ledLevel--;
        analogWrite(redPin, ledLevel);
        analogWrite(ember1Pin,ledLevel);
        analogWrite(ember2Pin, ledLevel);
        analogWrite(ember3Pin, ledLevel);
        if (ledLevel == 0)
        {
          state = FADE_IN_COOL;
        }
        previousMillis = currentMillis;
      }
      break ;

    case FADE_IN_COOL:
      if (currentMillis - previousMillis >= fadeInTime)//time to change LED level
      {
        ledLevel++;
        analogWrite(bluePin, ledLevel);

        if (ledLevel == 255)
        {
          state = WAIT_COOL;
        }
        previousMillis = currentMillis;
      }
      break;

       case WAIT_COOL:
      if (currentMillis - previousMillis >= waitCool)
      {
        state = FADE_OUT_COOL;
        previousMillis = currentMillis;
      }
      break;

    case FADE_OUT_COOL:
      if (currentMillis - previousMillis >= fadeOutTime)//time to change LED level
      {
        ledLevel--;
        analogWrite(bluePin, ledLevel);
        if (ledLevel == 0)
        {
          state = FADE_IN_HOT;
        }
        previousMillis = currentMillis;
      }
      break ;
  }
}

[/code]

Hi!

The code looks like made by a person having a bit of knowledge.

However...."LEDs" nowdays can be everything from one little 20 mA LED to 100 feet of LED strip consuming plenty of Amps.

Please post a wiring diagram, not any Fritzing wall picture, and links to the LEDs.

All the LEDs are set to a certain brightness all at the same time, I do not see fading, definitely not independent fading of the LEDs.

The solution probably lies into creating an array for the states and millis values, so you can control all LEDs independently. That independent control in turn can of course be linked to each other... i.e. you can give one LED the status to fade in as you give another the status fade out.

Hi ,
Railroader - The code is a combination of sketches I have found online and info I have received from forum members in the past . Arduino is still new and a mystery to me that gets a little less confusing with each project I attempt .
I will look for software to create the wiring diagram , or will a hand drawn sketch work ?
The LEDs are 5 single 3mm 20ma .

Wvmarle - I haven’t used arrays before , I will look at some examples and do some reading .

Thanks .

Fine, only "small", single LEDs. Each LED has a serial resistor I hope.
It's fundamental tofor me to make sure the powering is good. If it wouln't be, any undesired phenomena might take place.
Using pen and paper for wiring is good enough.

For basic schematics, pen and paper is fine indeed.

Recommended software: KiCAD and EagleCAD.

Or online: try EasyEDA, pretty popular nowadays and gives pretty good schematics as well.

How to make a schematic you can post

OK , first time using EasyEDA , drawing a schematic and attaching a picture to a reply . Fingers crossed it goes smoothly . It’s not the best looking schematic but I think it has the info you have requested .

Image looks blurred , probably because I saved it as a png ?
Back to reading and looking at examples of arrays . Still trying to solve my original problem in first post .

Great work! No problem to read the wiring.
Powered by USB also sounds good.

*Here is the problem *
The 3 LEDs pulse randomly until they reach full brightness then they stay on for 2 seconds instead of pulsing dim to bright randomly for 2 seconds after fade in

Which 3 LEDs? There are 5 LEDs.

Use Serial.print, temporarily, in the code. Print out "strategic" data, variables. Serial Monitor in the Pc....

Install Serial.begin(115200); in Setup(). Set the baud rate for Serial Monitor to 115200.

Dry debugging timing remotely is very difficult. The best debugging is done by You.

Thanks , glad the schematic wiring is easy to follow and shows the info needed .
Reading through my initial post I see I wasn’t very clear about which LEDs were the problem . The three ember LEDs
( ember1Pin , ember2Pin and ember3Pin ) need to do the pulse effect until the WAIT_HOT state is finished .
I’ll add the Serial.print to the code and post the updated code when finished .

Yes. Use Serial.print and monitor. That saved med during many years when I was thrown into new, large, unknown programs.

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