/* -------------------------------------------------------------
candle_array_random lights an LED connected to pin 6
-Uses Pulse-Width modulation and an array
--------------------------------------------------------------*/
int buttonState = 0; // create variable for storing "state" of the button
int buttonPin = 5; // create variable for button pin number
int LEDpin = 6;
int flicker_value[]={62,15,25,60,100,85,20,180}; // 8 item array
void setup()
{
pinMode(LEDpin, OUTPUT); // initialize the digital pin as an output (for LED.)
pinMode(buttonPin, INPUT_PULLUP); // initialize the digital pin as an input (for switch.)
}
void loop() { // repeat forever
buttonState = digitalRead(buttonPin); // "Read" voltage (check switch.)
// If low, the switch is closed
if (buttonState == LOW) {
blow();
} else {
no_blow();
}
}
void blow() {
int r = random(0, 8);
analogWrite(LEDpin, flicker_value[r]); // PWM (brightness) value from flicker_value array
delay(r * 10);
} // end blow function
void no_blow() {
int r = random(100, 255);
analogWrite(LEDpin, r); // PWM for random brightness in 100-255 range
delay(random(200,350)); // delay for random duration
}
|