@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
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 ![]()
/*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);
}
}
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???