Hello again all
I have been playing all day with different ways to switch between the diffenrent sequences, modes, that the LED's should be displaying.
/*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 <= 9; 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 = 9; i >= 0; i--){ //same as above but rather than starting at 0 and counting up
//we start at seven and count down
digitalWrite(ledPins[i], LOW); //Turns off LED #i each time this runs i
delay(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(){
// int delaytime = 350; //the time (in milliseconds) to pause between LEDs
//make smaller for quicker switching and larger for slower
//Turn each LED on one after another
for(int i = 0; i <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);
}
}
I have tried different ways, started out with the "Debounce" example. Did not work for me.
The "Button" example, saving the state until I used it and setting it to LOW after. Did not work for me.
Been looking all over the internet after solutions and can get my head around it.
The problem is that it will not ALWAYS change the sequence when I pressed the button. And sometimes it changes more than one sequence.
It differs ALOT on the speed that I update my LED's at. That means:
int Time = 300; //Here you put how fast the LED's shall shift
and that's not good.
I have now gone with Robin2's example to run a lot of stuff at the same time, I can't get it to work and now I'm a little tired of it.
Is there a kind soul that can poke me in the right direction?