77Views0Replies

Author Options:

How to alter the code in this LED chase Answered


Hi,here is the code i have for a chase controlled by a button. When the button is engaged (held in) the chase runs. When the button is released the chase stops. My question is how can I make the LEDS appear to go haywire when the button is released and not simply go off.

// A chase controlled by a button

int Button=2; //button assigned to pin 2
int timer = 150; // 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++) {
//turn leds on
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
//turn leds off
digitalWrite(ledPins[thisPin], LOW);
}
//turn leds on
for (int thisPin = pinCount -1; thisPin >= 0; thisPin--) {
//turn leds off
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
digitalWrite(ledPins[thisPin], LOW);
}
}
}

many thanks,

Frank

Discussions