Hey everyone,
So this is a really simple idea, but I can't seem to get it just right. I'm attempting to make a program that will cause an LED to flicker using PWM, but I want it to flicker more intensely when a button is pressed. This is what I have so far. It does work, the only issue I'm having is a delay.
What it does is the LED will slicker dimly, but if I connect a wire between pin 6 and 12, it will flicker much brighter. The problem is that when I disconnect the wire between pin 6 and 12, it will still flicker brightly for a second or two. I want to make it so that once I disconnect it, it immediately goes back to the original dim flickering.
void setup(){
pinMode(5,OUTPUT);
pinMode(12,OUTPUT);
pinMode(6,INPUT);
digitalWrite(6,LOW);
digitalWrite(12,HIGH);
}
void test2(){ // function when pins not connected
analogWrite(5, (random(1,5)));
digitalWrite(6,LOW);
}
void test(){ // function when pins are connected
analogWrite(5, (random(100,150)));
digitalWrite(6,LOW);
}
void loop()
{
if (digitalRead(6) == LOW) // test condition
test2();
else // if condition isn't met
test();
delay(50);
}
Thank you for your help!