firt the code:
/*Alittle something for my dad's H0 modeltrain
*this is the light for a swing from the '50s
*based on a 8 LED kit from some web retailer.
*the code is simple, as were the Tivoli light in the '50s
************************************************************************/
const int buttonPin = 12; // the number of the pushbutton pin
//LED Pin Variables
int ledPins[] = {
2, 3, 4, 5, 6, 7, 8, 9, 10, 11}; //An array to hold the pin each LED is connected to
//i.e. LED #0 is connected to pin 2, LED #1, 3 and so on
//to address an array use ledPins[0] this would equal 2
//and ledPins[9] would equal 11
int pinCount = 10;
int delayTime = 350;
int ledState = HIGH; // the current state of the output pin
int buttonState; // the current reading from the input pin
int lastButtonState = LOW; // the previous reading from the input pin
// the following variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long lastDebounceTime = 0; // the last time the output pin was toggled
long debounceDelay = 50; // the debounce time; increase if the output flickers
/***********************************************************************************
* setup() - this function runs once when you turn your Arduino on
* We the three control pins to outputs
******************************************************************************/
void setup() {
//Set each pin connected to an LED to output mode (pulling high (on) or low (off)
int i;
for( int i=0; i < pinCount; i++){
pinMode(ledPins[i],OUTPUT); //we use this to set each LED pin to output
}
pinMode(buttonPin, INPUT);
}
/**************************************************************
(commented code will not run)
* these are the lines replaced by the for loop above they do exactly the
* same thing the one above just uses less typing
pinMode(ledPins[2],OUTPUT);
pinMode(ledPins[3],OUTPUT);
pinMode(ledPins[4],OUTPUT);
pinMode(ledPins[5],OUTPUT);
pinMode(ledPins[6],OUTPUT);
pinMode(ledPins[7],OUTPUT);
pinMode(ledPins[8],OUTPUT);
pinMode(ledPins[9],OUTPUT);
pinMode(ledPins[10],OUTPUT);
pinMode(ledPins[11],OUTPUT);
(end of commented code)
**********************************************************************************/
void oneAfterAnotherLoop(){
// int delayTime = 350; //the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower
//Turn Each LED on one after another
for(int i = 0; i <= 9; i++){
digitalWrite(ledPins[i], HIGH); //Turns on LED #i each time this runs i
delay(delayTime); //gets one added to it so this will repeat
} //8 times the first time i will = 0 the final
//time i will equal 7;
//Turn Each LED off one after another
for(int i = 9; i >= 0; i--){ //same as above but rather than starting at 0 and counting up
//we start at seven and count down
digitalWrite(ledPins[i], LOW); //Turns off LED #i each time this runs i
delay(delayTime); //gets one subtracted from it so this will repeat
} //8 times the first time i will = 7 the final
//time it will equal 0
}
/*******************************************************************************************
*/
void slowonalloff(){
// int delaytime = 350; //the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower
//Turn each LED on one after another
for(int i = 0; i <= 9; i++){
digitalWrite(ledPins[i], HIGH);
delay(delayTime);
}
//Turn all LED off at one
for(int i = 0; i <pinCount; ){
digitalWrite(ledPins[i], LOW); //Turns all LED off at once
delay(delayTime);
}
}
/*************************************************************************************/
void LowToHigh(){ //This will run the light from the buttom to the top
for(int i = 0; i < pinCount; i++) {
digitalWrite(ledPins[i], HIGH); //Will turn the LED on
delay(delayTime);
digitalWrite(ledPins[i], LOW); //Now we turn it off
}
}
/**********************************************************************************/
void HighToLow(){ //This runs the light from the top to the buttom
for(int i=pinCount - 1; i >=0; i--) {
digitalWrite(ledPins[i], HIGH); //Turn the LED on
delay(delayTime); //Wait a little
digitalWrite(ledPins[i], LOW);//And off again
}
}
/****************************************************************************************
* loop() - this function will start after setup finishes and then repeat
* we call a function called oneAfterAnother().
*****************************************************************************************/
void loop() {
// read the state of the switch into a local variable:
int reading = digitalRead(buttonPin);
// check to see if you just pressed the button
// (i.e. the input went from LOW to HIGH), and you've waited
// long enough since the last press to ignore any noise:
// If the switch changed, due to noise or pressing:
if (reading != lastButtonState) {
// reset the debouncing timer
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
// whatever the reading is at, it's been there for longer
// than the debounce delay, so take it as the actual current state:
// if the button state has changed:
if (reading != buttonState) {
buttonState = reading;
// only toggle the LED if the new button state is HIGH
if (buttonState == HIGH) {
ledState = !ledState;
}
}
}
{ // run over and over again
slowonalloff();
}
}
The part that is the problem it this:
void slowonalloff(){
// int delaytime = 350; //the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower
//Turn each LED on one after another
for(int i = 0; i <= 9; i++){
digitalWrite(ledPins[i], HIGH);
delay(delayTime);
}
//Turn all LED off at one
for(int i = 0; i <pinCount; ){
digitalWrite(ledPins[i], LOW); //Turns all LED off at once
delay(delayTime);
}
}
When I power up, it goes on as it should, but only the first pin goes off and then the program freezes.
I have been looking all over after a solution to this little but BIG problem.
I'm sure it's just something I missed but please help me understand what?