Pages: [1]   Go Down
Author Topic: Newbie LED chase sequence  (Read 591 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 18
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 596
Posts: 33288
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Please use the # icon when posting code:-
Code:
// 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.
Logged

New Hampshire
Offline Offline
God Member
*****
Karma: 17
Posts: 781
There are 10 kinds of people, those who know binary, and those who don't.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Pages: [1]   Go Up
Jump to: