Changing from delay() to "without delay()"

Here i smy 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 buttonInterval = 100; // number of millisecs between button readings
const int buttonPin = 2;    // the number of the pushbutton pin
//LED Pin Variables
const int ledPins[] = {
  3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; 
//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
//Just write in, the pins you use
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

int funcCount = 7;           //Here you put how many functions you have                                   
int sequence = 1;            //Here we hold the current sequence in the program
int pinCount = 10;           //Here you type how many LED's you have in your array                         
int Time = 300;              //Here you put how fast the LED's shall shift
// 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                                   
boolean Next = false;

/***********************************************************************************
 * 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);
  }
}
/****************************************************************************************
 * loop() - this function will start after setup finishes and then repeat
 * we call a function called oneAfterAnother().
 *****************************************************************************************/

void loop() {
  readButton();               // call the functions that do the work
  if (Next == true)
    if(sequence == funcCount)
    {
      sequence = 1;
    }
    else
    {
      sequence++;
    }

  switch(sequence)
  {
  case 1:
    slowonalloff();
    break;
  case 2:
    oneAfterAnotherLoop();
    break;
  case 3:
    LowToHigh();
    break;
  case 4:
    HighToLow();
    break;
  case 5:
    UpAndDown();
    break;
  case 6:
    SlowOnSlowOff();
    break;
  case 7:
    SlowOnSlowOffReverse();
    break;
  }

}

/******************************************************************
 * Here we start typing in the different types of sequenses we want in
 * our program.
 ******************************************************************/
void readButton() {
  // 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;
    }
    if (buttonState == HIGH) {
      Next = true;
    }
  }
}
/******************************************************************************/
void oneAfterAnotherLoop(){
  //Turn Each LED on one after another
  for(int i = 0; i < pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  //Turns on LED #i each time this runs i
    delay(Time);                //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 = pinCount; 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(Time);                //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(){
  //Turn each LED on one after another
  for(int i = 0; i <pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    delay(Time);                 
  }                                  

  //Turn all LED off at one
  for(int i = 0; i <pinCount;i++ ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
  }
  {
    delay(Time);
  }
}
/*************************************************************************************/

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(Time);
    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(Time); //Wait a little
    digitalWrite(ledPins[i], LOW);//And off again

  }
}
/************************************************************************************/
void UpAndDown(){
  for (int i = 0; i < pinCount; i++) {
    digitalWrite (ledPins[i], HIGH);
    delay(Time);
    digitalWrite(ledPins[i],LOW);
  }
  {
    for(int i=pinCount - 1; i >=0; i--) { 
      digitalWrite(ledPins[i], HIGH); //Turn the LED on
      delay(Time); //Wait a little
      digitalWrite(ledPins[i], LOW);//And off again
    }
  }
}
/********************************************************************************/
void SlowOnSlowOff(){
  //Turn each LED on one after another
  for(int i = 0; i <pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    delay(Time);                 
  }                                  

  //Turn each LED off again
  for(int i = 0; i <pinCount;i++ ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
    delay(Time);
  }
}
/*********************************************************************************/
void SlowOnSlowOffReverse(){
  //Turn each LED on one after another
  for(int i = pinCount; i <0; i--){
    digitalWrite(ledPins[i], HIGH);  
    delay(Time);                 
  }                                  

  //Turn each LED off again
  for(int i = pinCount; i <0;i-- ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
    delay(Time);
  }
}

The light works but because af all the delay()'s the button don't work.
I have been trying a lot of stuff and searching all over the internet, but can't find anything that I can understand and make work.
In theory i'm doing this:

Light a LED;
wait some time;
Light next LED;

But I'd rather do this:

Light a LED;
come back later when it's time;
Light next LED;

Is it possible??

I have had another tread about this here:
http://forum.arduino.cc/index.php?topic=226331.msg1655620#msg1655620
But I'm not going to get that working and it's beginning to be too messed...

Hi Pady,

I can see that from that thread that Robin2 is helping you with that its hard to get your head around if you are a beginner!

How about this for a "quick & dirty" fix:

void myDelay(long t) {
  for (long i = 0; i<= t && digitalRead(buttonPin) == LOW; i++) {
    delay(1);
  }
}

Add this function to your sketch and then replace each of your calls to "delay(Time)" with "myDelay(Time)".

All it basically does is break down the long delays into a series of short ones, and checks for the button press before each one. So if the button is pressed, the delay cut short and which ever animation function is running, it finishes in less than the blink of an eye, so your sketch then returns to main() and can check the button properly.

I have not tested that, only checked that it compiles OK. Also you seem to have some strange extra braces in your sketch - "{" & "}" - you might want to double-check, for example in slowonalloff().

Paul

@Paul
I have removed a little extra brackets and put in your fix, but it tells me that there are too few arguments??

/*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 buttonInterval = 100; // number of millisecs between button readings
const int buttonPin = 2;    // the number of the pushbutton pin
//LED Pin Variables
const int ledPins[] = {
  3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; 
//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
//Just write in, the pins you use
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

int funcCount = 7;           //Here you put how many functions you have                                   
int sequence = 1;            //Here we hold the current sequence in the program
int pinCount = 10;           //Here you type how many LED's you have in your array                         
int Time = 300;              //Here you put how fast the LED's shall shift
// 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                                   
unsigned long t;

boolean Next = false;

/***********************************************************************************
 * 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);
}
/****************************************************************************************
 * loop() - this function will start after setup finishes and then repeat
 * we call a function called oneAfterAnother().
 *****************************************************************************************/

void loop() {
  readButton();               // call the functions that do the work
  myDelay();
  if (Next == true)
    if(sequence == funcCount)
    {
      sequence = 1;
    }
    else
    {
      sequence++;
    }

  switch(sequence)
  {
  case 1:
    slowonalloff();
    break;
  case 2:
    oneAfterAnotherLoop();
    break;
  case 3:
    LowToHigh();
    break;
  case 4:
    HighToLow();
    break;
  case 5:
    UpAndDown();
    break;
  case 6:
    SlowOnSlowOff();
    break;
  case 7:
    SlowOnSlowOffReverse();
    break;
  }

}

/******************************************************************
 * Here we start typing in the different types of sequenses we want in
 * our program.
 ******************************************************************/
void myDelay(long t) {
  for (long i = 0; i<= t && digitalRead(buttonPin) == LOW; i++) {
    delay(1);
  }
}
/*************************************************************/
void readButton() {
  // 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;
    }
    if (buttonState == HIGH) {
      Next = true;
    }
  }
}
/******************************************************************************/
void oneAfterAnotherLoop(){
  //Turn Each LED on one after another
  for(int i = 0; i < pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  //Turns on LED #i each time this runs i
    myDelay(Time);                //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 = pinCount; 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
    myDelay(Time);                //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(){
  //Turn each LED on one after another
  for(int i = 0; i <pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    myDelay(Time);                 
  }                                  

  //Turn all LED off at one
  for(int i = 0; i <pinCount;i++ ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
  }
    myDelay(Time);
}
/*************************************************************************************/

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
    myDelay(Time);
    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
    myDelay(Time); //Wait a little
    digitalWrite(ledPins[i], LOW);//And off again

  }
}
/************************************************************************************/
void UpAndDown(){
  for (int i = 0; i < pinCount; i++) {
    digitalWrite (ledPins[i], HIGH);
    myDelay(Time);
    digitalWrite(ledPins[i],LOW);
  }
  {
    for(int i=pinCount - 1; i >=0; i--) { 
      digitalWrite(ledPins[i], HIGH); //Turn the LED on
      myDelay(Time); //Wait a little
      digitalWrite(ledPins[i], LOW);//And off again
    }
  }
}
/********************************************************************************/
void SlowOnSlowOff(){
  //Turn each LED on one after another
  for(int i = 0; i <pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    myDelay(Time);                 
  }                                  

  //Turn each LED off again
  for(int i = 0; i <pinCount;i++ ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
    myDelay(Time);
  }
}
/*********************************************************************************/
void SlowOnSlowOffReverse(){
  //Turn each LED on one after another
  for(int i = pinCount; i <0; i--){
    digitalWrite(ledPins[i], HIGH);  
    myDelay(Time);                 
  }                                  

  //Turn each LED off again
  for(int i = pinCount; i <0;i-- ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
    myDelay(Time);
  }
}

I have assinged an unsigned long t; in the global variables, so why is'ent it working?
And I love your "Quick N' Dirty" fix, much easier to get my head around :slight_smile:

void loop() {
  readButton();               // call the functions that do the work
  myDelay(); // <-- What's this?
  if (Next == true)

You added an extra call in loop() without a parameter. Take that out and it compiles. You don't need the extra global variable.

But don't Ineed to call it in the loop to use the function outside the loop??

It complies all right and can be uploaded, but all the LED's are lit and are doing nothing.
When I push the button the LED's light up a little more when the button is down...

If you to back to your original code from the first post (with the corrections we spotted) does it work correctly (other than the button not responding)?

Does pin 2 go HIGH when the button is pressed, or low?

Lets see your schematic, or a well-focussed close-up pic.

/*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 buttonInterval = 100; // number of millisecs between button readings
const int buttonPin = 2;    // the number of the pushbutton pin
//LED Pin Variables
const int ledPins[] = {
  3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; 
//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
//Just write in, the pins you use
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

int funcCount = 7;           //Here you put how many functions you have                                   
int sequence = 1;            //Here we hold the current sequence in the program
int pinCount = 10;           //Here you type how many LED's you have in your array                         
int Time = 300;              //Here you put how fast the LED's shall shift
// 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                                   

boolean Next = false;

/***********************************************************************************
 * 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);
}
/****************************************************************************************
 * loop() - this function will start after setup finishes and then repeat
 * we call a function called oneAfterAnother().
 *****************************************************************************************/

void loop() {
  readButton();               // call the functions that do the work
  if (Next == true)
    if(sequence == funcCount)
    {
      sequence = 1;
    }
    else
    {
      sequence++;
    }

  switch(sequence)
  {
  case 1:
    slowonalloff();
    break;
  case 2:
    oneAfterAnotherLoop();
    break;
  case 3:
    LowToHigh();
    break;
  case 4:
    HighToLow();
    break;
  case 5:
    UpAndDown();
    break;
  case 6:
    SlowOnSlowOff();
    break;
  case 7:
    SlowOnSlowOffReverse();
    break;
  }

}

/******************************************************************
 * Here we start typing in the different types of sequenses we want in
 * our program.
 ******************************************************************/
/*void myDelay(long t) {
  for (long i = 0; i<= t && digitalRead(buttonPin) == LOW; i++) {
    delay(1);
  }
}
/*************************************************************/
void readButton() {
  // 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;
    }
    if (buttonState == HIGH) {
      Next = true;
    }
  }
}
/******************************************************************************/
void oneAfterAnotherLoop(){
  //Turn Each LED on one after another
  for(int i = 0; i < pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  //Turns on LED #i each time this runs i
    delay(Time);                //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 = pinCount; 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(Time);                //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(){
  //Turn each LED on one after another
  for(int i = 0; i <pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    delay(Time);                 
  }                                  

  //Turn all LED off at one
  for(int i = 0; i <pinCount;i++ ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
  }
    delay(Time);
}
/*************************************************************************************/

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(Time);
    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(Time); //Wait a little
    digitalWrite(ledPins[i], LOW);//And off again

  }
}
/************************************************************************************/
void UpAndDown(){
  for (int i = 0; i < pinCount; i++) {
    digitalWrite (ledPins[i], HIGH);
    delay(Time);
    digitalWrite(ledPins[i],LOW);
  }
  {
    for(int i=pinCount - 1; i >=0; i--) { 
      digitalWrite(ledPins[i], HIGH); //Turn the LED on
      delay(Time); //Wait a little
      digitalWrite(ledPins[i], LOW);//And off again
    }
  }
}
/********************************************************************************/
void SlowOnSlowOff(){
  //Turn each LED on one after another
  for(int i = 0; i <pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    delay(Time);                 
  }                                  

  //Turn each LED off again
  for(int i = 0; i <pinCount;i++ ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
    delay(Time);
  }
}
/*********************************************************************************/
void SlowOnSlowOffReverse(){
  //Turn each LED on one after another
  for(int i = pinCount; i <0; i--){
    digitalWrite(ledPins[i], HIGH);  
    delay(Time);                 
  }                                  

  //Turn each LED off again
  for(int i = pinCount; i <0;i-- ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
    delay(Time);
  }
}

This works, but with very unstable button.
I have looked a lot on the arduino examples on how to hook up the button and to write the code to that bit..

The pic I added is of the schematic :slight_smile:

I can't spot the problem. The new version of the sketch and schematic both look ok.

Try the new code again with this slight change to myDelay:

void myDelay(long t) {
  for (long i = 0; i<= t; i++) {
    delay(1);
  }
}

This should behave just like the old version of the sketch. Does it?

This should behave just like the old version of the sketch. Does it?

Nope, now the button has no effect at all, can you understand my fustration?? Have been fighting with this a couple of weeks...

I found a few more mistakes. Your readButton() function was never setting Next to true. Also your SlowOnSlowOffReverse() was getting stuck in an infinite loop.

Here you go:

/*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 buttonInterval = 100; // number of millisecs between button readings
const int buttonPin = 2;    // the number of the pushbutton pin
//LED Pin Variables
const int ledPins[] = {
  3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; 
//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
//Just write in, the pins you use
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

int funcCount = 7;           //Here you put how many functions you have                                   
int sequence = 1;            //Here we hold the current sequence in the program
int pinCount = 10;           //Here you type how many LED's you have in your array                         
int Time = 300;              //Here you put how fast the LED's shall shift
// 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                                   

boolean Next = false;

/***********************************************************************************
 * 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);
}
/****************************************************************************************
 * loop() - this function will start after setup finishes and then repeat
 * we call a function called oneAfterAnother().
 *****************************************************************************************/

void loop() {
  readButton();
  if (Next == true) {
    if(sequence == funcCount)
    {
      sequence = 1;
    }
    else
    {
      sequence++;
    }
    Next = false;
  }
  
  switch(sequence)
  {
  case 1:
    slowonalloff();
    break;
  case 2:
    oneAfterAnotherLoop();
    break;
  case 3:
    LowToHigh();
    break;
  case 4:
    HighToLow();
    break;
  case 5:
    UpAndDown();
    break;
  case 6:
    SlowOnSlowOff();
    break;
  case 7:
    SlowOnSlowOffReverse();
    break;
  }

}

/******************************************************************
 * Here we start typing in the different types of sequenses we want in
 * our program.
 ******************************************************************/

void myDelay(unsigned long t) {
  for (unsigned long i = 0; i < t && !Next; i++) {
    delay(1);
    readButton();
  }
}

void readButton() {
  // 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;
      lastDebounceTime = millis();
    }
    if (buttonState == HIGH) {
      Next = true;
    }
  }
}
/******************************************************************************/
void oneAfterAnotherLoop(){
  //Turn Each LED on one after another
  for(int i = 0; i < pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  //Turns on LED #i each time this runs i
    myDelay(Time);                //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 = pinCount; 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
    myDelay(Time);                //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(){
  //Turn each LED on one after another
  for(int i = 0; i <pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    myDelay(Time);                 
  }                                  

  //Turn all LED off at one
  for(int i = 0; i <pinCount;i++ ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
  }
    myDelay(Time);
}
/*************************************************************************************/

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
    myDelay(Time);
    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
    myDelay(Time); //Wait a little
    digitalWrite(ledPins[i], LOW);//And off again

  }
}
/************************************************************************************/
void UpAndDown(){
  for (int i = 0; i < pinCount; i++) {
    digitalWrite (ledPins[i], HIGH);
    myDelay(Time);
    digitalWrite(ledPins[i],LOW);
  }
  {
    for(int i=pinCount - 1; i >=0; i--) { 
      digitalWrite(ledPins[i], HIGH); //Turn the LED on
      myDelay(Time); //Wait a little
      digitalWrite(ledPins[i], LOW);//And off again
    }
  }
}
/********************************************************************************/
void SlowOnSlowOff(){
  //Turn each LED on one after another
  for(int i = 0; i <pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    myDelay(Time);                 
  }                                  

  //Turn each LED off again
  for(int i = 0; i <pinCount;i++ ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
    myDelay(Time);
  }
}
/*********************************************************************************/
void SlowOnSlowOffReverse(){
  //Turn each LED on one after another
  for(int i = pinCount; i >= 0; i--){
    digitalWrite(ledPins[i], HIGH);  
    myDelay(Time);                 
  }                                  

  //Turn each LED off again
  for(int i = pinCount; i >= 0;i-- ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
    myDelay(Time);
  }
}

I tested the code you gave me, but the LED's just stood and flickered. But looking at the code made me realize that I had some minor problems, I adjusted them and now the button works but sometimes it swiches when I don't push or switches more than one sequence..
So Now I have a button stabillity problem...
But thanks for the help till now :slight_smile:

And... this is why I wish they would take that "Blink" sketch out of the examples. Teaching us to use delay() to time events first, is like teaching a student driver to brake by using the emergency brake.

Have you taken a look at "Blink Without Delay" and "Debounce" in the Examples?

Pady:
I tested the code you gave me, but the LED's just stood and flickered.

I tested that code, I know it works. Its working right now, on the desk in my study at home. I followed your schematic too, no problems with that. I did not get the problems you describe. You should not have to adjust the code any further...

Can you post a pic of your circuit. I'll post one too this evening.

@PaulRB
Now my code works as well, I think that I have a bad resistor or something like that, so I change to INPUT_PULLUP instead and now it WORKS :slight_smile: I love this thank you SOOOOO much :slight_smile:
One question thou, can you explain what happens in

void myDelay(unsigned long t) {
  for (unsigned long i = 0; i < t && !Next; i++) {
    delay(1);
    readButton();
  }
}

I adjusted the debounce delay up a bit and it work like a charm :slight_smile:

/*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 buttonInterval = 500; // number of millisecs between button readings
const int buttonPin = 2;    // the number of the pushbutton pin
//LED Pin Variables
const int ledPins[] = {
  3, 4, 5, 6, 7, 8, 9, 10, 11, 12}; 
//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
//Just write in, the pins you use
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

int funcCount = 7;           //Here you put how many functions you have                                   
int sequence = 1;            //Here we hold the current sequence in the program
int pinCount = 10;           //Here you type how many LED's you have in your array                         
int Time = 300;              //Here you put how fast the LED's shall shift
// 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 = 200;    // the debounce time; increase if the output flickers                                   

boolean Next = false;

/***********************************************************************************
 * 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_PULLUP);
}
/****************************************************************************************
 * loop() - this function will start after setup finishes and then repeat
 * we call a function called oneAfterAnother().
 *****************************************************************************************/

void loop() {
  readButton();
  if (Next == true) {
    if(sequence == funcCount)
    {
      sequence = 1;
    }
    else
    {
      sequence++;
    }
    Next = false;
  }
  
  switch(sequence)
  {
  case 1:
    slowonalloff();
    break;
  case 2:
    oneAfterAnotherLoop();
    break;
  case 3:
    LowToHigh();
    break;
  case 4:
    HighToLow();
    break;
  case 5:
    UpAndDown();
    break;
  case 6:
    SlowOnSlowOff();
    break;
  case 7:
    SlowOnSlowOffReverse();
    break;
  }

}

/******************************************************************
 * Here we start typing in the different types of sequenses we want in
 * our program.
 ******************************************************************/

void myDelay(unsigned long t) {
  for (unsigned long i = 0; i < t && !Next; i++) {
    delay(1);
    readButton();
  }
}

void readButton() {
  // 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;
      lastDebounceTime = millis();
    }
    if (buttonState == LOW) {
      Next = true;
    }
  }
}
/******************************************************************************/
void oneAfterAnotherLoop(){
  //Turn Each LED on one after another
  for(int i = 0; i < pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  //Turns on LED #i each time this runs i
    myDelay(Time);                //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 = pinCount; 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
    myDelay(Time);                //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(){
  //Turn each LED on one after another
  for(int i = 0; i <pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    myDelay(Time);                 
  }                                  

  //Turn all LED off at one
  for(int i = 0; i <pinCount;i++ ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
  }
    myDelay(Time);
}
/*************************************************************************************/

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
    myDelay(Time);
    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
    myDelay(Time); //Wait a little
    digitalWrite(ledPins[i], LOW);//And off again

  }
}
/************************************************************************************/
void UpAndDown(){
  for (int i = 0; i < pinCount; i++) {
    digitalWrite (ledPins[i], HIGH);
    myDelay(Time);
    digitalWrite(ledPins[i],LOW);
  }
  {
    for(int i=pinCount - 1; i >=0; i--) { 
      digitalWrite(ledPins[i], HIGH); //Turn the LED on
      myDelay(Time); //Wait a little
      digitalWrite(ledPins[i], LOW);//And off again
    }
  }
}
/********************************************************************************/
void SlowOnSlowOff(){
  //Turn each LED on one after another
  for(int i = 0; i <pinCount; i++){
    digitalWrite(ledPins[i], HIGH);  
    myDelay(Time);                 
  }                                  

  //Turn each LED off again
  for(int i = 0; i <pinCount;i++ ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
    myDelay(Time);
  }
}
/*********************************************************************************/
void SlowOnSlowOffReverse(){
  //Turn each LED on one after another
  for(int i = pinCount; i >= 0; i--){
    digitalWrite(ledPins[i], HIGH);  
    myDelay(Time);                 
  }                                  

  //Turn each LED off again
  for(int i = pinCount; i >= 0;i-- ){
    digitalWrite(ledPins[i], LOW);        //Turns all LED off at once
    myDelay(Time);
  }
}

@polymorph

nd... this is why I wish they would take that "Blink" sketch out of the examples. Teaching us to use delay() to time events first, is like teaching a student driver to brake by using the emergency brake.

Have you taken a look at "Blink Without Delay" and "Debounce" in the Examples?

Yes that would be a wonderful idea :slight_smile: The "Debounce" is used to control the button, but the "Blink Without Delay" was to no help for me, the problem for me and everybody else is that it must be impossible to control the sequence's and change between them...
But it work now :slight_smile:
Is there a good place to write about it and show it to others so next person doing this don't have to work several weeks???

It is possible to use "Blink Without Delay" to control large numbers of events, but you have to set it up correctly. Each event has to have its own variable storing the last time it was changed.

It would be nice to have an example including more than one event.

If you look in the start of the topic, I linked to another topic where I learned a lot but could not get that to work.
It seems like a big problem cause whenever this kind of question comes up, everybode just say "Look at the Blink without Delay()" but no one makes a code that can do it... Or is it just me??
I know I'm not a coding genius, but I can make my way round the forums and peace something together :slight_smile: Now I'm trying to load the code up to a Pro Mini that will drive it all...

Pady:
One question thou, can you explain what happens in

void myDelay(unsigned long t) {

for (unsigned long i = 0; i < t && !Next; i++) {
    delay(1);
    readButton();
  }
}

Well, like I said earlier, it breaks down long delays into a series of short delays. Between each short delay, your function gets called to check for button presses. So instead of only getting called once every 2 or 3 seconds, it gets called every 1 millisecond. If the switch is pressed and stays pressed longer than the debounce period, your global variable "Next" gets set to true. When that happens, the series of short delays gets aborted, and no other delays can be allowed until Next gets set to false again by the loop() function. So the remaining steps of animating the LEDs in whichever pattern is currently going on gets finished in a very short time (you can just see the LEDs flash briefly before the new pattern starts).

This technique is "quick and dirty", as we discussed earlier, and is OK in some situations but not all. If all that is being controlled is LEDs, it doesn't matter much if they flash briefly before the next sequence starts. But in other situations, where signals are being fed to other components for example, even brief unwanted signals will cause the circuit to not function as desired.

Paul

I'm at work, so I can't test this code. Someone tell me if there is anything wrong here. This should blink four LEDs asynchronously.

*/ Blink Four LEDs Without Delay
  Adapted from Blink Without Delay
  by Steven J Greenfield, aka Polymorph
  Use however you like
/*


// constants won't change. Used here to 
// set pin numbers:
const int ledPin0 =  2;      // the number of the LED0 pin
const int ledPin1 =  3;      // the number of the LED1 pin
const int ledPin2 =  4;      // the number of the LED2 pin
const int ledPin3 =  5;      // the number of the LED3 pin

// Variables will change:
int ledState0 = LOW;             // ledState used to set the LED0
int ledState1 = LOW;             // ledState used to set the LED1
int ledState2 = LOW;             // ledState used to set the LED2
int ledState3 = LOW;             // ledState used to set the LED3

unsigned long previousMillis0 = 0;        // will store last time LED0 was updated
unsigned long previousMillis1 = 0;        // will store last time LED1 was updated
unsigned long previousMillis2 = 0;        // will store last time LED2 was updated
unsigned long previousMillis3 = 0;        // will store last time LED3 was updated

// the follow variables is a long because the time, measured in milliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long interval0 = 1000;           // interval at which to blink LED0 (milliseconds)
unsigned long interval1 = 457;           // interval at which to blink LED1 (milliseconds)
unsigned long interval2 = 1020;           // interval at which to blink LED2 (milliseconds)
unsigned long interval3 = 742;           // interval at which to blink LED3 (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin0, OUTPUT);    
  pinMode(ledPin1, OUTPUT); 
  pinMode(ledPin2, OUTPUT); 
  pinMode(ledPin3, OUTPUT);   
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.

  // save the current time so it doesn't change during an operation
  unsigned long currentMillis = millis();

  // LED0 
  if(currentMillis - previousMillis0 > interval0) {
    // save the last time you blinked the LED0 
    previousMillis0 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState0 == LOW)
      ledState0 = HIGH;
    else
      ledState0 = LOW;

  //LED1
  if(currentMillis - previousMillis1 > interval1) {
    // save the last time you blinked the LED0 
    previousMillis1 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState1 == LOW)
      ledState1 = HIGH;
    else
      ledState1 = LOW;

  //LED2
  if(currentMillis - previousMillis2 > interval2) {
    // save the last time you blinked the LED0 
    previousMillis2 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState2 == LOW)
      ledState2 = HIGH;
    else
      ledState2 = LOW;

  //LED3
  if(currentMillis - previousMillis3 > interval3) {
    // save the last time you blinked the LED0 
    previousMillis3 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState3 == LOW)
      ledState3 = HIGH;
    else
      ledState3 = LOW;

    // set the LEDs with the ledStates of the variable:
    digitalWrite(ledPin0, ledState0);
    digitalWrite(ledPin1, ledState1);
    digitalWrite(ledPin2, ledState2);
    digitalWrite(ledPin3, ledState3);

  }
}

I had to fix various comment delimiters and missing braces. Is this what you meant?

/* Blink Four LEDs Without Delay
  Adapted from Blink Without Delay
  by Steven J Greenfield, aka Polymorph
  Use however you like
*/


// constants won't change. Used here to 
// set pin numbers:
const int ledPin0 =  2;      // the number of the LED0 pin
const int ledPin1 =  3;      // the number of the LED1 pin
const int ledPin2 =  4;      // the number of the LED2 pin
const int ledPin3 =  5;      // the number of the LED3 pin

// Variables will change:
int ledState0 = LOW;             // ledState used to set the LED0
int ledState1 = LOW;             // ledState used to set the LED1
int ledState2 = LOW;             // ledState used to set the LED2
int ledState3 = LOW;             // ledState used to set the LED3

unsigned long previousMillis0 = 0;        // will store last time LED0 was updated
unsigned long previousMillis1 = 0;        // will store last time LED1 was updated
unsigned long previousMillis2 = 0;        // will store last time LED2 was updated
unsigned long previousMillis3 = 0;        // will store last time LED3 was updated

// the follow variables is a long because the time, measured in milliseconds,
// will quickly become a bigger number than can be stored in an int.
unsigned long interval0 = 1000;           // interval at which to blink LED0 (milliseconds)
unsigned long interval1 = 457;           // interval at which to blink LED1 (milliseconds)
unsigned long interval2 = 1020;           // interval at which to blink LED2 (milliseconds)
unsigned long interval3 = 742;           // interval at which to blink LED3 (milliseconds)

void setup() {
  // set the digital pin as output:
  pinMode(ledPin0, OUTPUT);    
  pinMode(ledPin1, OUTPUT); 
  pinMode(ledPin2, OUTPUT); 
  pinMode(ledPin3, OUTPUT);   
}

void loop()
{
  // here is where you'd put code that needs to be running all the time.

  // check to see if it's time to blink the LED; that is, if the 
  // difference between the current time and last time you blinked 
  // the LED is bigger than the interval at which you want to 
  // blink the LED.

  // save the current time so it doesn't change during an operation
  unsigned long currentMillis = millis();

  // LED0 
  if(currentMillis - previousMillis0 > interval0) {
    // save the last time you blinked the LED0 
    previousMillis0 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState0 == LOW)
      ledState0 = HIGH;
    else
      ledState0 = LOW;
  }

  //LED1
  if(currentMillis - previousMillis1 > interval1) {
    // save the last time you blinked the LED0 
    previousMillis1 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState1 == LOW)
      ledState1 = HIGH;
    else
      ledState1 = LOW;
  }

  //LED2
  if(currentMillis - previousMillis2 > interval2) {
    // save the last time you blinked the LED0 
    previousMillis2 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState2 == LOW)
      ledState2 = HIGH;
    else
      ledState2 = LOW;
  }

  //LED3
  if(currentMillis - previousMillis3 > interval3) {
    // save the last time you blinked the LED0 
    previousMillis3 = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState3 == LOW)
      ledState3 = HIGH;
    else
      ledState3 = LOW;

    // set the LEDs with the ledStates of the variable:
    digitalWrite(ledPin0, ledState0);
    digitalWrite(ledPin1, ledState1);
    digitalWrite(ledPin2, ledState2);
    digitalWrite(ledPin3, ledState3);

  }
}