Hello all, it has been some time since I was here last. I am trying to get some code working to turn on /off each I/O pin sequentially each time the input button is pressed. Would someone be so kind as to check my code and let me know if it is going to do what I am trying to accomplish?
int INDICATOR = 13; // choose the pin for power indicator
int buttonPin = 14; // the number of the pushbutton pin
int timer = 500; // Sets the timer for 0.5 seconds
int ledPins[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; // an array of pin numbers to which LEDs are attached
int pinCount = 12; // the number of pins (i.e. the length of the array)
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
pinMode(INDICATOR, OUTPUT); // declare INDICATOR as output
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an 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);
}
}
void loop() {
digitalWrite(INDICATOR, LOW); // turn INDICATOR on
buttonState = digitalRead(buttonPin); // read the state of the pushbutton value
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
// turn the pin on:
digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
// turn the pin off:
digitalWrite(ledPins[thisPin], LOW);
}
}
}
Thanks in advance.
Sean