Noob, need Help with mulitple codes on a switch

I have done a ton of tutorials but i cant seem to find one about changing code or switching code in a loop. I am trying to write two sets of looped code that have a certain LEd sequence. I am trying to let the first code to loop until a button on the analog side is switched changing the LED sequence to the new one and looping that code. Then, obviously switching the switch back the code goes back to the first loop.
Any help is apreciated

Joe

delorean11defa:
I have done a ton of tutorials but i cant seem to find one about changing code or switching code in a loop. I am trying to write two sets of looped code that have a certain LEd sequence. I am trying to let the first code to loop until a button on the analog side is switched changing the LED sequence to the new one and looping that code. Then, obviously switching the switch back the code goes back to the first loop.
Any help is apreciated

Joe

Well the C/C++ language certainly has the means to perform that kind of thing. You probably need to spend more time on language fundamentals then rushing to a application and trying to learn the various methods and commands 'on the fly'. The arduino reference section as a bunch of commands under the Control Structures label ( Arduino - Home ) These are the command one uses to make the program perform stuff when and only when you want them to be performed. When I started out I was able to learn a lot about the programming language by loading and reading the many many example sketches that are include in the Arduino IDE.

Otherwise all you can do is try your best and if it doesn't work post your sketch here and someone will surely try and explain what is wrong.

Good luck
Lefty

You need to track the 'mode' the switch is in. So assuming it is a tactile, every time you press it, it will increment an integer by one until it is the highest 'mode' then it will cycle back to 0 or 1. You will handle this in the Void loop.

The void loop is executed every cycle. In other words. As soon as the code reaches the last line of the loop, it goes back to the top of the loop and does it again.

//So in the declarations at the top of the program, declare 

int switchmode = 1; //You can set this to whatever starting mode you want. Even 0 if you don't want it to do anything until you press the button the first time.

//in the void loop () portion of the program you will need to increment that number every time you press the button something like this:

if (digitalRead(switchpin)) 
{
 switchmode += 1;
}

//If you have only modes 1,2,3 then when you hit mode 4 you want it to cycle back to 1 like so:

if (switchmode == 4) 
{
 switchmode = 1;
}

/*
Once you have the switchmode you can handle it a couple of ways. One way is to include everything in if then loops within the main void loop. This is not the best way since it will have to scan through that loop code every time, although it will skip over anywhere the if is not met. If you have custom functions instead, it will keep your code cleaner. So using that method, you will need to look at switchmode and then call a function based on what the switchmode int value is like so:
*/

//Also in the void loop 
void loop()
{

if (switchmode == 1)
{
 firstfunction(); //obviously you will want to call these functions something that is meaningful.
}
else if (switchmode == 2)
{
 secondfunction();
}
else
{
 thirdfunction();
}


//Then below the void loop or above it or where ever, my preference is below, you will create your functions:

void firstfunction()
{
 //what you want to happen if you are in mode 1
}

void secondfunction()
{
 //ditto
}

void thirdfunction()
{
 //and again
}
}

For me this is a better practice than trying to nest a ton of if loops in the main loop. This keeps your code separate and clean. If you have to make changes to the 3rd mode, you don't have to try to remember which parts of the main loop you need to modify, you simply go to the thirdfunction. However, if your program threatens to outgrow your ram space, these functions may be the first thing you have to sacrifice if it means you can combine code in the void loop()

This is the simplest way to call a function. Don't forget that you can pass data back and forth between the functions which may or may not be handy.

Hope this helps.

Luck,

Wade

One more thing.

If you are indeed using a momentary button like a tactile, you are going to quickly run into a problem with button debounce and if you are unfamiliar with it, it is going to confuse the crap out of you when the modes don't switch like you expect them to.

I would suggest two things,

First watch this video Tutorial 02 for Arduino: Buttons, PWM, and Functions - YouTube

In fact, I would watch all of his tutorials, but this one explains how to handle debounce in the software instead of hardware.

Second, put an LED on your protoboard for each mode, turning them on or off as appropriate. This will let you know you are in the right mode without having to look at terminal server all the time. While not at all required, I like the quick visual indicator that I am in the right mode.

Luck,

Wade

awesome thank you ! great posts they all helped a lot.. i have it up and running and got some great inspiration along the way

Thanks again!

Joe