This is my first program so please, be gentle, my goal is to set up the code so that once I press the button, it glows at full brightness, then fades out after I let go, this is just a test code until I feel confident enough to move on to my real project, which is to wire my arduino Uno to the sound system of my car so that a set of LED lights pulse along with the bass of the music, I plan to use the same basic principle as with this code, but replace the button input with a parallel line running from the subwoofers so that the electrical impulse sets off the arduino lighting the LEDs so that the bass hits light the LEDs and then fade off smoothly, so if you have any input on that as well that would be fantastic, I'll post what I have so far, but in all honesty its really just a copy and paste scrap book from the reference section of the website. Right now with this code, my Arduino fades continuously from high to low when the button is not pressed and glows solid when it is pressed, any help you could offer would be immensly appreciated!
const int buttonPin = 2;
const int ledPin = 9;
int buttonState = 0;
int led = 9;
int brightness = 0;
int fadeAmount = 5;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
analogWrite(led, brightness);
brightness = brightness - fadeAmount;
if (brightness == 0) {
fadeAmount = -fadeAmount ;
}
delay(10);
}
}