hey guys im trying to set up 5 leds that come on for 5 different amount of time when a psuh button is pressed. i manged to get one working but having problem writing the code for mutiple leds.
(also with new code would this work leds simultaniously or one at a time, hopeing all togther)
im sorry for the stupid question
thanks
#define LED 12
#define BUTTON 7
void setup()
{
pinMode(LED, OUTPUT); // output for the LED
pinMode(BUTTON, INPUT); // input for the button
}
void loop()
{
if ( digitalRead(BUTTON) == HIGH )
{
digitalWrite(LED, HIGH); // turn on the LED
delay(500); // wait for 0.5 seconds
digitalWrite(LED, LOW); // turn off the LED
}
}
My attempt at making it for multiple Leds(
#define LED8
#define LED9
#define LED10
#define LED11
#define BUTTON 7
void setup()
{
pinMode(LED8, OUTPUT);
pinMode(LED9, OUTPUT);// output for the LED
pinMode(LED10, OUTPUT);
pinMode(LED11, OUTPUT);
pinMode(BUTTON, INPUT); // input for the button
}
void loop()
{
if ( digitalRead(BUTTON) == HIGH )
{
digitalWrite(LED8, HIGH); // turn on the LED
delay(1000); // wait for 0.5 seconds
digitalWrite(LED8, LOW); // turn off the LED
}
{
digitalWrite(LED9, HIGH); // turn on the LED
delay(1000); // wait for 0.5 seconds
digitalWrite(LED9, LOW); // turn off the LED
}
{
digitalWrite(LED10, HIGH); // turn on the LED
delay(1000); // wait for 0.5 seconds
digitalWrite(LED10, LOW); // turn off the LED
}
{
digitalWrite(LED11, HIGH); // turn on the LED
delay(1000); // wait for 0.5 seconds
digitalWrite(LED11, LOW) // turn off the LED
}
}
{
digitalWrite(LED8, HIGH); // turn on the LED
delay(1000); // wait for 0.5 seconds
digitalWrite(LED8, LOW); // turn off the LED
}
The last } in above closes the block that depends on the state of the button. Anything after that } will not depend on the state of the button and will be executed unconditionally.
Im new to arduino so im still learning, ive looked everywhere for a tutorial
and no delta g i dont want them all going off at same time, im trying to get them to all stay on for individual time delays.
I was going to describe how to do it but in the end wrote an example
const byte buttonPin = A1;
const byte ledPins[] = {3, 5, 6, 9};
unsigned long periods[] = {1000, 1500, 2000, 5000};
unsigned long startTime;
unsigned long currentTime;
boolean running = false;
void setup()
{
Serial.begin(115200);
pinMode(buttonPin, INPUT_PULLUP);
for (int pin = 0; pin < 4; pin++) //set pinMode()s for the LEDs
{
pinMode(ledPins[pin], OUTPUT);
digitalWrite(ledPins[pin], HIGH); //turn off leds at the start
}
}
void loop()
{
if (!running) //read the input only if not already running the sequence
{
if (digitalRead(buttonPin) == LOW) //button pressed
{
for (int pin = 0; pin < 4; pin++)
{
digitalWrite(ledPins[pin], LOW); //turn on all the leds
}
startTime = millis(); //save the time we started
running = true; //flag that the sequence is running
}
}
else //actions to take only when the sequence is running
{
currentTime = millis();
for (int led = 0; led < 4; led++) //test whether each led has been on long enough
{
if (currentTime - startTime >= periods[led])
{
digitalWrite(ledPins[led], HIGH); //turn off this led when period has been reached
if (led == 3) //if this is the final led
{
running = false; //change state so that we can start again when the button is pressed
}
}
}
}
}
Note that the example uses 4 LEDs and that the logic may be reversed to that of your system so will need changing
Using arrays allows the code to be shorter and using millis() for timing allows the loop() function to keep running freely with no blocking such as would be caused by using delay()