Hey,
I'm very sorry if this is a stupid or already asked question. Before someone drops a lmgtfy link, I DID search for it but was not able to find it.
For our project, we have multiple buttons (9 to be exact) that trigger their own cluster of LEDs (but testing the script now we only have 1 LED per button, so 9 LEDs). There are no double triggers, so when button 1 is pressed, only LED 1 will go on. Also, when released, the LEDs fade out.
We finally were able to script the first part correctly, button 1 triggering LED 1, and we tried expanding it to more LEDs, but when you press button 1, LED 1 goes on, and when you press button 2 when LED 1 is STILL on, LED 2 won't turn on until LED 1 has faded out, but we need the LEDs to be turned on at the same time!
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 11; // the number of the LED pin
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
int brightness = 0; // how bright the LED is
const int buttonPin2 = 4; // the number of the pushbutton pin
const int ledPin2 = 10; // the number of the LED pin
// variables will change:
int buttonState2 = 0; // variable for reading the pushbutton status
int brightness2 = 0; // how bright the LED is
int fadeAmount = 5;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
// initialize the LED pin as an output:
pinMode(ledPin2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
brightness = 255;
analogWrite(11, 255);
digitalWrite(ledPin, HIGH);
}
else{
while (brightness >= 0 && buttonState != HIGH) {
analogWrite(11, brightness);
brightness = brightness - fadeAmount;
delay(70);
buttonState = digitalRead(buttonPin);
}
}
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState2 == HIGH) {
// turn LED on:
brightness2 = 255;
analogWrite(10, 255);
digitalWrite(ledPin2, HIGH);
}
else{
while (brightness2 >= 0 && buttonState2 != HIGH) {
analogWrite(10, brightness2);
brightness2 = brightness2 - fadeAmount;
delay(70);
buttonState2 = digitalRead(buttonPin2);
}
}
}
Here's the code we used. Input 11 and 2 = LED 1 and button 1, input 10 and 4 = LED 2 and button 2
We're completely new to Arduino and some things we don't yet understand... please help us...