I'm learning to code but have been hitting a lot of walls trying to do this due to being a complete novice. I've got 2 leds connected to a button switch and have them set to simply turn on and off with a press of the button. What I really want to do is have them flicker on, then run a continual more subtle flicker, and second button press have it fade to off instead of immediate off. Then repeat on next button press. Flicker on, fade off, etc. I haven't been able to insert the functions in a way that actually makes this happen. Am I going about this entirely the wrong way?
int button = 4; // switch is connected to pin 4
int led1 = 5; // Green LED pin 5
int led2 = 6; // Green LED pin 6
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
int buttonPresses = 0; // how many times the button has been pressed
int lightMode = 0; // What mode is the light in?
// button switch here
int state = LOW; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = HIGH; // the previous reading from the input pin
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
// base flicker here
int pulseMin = 1; // Minimum number of pulses in each set
int pulseMax = 3; // Maximum number of pulses in each set
int brightMin = 106; // Baseline LED brightness. Cannot exceed 128.
int minDelay = 2500; // Minimum delay between pulse sets (ms)
int maxDelay = 7000; // Maximum delay between pulse sets (ms)
// fade out here
int fade1 = 2;
void setup() {
randomSeed (analogRead (3)); // Randomise
pinMode(button, INPUT); // Sets button to be an input
pinMode(led1, OUTPUT); // Sets LED Pin to be an output
pinMode(led2, OUTPUT);
}
void loop()
{
// For on and off button
reading = digitalRead(button);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == LOW && previous == HIGH && millis() - time > debounce) {
if (state == LOW){
state = HIGH;
powerUp();
}else{
state = LOW;
fadeOff();
}
time = millis();
}
if(state = HIGH){
baseFlicker();
}
previous = reading;
}
// Function for power-on flicker
void powerUp() {
analogWrite(led1, 50);
analogWrite(led2, 50);
delay(50);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(20);
analogWrite(led1, 80);
analogWrite(led2, 80);
delay(70);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(40);
analogWrite(led1, 120);
analogWrite(led2, 120);
delay(90);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(60);
analogWrite(led1, 140);
analogWrite(led2, 140);
delay(110);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
delay(00);
}
// Function to make LED flicker continually while on
void baseFlicker() {
// For loop sets the number of pulses and repeats these pulses
for (int x = random(pulseMin, pulseMax); x > 0; x-- ) {
int bright = 224 - random(96); // Sets a random maximum brightness level for this pulse
// For loop raises the brightness of the LED from minimum to maximum value
for (int y = brightMin; y < bright ; y++) {
analogWrite(led1, y);
analogWrite(led2, y);
delay(3);
}
// For loop lowers the brightness of the LED from maximum to minimum value
for (int y = bright; y > brightMin; y--) {
analogWrite(led1, y);
analogWrite(led2, y);
delay(3);
}
delay(10); // Adds a delay between pulses to make them more visible
}
analogWrite(led1, brightMin);
analogWrite(led2, brightMin);
delay(random(minDelay,maxDelay)); // Adds a delay between pulse sets
}
// Function to fade to off
void fadeOff () {
static int lastBright1 = 0; // set the lastbright variable
if (state < HIGH) { // check if LED is on
analogWrite(led1, LOW); // turn the led off
analogWrite(led2, LOW); // turn the led off
if (lastBright1 > 0) // check to see if the last brightness is more than 0
lastBright1 = lastBright1 * fade1; // Dim the light til it reaches 0
analogWrite(led1, lastBright1); // write the brightness to the led
analogWrite(led2, lastBright1); // write the brightness to the led
}
}
Thanks in advance for any help/advice.