Fade up to flicker effect

I'm rather new to arduinos and am having trouble wrapping my head around the fade codes.

Basically what I'm trying to do is create a simulated trashcan fire using MOSFETs and 12v lights. I'd like for the lights to fade up on a button push, and once they get to a certain level to just remain on and flicker. I've got the flicker portion down from some candles for a previous project. Here's the code I'm working with.

(NOTE: If you know of a better way of getting this effect I'm open to suggestions, but it HAS to fade up on a button push and stay on)

const int buttonPin = 2;  
int LED_Pin5 = 5;  // ATTiny44 PWM Pin 8
int LED_Pin6 = 9;  // ATTiny44 PWM Pin 5
int LED_Pin7 = 10;  // ATTiny44 PWM Pin 6
int LED_Pin8 = 6;  // ATTiny44 PWM Pin 7


int howBright5;   // Intensity of the LED: [0 = Off], [255 = Brightly Lit] 
int howBright8; 
int howBright7;
int howBright6;
int buttonState = 0; 

void setup()
{ 
  pinMode(LED_Pin5, OUTPUT);  // ATTiny44 PWM Pin 8
  pinMode(LED_Pin8, OUTPUT);  // ATTiny44 PWM Pin 5
  pinMode(LED_Pin7, OUTPUT);  // ATTiny44 PWM Pin 6
  pinMode(LED_Pin6, OUTPUT);  // ATTiny44 PWM Pin 7
       
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}
void loop() 
{ 
   buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {     
    // turn LED on:    
    digitalWrite(LED_Pin5, LOW);
    digitalWrite(LED_Pin6, LOW);
    digitalWrite(LED_Pin7, LOW);
    digitalWrite(LED_Pin8, LOW);
  } 
  else {
    // turn LED off:
    
  howBright5 = random(1,25);     // Change brightness to something between 1 and 255 
  howBright8 = random(1,35);
  howBright7 = random(1,55);
  howBright6 = random(1,65);
 
  analogWrite(LED_Pin5, howBright5); // Illuminate the LED with the brightness picked 
  delay(random(1,14));           // Makes LED seem to flicker when on for a random time 
  analogWrite(LED_Pin8, howBright8); // Illuminate the LED with the brightness picked 
  delay(random(1,25));           // Makes LED seem to flicker when on for a random time 
  analogWrite(LED_Pin7, howBright7); // Illuminate the LED with the brightness picked 
  delay(random(1,36));           // Makes LED seem to flicker when on for a random time 
  analogWrite(LED_Pin6, howBright6); // Illuminate the LED with the brightness picked 
  delay(random(1,47));           // Makes LED seem to flicker when on for a random time 
  } 
}

You’re using delay() between your different brightness levels.

delay() is expressed in milliseconds, which are thousandths of a second. You can’t see much faster than 1/10th of a second or it looks like flickering. Plus, the Arduino simulates analog output using pulse width modulation at somewhere around 500 Hz (1 pulse every 2 MS.) Try increasing your delay values by a factor of 100. (delays of 1/10 of a second (100 milliseconds) or more.)

If you want your brightness to smoothly change from one brightness to the next, you need to do math to calculate the difference between the starting and ending brightness, decide how long your transition will be, and how many steps you’re going to use. Calculate how much to change the brightness values between steps.

I would suggest you add a variable currentBrightness.

You might use a method like this: (Code has not even been compiled, much less tested. It’s provided as a guide, not for copy/pasting. You’ll need to debug it and make it work with your program.

void setNewBrightness(int LED_pin, int newBrightness, int steps)
{
  int tweenBrightness;
  int change = newBrightness - currentBrightness;
  float stepChange = change/steps;
  for (int step = 0; step< steps; step++)
  {
    tweenBrightness = currentBrightness + stepChange*step;
    analogWrite(LED_Pin, tweenBrightness); // 
    delay(50);
  }
  analogWrite(LED_Pin, newBrightness); // 
  currentBrightness = newBrightness;
}

The code above uses 50 MS/step. If you want a change time of 1/2 second, use 10 steps. That should give pretty smooth changes.

Work that code (or something similar) into your program.

ScottKouta:
I’m rather new to arduinos and am having trouble wrapping my head around the fade codes.

Basically what I’m trying to do is create a simulated trashcan fire using MOSFETs and 12v lights. I’d like for the lights to fade up on a button push, and once they get to a certain level to just remain on and flicker. I’ve got the flicker portion down from some candles for a previous project. Here’s the code I’m working with.

(NOTE: If you know of a better way of getting this effect I’m open to suggestions, but it HAS to fade up on a button push and stay on)

const int buttonPin = 2;  

int LED_Pin5 = 5;  // ATTiny44 PWM Pin 8
int LED_Pin6 = 9;  // ATTiny44 PWM Pin 5
int LED_Pin7 = 10;  // ATTiny44 PWM Pin 6
int LED_Pin8 = 6;  // ATTiny44 PWM Pin 7

int howBright5;   // Intensity of the LED: [0 = Off], [255 = Brightly Lit]
int howBright8;
int howBright7;
int howBright6;
int buttonState = 0;

void setup()
{
  pinMode(LED_Pin5, OUTPUT);  // ATTiny44 PWM Pin 8
  pinMode(LED_Pin8, OUTPUT);  // ATTiny44 PWM Pin 5
  pinMode(LED_Pin7, OUTPUT);  // ATTiny44 PWM Pin 6
  pinMode(LED_Pin6, OUTPUT);  // ATTiny44 PWM Pin 7
       
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);     
}
void loop()
{
   buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {     
    // turn LED on:   
    digitalWrite(LED_Pin5, LOW);
    digitalWrite(LED_Pin6, LOW);
    digitalWrite(LED_Pin7, LOW);
    digitalWrite(LED_Pin8, LOW);
  }
  else {
    // turn LED off:
   
  howBright5 = random(1,25);     // Change brightness to something between 1 and 255
  howBright8 = random(1,35);
  howBright7 = random(1,55);
  howBright6 = random(1,65);

analogWrite(LED_Pin5, howBright5); // Illuminate the LED with the brightness picked
  delay(random(1,14));           // Makes LED seem to flicker when on for a random time
  analogWrite(LED_Pin8, howBright8); // Illuminate the LED with the brightness picked
  delay(random(1,25));           // Makes LED seem to flicker when on for a random time
  analogWrite(LED_Pin7, howBright7); // Illuminate the LED with the brightness picked
  delay(random(1,36));           // Makes LED seem to flicker when on for a random time
  analogWrite(LED_Pin6, howBright6); // Illuminate the LED with the brightness picked
  delay(random(1,47));           // Makes LED seem to flicker when on for a random time
  }
}

For the time being I don’t want to start messing with my working code, just adding onto it the fade portion. I’ve got 4 MR16 lamps hooked up to it, so the PWM steps aren’t really that noticable. I will take your advice on future LED candles though.