Im having trouble get an array to stop on the current objects its on when i push a button and trying to get the array to reverse so that the light would go in the opposite direction when a press another button e.g if the array went from pin 3 to 5 i want it to reverse to go back from pin 5 to 3. your help is greatly apreciated.
int timer = 1000; // The higher the number, the slower the timing.
int ledPins[] = {
2, 3, 4, 5, 6, 7 }; // an array of pin numbers to which LEDs are attached
int pinCount = 6; // the number of pins (i.e. the length of the array)
void setup() {
for (int thisPin = 0; thisPin < pinCount; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
}
void loop() {
// loop from the lowest pin to the highest:
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);
}
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin -= 6); // reset thispin back to zero
}
Im having trouble get an array to stop on the current objects its on
Since arrays don't move, this is likely not your problem.
and trying to get the array to reverse
Arrays don't reverse, either.
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin -= 6); // reset thispin back to zero
The variable thisPin is a local variable. It doesn't need to be "reset back to sero".
The variable thisPin has a lousy name. It is not a pin number. It is used as an index into an array, and, usually, index variables have one letter names.
Perhaps if you actually had any code to read the state of the switch (pushing buttons on your shirt seems pretty useless), you could then make the code stop iterating through through the array.
using delay() is not preferred, check blink without delay how to improve on this sketch
you need a state variable that represents the state of the currentLED
I removed the loops as loop() is called iteratively, this should get you started
(code not tested)
int timer = 1000;
int ledPins[] = {
2, 3, 4, 5, 6, 7 };
int pinCount = 6; // the number of pins
int currentLed = 0;
int direction = +1;
void setup()
{
Serial.begin(115200); // for debugging
pinMode(2, INPUT); for the button
for (int thisPin = 0; thisPin < pinCount; thisPin++) pinMode(ledPins[thisPin], OUTPUT);
}
void loop()
{
// if (time to switch on) ...
// if (time to switch off) ...
digitalWrite(ledPins[currentLed ], HIGH);
delay(timer);
digitalWrite(ledPins[currentLed ], LOW);
currentLed += direction; // currentLed = currentLed + direction;
// prevent out of bounds
if (currentLed >= pinCount) currentLed = pinCount -1; // 6 leds => indices 0..5
if (currentLed < 0) currentLed = 0;
// is a biutton pressed
if (digitalRead(2) == HIGH) // connect pin 2 with +5V to change direction; be aware buttons bounce ...
{
direction = -direction;
delay(100); // for handling button bounce;
}
}