I AM A NEWBIE AND LOOKING FOR SOME ADVISE PLEASE
Looking to have 25 LED flash on/off one at a time on after one another starting with a push button
They then need to stop after a short delay (after the button is realised ) and one light is randomly selected
to sat lit until the button is pressed again
I have got the button working and light flashing in sequence
really struggling to get the off delay and the random light to selection
My current code attached (thanks to Mitesh Upda for assistance so far)
not use if this is best approach but started with an array for the 25 pins thought this would save coding later
can anyone help with this next stage or tell me if I have the wrong approach
Code here:
//LED Pin Variables
int ledPins[] = {22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46}; //An array to hold the pin each LED is connected to
//i.e. LED #0 is connected to pin 2, LED #1, 3 and so on
//to address an array use ledPins[0] this would equal 2
//and ledPins[7] would equal 9
int buttonPin = 11; // button pin variable, we will be using pin 11
int val = 0; // variable to read button pin value
void setup()
{
//Set each pin connected to an LED to output mode (pulling high (on) or low (off)
for(int i = 0; i < 25; i++){ //this is a loop and will repeat eight times
pinMode(ledPins*,OUTPUT); //we use this to set each LED pin to output*
-
} //the code this replaces is below*
-
pinMode(buttonPin, INPUT_PULLUP); // set button pin to be an input*
}
void loop( ) // run over and over again
*{ *
val = digitalRead(buttonPin);
*if(val == LOW) *
*oneOnAtATime(); *
}
//oneOnAtATime() - Will light one LED then the next turning off all the others
void oneOnAtATime(){
-
int delayTime = 300; //the time (in milliseconds) to pause between LEDs*
-
//make smaller for quicker switching and larger for slower*
-
for(int i = 0; i <= 25; i++){*
-
int offLED = i - 1; *
-
if(i == 0) { *
-
offLED = 25 ; *
-
} *
_ digitalWrite(ledPins*, HIGH); //turn on LED #i*_
* digitalWrite(ledPins[offLED], LOW); //turn off the LED we turned on last time*
* delay(delayTime);*
* }*
}
sketch_flashing_lights.ino (1.66 KB)