Want to fade ledPin2 after ledPin1 ends

Hello,
I am attempting to write a welding torch simulator sketch for model railroading… I have the White (ledPin1) doing what I want but I’m stuck when I try to get my “glow” (ledPin2)to start out full on and fade down to 0 when ledPin1 finishes flashing… I want it to appear as if the ledPin2 is the metal glowing and cooling after the torch stops.

int ledPin =  2;    // LED connected to digital pin 2.. Torch
int ledPin2 = 3;  // LED connected to digital Pin 3.. Glow
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
void setup()   {
  pinMode(ledPin, OUTPUT);
  pinMode(ledPin2, OUTPUT);
}
void loop()
{

  int i, count;  count = random(10, 190);
  for (i = 0; i < count; i++) {
    digitalWrite(ledPin, HIGH);   // set the ledPin 1 LED on
    delay(random(20));
    //digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin, LOW);    // set the LED off
    delay(random(55));
  }

  {
    analogWrite(ledPin2, brightness); // Set the ledPin2 LED to full on
    brightness = brightness + fadeAmount;

   // reverse the direction of the fading at the ends of the fade:
   if (brightness == 5 || brightness == 255) {
     fadeAmount = -fadeAmount ;
   }
   // wait for 30 milliseconds to see the dimming effect
   delay(30);

    digitalWrite(ledPin2, HIGH);   // set the LED on
    delay(random(520, 2000));
    digitalWrite(ledPin2, LOW);    // set the LED off
    delay(random(1000, 5000));

  }
}

Welder_Simulator_with_glow_With_Dimming.ino (1022 Bytes)

Perhaps because you have no dimming code? Just use a similar method to what you used to randomly dim/bright the torch, but change the random function to a decreasing ramp. Use analogWrite().

Ok..

Another hint please...

Where in the code should I insert it? After the digitalWrite(ledPin, LOW)?

int ledPin =  2;    // LED for "welders Torch"
int ledPin1 = 3;    // LED for "Hot Metal"
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int i = 0;  // set i to 0
void setup()   {
  pinMode(ledPin, OUTPUT);  //sets pin to OUTPUT
  pinMode(ledPin1, OUTPUT); //sets pin to OUTPUT
}
void loop()
{
// Set up welders torch strobing
  int i, count;  count = random(10, 190); 
  for (i = 0; i < count; i++) {
    digitalWrite(ledPin, HIGH);   // set the LED on
    delay(random(20));
    digitalWrite(ledPin, LOW);    // set the LED off
    delay(random(55));
  }


  {
    // Set up Hot Metal Effect (fade)
    int i, count; count = random(10, 255);

    for (i = 0; i < count; i--) {
      analogWrite(ledPin1, i); {
        brightness = brightness - fadeAmount;
        if (brightness == 255 || brightness == 120)
          fadeAmount = -fadeAmount;
        delay(10);
      }

    }
  }
}

one of these days I'll post correctly :frowning:

I added my fading code and it works as written.. BUT (you knew that was coming) now I get the torch doing its strobing, then it stops as written, the fading starts and goes to low and it keeps repeating the fade down..

What am I missing here????

I noticed if I let the sketch run for a while (5-10 minutes) the torch part of the loop will run again for one loop then the fade keeps looping for another 5 or so minutes…

I have searched for my mistake but my limited knowledge is not finding it…

Please tell me where I messed up… I’m so close !!

I GOT IT… at least it seems to work…
Any suggestions as to cleaning it up or streamlining it?

Thanks for the guidance on the fade down…

int ledPin =  2;    // LED for "welders Torch"
int ledPin1 = 3;    // LED for "Hot Metal"
int brightness = 0;    // how bright the LED is
int fadeAmount = 5;    // how many points to fade the LED by
int j = 0;  // set i to 0
void setup()   {
  pinMode(ledPin, OUTPUT);  //sets pin to OUTPUT
  pinMode(ledPin1, OUTPUT); //sets pin to OUTPUT
}
void loop()
{
  // Set up welders torch strobing
  int i, count;  count = random(10, 190);
  for (i = 0; i < count; i++) {
    digitalWrite(ledPin, HIGH);   // set the LED on
    delay(random(20));
    digitalWrite(ledPin, LOW);    // set the LED off
    delay(random(55));
  }


  {
    // Set up Hot Metal Effect (fade)
    int j = 0;
    {
      {
        for (j = 255; j >= 0; j-=5) {
          analogWrite(ledPin1, j);
          brightness = brightness - fadeAmount;
          if (brightness == 0 || brightness == 255)
            fadeAmount = -150;//fadeAmount;
          delay(20);
        }
        analogWrite(ledPin1, LOW);
        delay(random(1000, 5550));
      }

    }
  }
}