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
}
}