Go Down

Topic: Newbie LED chase sequence (Read 671 times) previous topic - next topic

frankhiggins

Hi all,
I have this code that uses a pushbutton to start a chase and I want to alter the end from all the LEDS going out at once to going off one at a time in a random manner. Could someone please give me some code or tell me what I need to write in order to make this happen.

// A chase controlled by a button

int Button=2; //button assigned to pin 2
int timer = 100; // The higher the number, the slower the timing.
int ledPins[] = {
3,4,5,6,7,8,9,10 }; // an array of pin numbers to which LEDs are attached
int pinCount = 8; // the number of pins (i.e. the length of the array)

void setup() {
  Serial.begin(9600);
pinMode(Button, INPUT);
int thisPin;
// the array elements are numbered from 0 to (pinCount - 1).
// use a for loop to initialize each pin as an output:
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
//turn LEDS on
void loop() {
while (digitalRead(Button)==HIGH);
{
//delay (10);
  Serial.println("LOW");

}

while (digitalRead(Button)==LOW)
{
  Serial.println("HIGH");
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}

for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) {
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
}
}

I thought it might be something like this but can't seem to piece it together to get it to work.

// switch them randomly on and off
int someNumber = 22;
for (int i = 0; i < someNumber; i++){
int thisPin = random(3,10);
if (random(1) == 1){ // random(1) returns 0 or 1, I hope
  digitalWrite(ledPins[thisPin], HIGH);
}
else
{
  digitalWrite(ledPins[thisPin], LOW);
}
}
// Finally, switch them all off
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], LOW);
}


Any help much appreciated.

many thanks,

Frank

Grumpy_Mike

Please use the # icon when posting code:-
Code: [Select]
// Finally, switch them all off
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
digitalWrite(ledPins[thisPin], LOW);

This will work you need to put it in the right place. Before the last } in your loop() function is good.

jraskell

Don't cross post.  You posted this in the Programming forum as well.

Go Up