switch case question

Hi

I am trying to get the button to turn on a corresponding led stay on and then on a button press turn off the switch code example works fine for one button, one led. whatt im trying to do is do the same but with 8 buttons and 8 leds through an array(but in the example only have 2buttons 2 leds)is there a way to make it work without the same piece of code copyed 8 time for example with a switch case statement.
here is what i have so far,if anyone can help that would be great.

Greg

/* switch
 * 
 * Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 * press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 * a minimum delay between toggles to debounce the circuit (i.e. to ignore
 * noise).  
 *
 * David A. Mellis
 * 21 November 2006
 */

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin
int butpin = 3;
int outpin2 = 10;
int state = HIGH;      // the current state of the output pin
int reading [2];          // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin
int buttons [2] = { 2, 3 };
int leds [2] = { 10, 13 };
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
  for(int i=0; i<2; i++) pinMode(leds[i], OUTPUT);
for(int i=0; i<2; i++) pinMode(buttons[i], INPUT);
 // pinMode(inPin, INPUT);
 // pinMode(outPin, OUTPUT);
}

void loop()
{
   for(int i=0; i<2; i++){
  reading[i] = digitalRead(buttons[i]);

  // if the input just went from LOW and HIGH and we've waited long enough
  // to ignore any noise on the circuit, toggle the output pin and remember
  // the time
  if (reading[i] == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(leds[i], state);

  previous = reading[i];
}
}

You've only got one previous state. You need one per input.

thanks would this be right?int previous [2] = { LOW, LOW };

that's correct assuming that index in the array corresponds to a LED. For e.g. previous[0] represent one LED, previous[2] represent another, etc

Why the hating on previous[1]? Especially since in the program fragment given, there is no previous[2].

typo it should be previous[0] and previous[1] not 2.

What would happen if you changed these statement:

int buttons [2] = { 2, 3 };


    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

to these:

int buttons [] = { 2, 3 };   // Let the compiler count it


      state = !state;        // Flip the state

In the first statement, compilers are really good at counting, so why not let the compiler to the counting for you? With two elements, it doesn't make much difference, but with long lists it does. Also, it makes sizing automatic if you add or delete an element or two. The second change is just a standard idiom for flipping the logic state of a variable.

while it is probably safe in this case, what if HIGH = 1 and LOW = -1?

KeithRB: while it is probably safe in this case, what if HIGH = 1 and LOW = -1?

This is an Arduino Forum, hence I think it's safe here most of the time.

Thank you for all the replies, but the code doesn’t seam to be working the way i planed button 1 when pressed turns on both leds and button 2 turns on both leds is there a way to turn on led 1 with button1 and led2 with button2 in the array.

/* switch
 * 
 * Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 * press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 * a minimum delay between toggles to debounce the circuit (i.e. to ignore
 * noise).  
 *
 * David A. Mellis
 * 21 November 2006
 */

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin
int butpin = 3;
int outpin2 = 10;
int state = HIGH;      // the current state of the output pin
int reading [2];          // the current reading from the input pin
int previous [2] = { LOW, LOW };    // the previous reading from the input pin
int buttons [2] = { 2, 3 };
int leds [2] = { 10, 13 };
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
 for(int i=0; i<2; i++) pinMode(leds[i], OUTPUT);
 for(int i=0; i<2; i++) pinMode(buttons[i], INPUT);
 // pinMode(inPin, INPUT);
 // pinMode(outPin, OUTPUT);
}

void loop()
{
 for(int i=0; i<2; i++){
 reading[i] = digitalRead(buttons[i]);
 
 // if the input just went from LOW and HIGH and we've waited long enough
 // to ignore any noise on the circuit, toggle the output pin and remember
 // the time
 if (reading[i] == HIGH && previous[i] == LOW && millis() - time > debounce) {
 if (state == HIGH)
 state = LOW;
 else
 state = HIGH;
 
 time = millis();    
 }
 
 digitalWrite(leds[i], state);
 
 previous[i] = reading[i];
 }
}

Here is your loop without comments and nicer formatting. Play it through in your head and pay careful attention to what happens to the value of state once it changes to LOW. Among other things.

void loop()
{
  for (int i = 0; i < 2; i++)
  {
    reading[i] = digitalRead(buttons[i]);
    if (reading[i] == HIGH && previous[i] == LOW && millis() - time > debounce)
    {
      if (state == HIGH)
        state = LOW;
      else
        state = HIGH;
      time = millis();
    }
    digitalWrite(leds[i], state);
    previous[i] = reading[i];
  }
}

Im not following at all can you explain a little more please?

In that logic, once state equals LOW it can never be HIGH again.

@Jimmy,

      if (state == HIGH)
        state = LOW;
      else
        state = HIGH;

Assume state == LOW.

the if clause state == HIGH is false, so the else block is executed. As a result, state is set to HIGH.

What is the problem, again?

thanks for the reply

What is the problem, again?

The problem is this code will turn the leds on with both buttons what i want it to do is turn one led on with one button and the other led on with the other button.

Make state an array.

would this be right state[2] = {HIGH, HIGH};

gregchill: would this be right state[2] = {HIGH, HIGH};

No, you haven't specified a data type.

You make nice variables for the pins and then throw them away:

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin
int butpin = 3;
int outpin2 = 10;
int state = HIGH;      // the current state of the output pin
int reading [2];          // the current reading from the input pin
int previous [2] = { LOW, LOW };    // the previous reading from the input pin
int buttons [2] = { 2, 3 };
int leds [2] = { 10, 13 };

Your time variables should be unsigned long, not just long.

it seams to be working with the following code not sure if this is the right way to do it?

No, you haven’t specified a data type.

not sure what you mean?

/* switch
 * 
 * Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
 * press), the output pin is toggled from LOW to HIGH or HIGH to LOW.  There's
 * a minimum delay between toggles to debounce the circuit (i.e. to ignore
 * noise).  
 *
 * David A. Mellis
 * 21 November 2006
 */

int inPin = 2;         // the number of the input pin
int outPin = 13;       // the number of the output pin
int butpin = 3;
int outpin2 = 10;
int state[2] = { HIGH, HIGH };      // the current state of the output pin
int reading [2];          // the current reading from the input pin
int previous [2] = { LOW, LOW };    // the previous reading from the input pin
int buttons [2] = { 2, 3 };
int leds [2] = { 10, 13 };
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 200;   // the debounce time, increase if the output flickers

void setup()
{
	for(int i=0; i<2; i++) pinMode(leds[i], OUTPUT);
		for(int i=0; i<2; i++) pinMode(buttons[i], INPUT);
			// pinMode(inPin, INPUT);
	// pinMode(outPin, OUTPUT);
}

void loop()
{
	for(int i=0; i<2; i++){
		reading[i] = digitalRead(buttons[i]);
		
		// if the input just went from LOW and HIGH and we've waited long enough
		// to ignore any noise on the circuit, toggle the output pin and remember
		// the time
		if (reading[i] == HIGH && previous[i] == LOW && millis() - time > debounce) {
			if (state[i] == HIGH)
				state[i] = LOW;
			else
				state[i] = HIGH;
			
			time = millis();    
		}
		
		digitalWrite(leds[i], state[i]);
		
		
		
		previous[i] = reading[i];
		
		
	}
	delay(50);
}