Easy ? for the veterans...

Ok, I want to use a pushbutton as a type of “Mode” selector, and all of the Digital Pins are taken, so it will be connected to an analog one with a pull up or pull down resistor…

How do i make it select something different when it is pushed? like, cycle through 13 different things and then go back to number 1? I am unsure how to code this, but i believe it may consist of either a seperate loop to watch for button pushes and when it detects one, +1 to an integer variable, and then use if or possibly while statements to say…

My thoughts…(I think this would do it…)

int Button = 0;
int ButtonVal = 0;

void setup(){
ButtonVal = 0; //reset counter
}

void loop(){
ButtonVal = analogRead(Button);
//--------
Somehow convert (Button) to a on/off value, then store it’s count in (ButtonVal)
//--------

while (ButtonVal = 0){
Blah blah blah;}
while (ButtonVal = 1){
Blah blah blah;}

all the way to 13, and so on…

"Ok, if the value of (Variable) is 3, then do this…

If the value of (variable) is 10, then do this…

and then when it gets to say…13 or whatever…it will cycle back to 0.

and also will the button need to be debounced? and if the button is held on, it should just increase by 1, and not keep looping and +1 every loop.

can someone help me put these thoughts into code?

thanks a lot!

You can treat the analog pins as digital pins, just call them digital pins 14 through 19.

You'll probably want to debounce the switches. You might consider using the debounce library for button handling, unless you just want to write your own for practice.

ok, that solves those problems...i was unaware that you can call the analog pins as digital ones.

Thanks!

ok so, what's left is a way to count the button presses after they have been debounced.

Im thinking that if the button is held down, the void loop(){} will add 1 to its value every time the loop loops

You have the right basic scheme for using one button to cycle through different modes.

You need a variable to keep track of the current mode of the system (e.g. 0 - 12).

In your main loop, you can first check for a button press. Every time you detect one, you can increment that variable by one, taking care to make sure that when you are in the last mode that you wrap around to the first.

Then after that check, every time through the loop, you can look to see what mode is currently set and do the appropriate thing. A convenient way to accomplish that is with a switch statement. So, perhaps:

int mode = 0;

void setup () {
  // whatever
}

void loop () {

  // check for button press

  if "buttonPressed" {
    mode = mode + 1;
    if (mode > 12) mode = 0;
    }

  // do whatever is necessary for each mode

  switch (mode) {

    case 0:
       // do mode 0 function
      break;

    case 1:
      // do mode 1 function
      break;

    // etc.

    default:
      // HUH? unknown mode !
      break;

  }

}

Just wanted to point out, in the code that Andy posted, the last part:

I could be wrong, I've had little experience with Case statements, but from the few that I've used in examples, it seems the default is the same as using an Else statement. Instead of needing to use else{} for every case, you just set a Default.

But take a look at this pushButton tutorial, it has the same idea of what you want, you'll just need to add your extras to it! http://arduino.cc/en/Tutorial/ButtonStateChange

A few things to keep in mind about the tutorial: If you want it to reset the counter after it hits a certain number, use if (counter >= 14){counter = 0;} using >= makes sure it doesn't miss if you don't debounce and it counts as two pushes.

Using the Case statement, shown earlier, will make the code MUCH easier, no need for all the if(buttoncount == 2) statements. I would start with the tutorial above, get it working with every 4 pushes, then change the code up for yourself.

Hopefully some of these pointers help, I'm still experimenting with buttons myself, and this stuff took some research for to figure out myself!:D

it seems the default is the same as using an Else statement. Instead of needing to use else{} for every case, you just set a Default.

Functionally a series of cascading if/then/else if/else if/else is equivalent to the switch with a default. So:

if (mode == 0) {
  //do mode 0
  }
else if (mode == 1) {
  // do mode 1
  }
else if (mode == 2) {
  // do mode 2
  }
else {
  // do default
  };

equals

switch (mode) {
  case 0:
    // do mode 0
    break;
  case 1:
    // do mode 1
    break;
  case 2:
    // do mode 2
    break;
  default:
    // do default
    break;
  };

And for the compound conditional cases with OR, you can do this kind of thing:

if (mode == 0) || (mode == 1) {
  // mode 0 and 1
  }
else if (mode == 2) {
  // do mode 2
  }
else {
  // do default
  };

switch (mode) {
  case 0:
  case 1:
    //  mode 0 and 1 are identical
    break;
  case 2:
    // do mode 2
    break;
  default:
    // do default
    break;
  };

But the switch doesn't allow you to do compound ANDs. And you can see what happens if you forget to put in the break; -- you get the OR situation. For those reasons, many people do not like the switch statement. I find them handy for simple logical tests.

Also some people use tricky side effects of assignments within logical evaluations that do not work with switch statements.

And lastly, good warning on the >=. I also like to use the mod operator to handle the rollover case:

const int maxModes = 10;

int mode = 0;

//  then in the loop...

mode  = (mode + 1) % maxModes;

The "%" operator does division modulo a divisor: http://arduino.cc/en/Reference/Modulo

Thanks Everyone! This is exactly what I was looking for.

I have a few options here, I think im going to try the Switch Setup, because ive never seen it like that before.

If that doesn't work out, i will try some others.

I will print this out as soon as i get home and try to work this out.

Again, Thank You everyone!