Go Down

Topic: Changing from delay() to "without delay()" (Read 5937 times) previous topic - next topic

Pady

Mar 29, 2014, 07:56 am Last Edit: Mar 29, 2014, 07:59 am by Pady Reason: 1
Here i smy code:
Code: [Select]
/*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:
Code: [Select]
Light a LED;
wait some time;
Light next LED;

But I'd rather do this:
Code: [Select]
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...

PaulRB

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:

Code: [Select]
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

Pady

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

Code: [Select]
/*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
Code: [Select]
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 :-)

PaulRB

#3
Mar 29, 2014, 10:11 am Last Edit: Mar 29, 2014, 10:13 am by PaulRB Reason: 1
Code: [Select]
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.

Pady

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

Pady

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...

PaulRB

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.

Pady

Code: [Select]
/*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 :-)

PaulRB

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:

Code: [Select]

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?

Pady

#9
Mar 31, 2014, 07:34 pm Last Edit: Mar 31, 2014, 07:40 pm by Pady Reason: 1
Quote
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...

PaulRB

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:

Code: [Select]
/*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);
  }
}


Pady

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 :-)

polymorph

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?
Steve Greenfield AE7HD
Drawing Schematics: tinyurl.com/23mo9pf - tinyurl.com/o97ysyx - https://tinyurl.com/Technote8
Multitasking: forum.arduino.cc/index.php?topic=223286.0
gammon.com.au/blink - gammon.com.au/serial - gammon.com.au/interrupts

PaulRB


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.

Pady

@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 :-) I love this thank you SOOOOO much :-)
One question thou, can you explain what happens in
Code: [Select]
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 :-)
Code: [Select]
/*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
Quote
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 :-) 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 :-)
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???

Go Up