Push button problems

i have two push button that toggle Leds whenever i press it once
button 1 controls LedA, LedB and LedC
button 2 controls LedD, LedE, and LedF
My problem now is that when i press button 1, it can control all less, vice versa
can anyone help me with this problem? :frowning:

Yes we can help you.
Do you think we need to see your code and an image of your circuit?

i`m actually doing a program for washing machine stimulation
here is my code :
int normalled=12;
int delicateled=13;
int heavyled=A0;
int materialpin= 3;
int val=0;
int lowled=A2;
int mediumled=A3;
int highled=A4;

int levelbuttonpushcounter=0;
int buttonstate1=0;
int lastbuttonstate1=0;

int materialbuttonpushcounter=0;
int buttonstate2=0;
int lastbuttonstate2=0;

int startbutton= A1;

void setup() {

pinMode(normalled,OUTPUT);
pinMode(delicateled,OUTPUT);
pinMode(heavyled,OUTPUT);
pinMode(materialpin,INPUT);
pinMode(lowled,OUTPUT);
pinMode(mediumled,OUTPUT);
pinMode(highled,OUTPUT);
pinMode(startbutton,INPUT);
digitalWrite(normalled, LOW);
digitalWrite(delicateled, LOW);
digitalWrite(heavyled, LOW);
digitalWrite(lowled, LOW);
digitalWrite(mediumled, LOW);
digitalWrite(highled, LOW);
}

void loop()
{
// ********************************************material selection **********************************************
buttonstate2= digitalRead(materialpin);
val=digitalRead(startbutton);
if ( val== LOW )
{
if( buttonstate2 != lastbuttonstate2)
{
if(buttonstate2 == HIGH)
{
materialbuttonpushcounter++;

if(materialbuttonpushcounter == 4)
{
materialbuttonpushcounter =1;
}
if( materialbuttonpushcounter==1)
{
digitalWrite(normalled, HIGH);
}
else if(materialbuttonpushcounter==2)
{
digitalWrite(delicateled,HIGH);
}
else if(materialbuttonpushcounter==3)
{
digitalWrite(heavyled,HIGH);
}

}
lastbuttonstate2=buttonstate2;

// **************************************** water level selection **********************************************
buttonstate1= digitalRead(levelpin);
if(buttonstate1 != lastbuttonstate1)
{
if(buttonstate1 == HIGH)
{
levelbuttonpushcounter++;

if(levelbuttonpushcounter ==4)
{
levelbuttonpushcounter=1;
}
if(levelbuttonpushcounter ==1)
{
digitalWrite(lowled, HIGH);
}
if(levelbuttonpushcounter ==2)
{
digitalWrite(mediumled, HIGH);
}
if(levelbuttonpushcounter ==3)
{
digitalWrite(highled, HIGH);
}
}
lastbuttonstate1= buttonstate1;
}
}

}

I don't see any switch debouncing.
Are you using pull-up resistors on the switches ?
What is not working?
You should comment your code.

yes i did used resistors
the problem is that the buttons can control all led, it suppose to only control 3 leds per button
so how do i write out the switch debouncing code?

Here is a library that will handle bounce situations.
http://playground.arduino.cc/Code/Bounce

What is levelpin?

Think about breaking your code into several short functions so you can separate the logic from the actions as in this Thread Planning and Implementing a program

It will make it much easier to see where problems lie.

...R

hanseng3215:
yes i did used resistors
the problem is that the buttons can control all led, it suppose to only control 3 leds per button
so how do i write out the switch debouncing code?

Some trouble you have is that you turn leds on but never off.
You should learn about arrays and loops to save yourself typing and have shorter code.
Do you know how to get to the Example Sketches?

The forum just made a big change of look and feel. It makes editing and explaining difficult.

Another problem is that your code acts on button pin state instead of change of state.
The code runs through loop() in less than a millisecond and seeing the button is still pressed, turns each led on in what appears to human eyes as instantly but is not.
It would be better if you make code to watch each button in turn and determine if the pin state has changed and stayed changed, the same state for some time or many reads in a row. For that you will need not only is it high or low but what was the last stable state.
The speed of the Arduino is 16 million per second, it may read high 10 to 100 times then low 10 to 100 then high and low like that once or a few times in a single press all in a small fraction of a second. Or it may not, depending on the button and the press. And all faster than you can know.
Arduino has a clock but I think that you are not ready for that. But you can read the pin, see if the state has changed and count how many times it has not. If it has not changed in 1000 reads, it should be stable. If the stable pin state is different from the last stable pin state then the stable button state can be safely said to have changed and -then- if it is HIGH you add 1 to the button press count.

Make the led code use the stable state and change of press count as trigger to change the leds.
Make that not inside the button pin check if braces. It should only depend on the variables for stable state and press count.

Code will become easier as you learn more things that you can do. But then you will write more ambitious sketches so overall it will be harder but do more. Funny how that works.

This sketch example should be of some interest. It includes LEDs flashing at independent periods, and a debounced button toggling a LED - you can extend that toggle function easily to multiple states, and duplicate the code for further buttons and LEDs:

// Blink without "delay()" - multi!

const int led1Pin =  13;    // LED pin number
const int led2Pin =  10;
const int led3Pin =  11;
const int button1 =  4;
int led1State = LOW;        // initialise the LED
int led2State = LOW;
int led3State = LOW;
char bstate1 = 0;
unsigned long count1 = 0;   // will store last time LED was updated
unsigned long count2 = 0;
unsigned long count3 = 0;
unsigned long bcount1 = 0; // button debounce timer.  Replicate as necessary.

// Have we completed the specified interval since last confirmed event?
// "marker" chooses which counter to check 
boolean timeout(unsigned long *marker, unsigned long interval) {
  if (millis() - *marker >= interval) { 
    *marker += interval;    // move on ready for next interval
    return true;       
  } 
  else return false;
}

// Deal with a button read; true if button pressed and debounced is a new event
// Uses reading of button input, debounce store, state store and debounce interval.
boolean butndown(char button, unsigned long *marker, char *butnstate, unsigned long interval) {
  switch (*butnstate) {               // Odd states if was pressed, >= 2 if debounce in progress
  case 0: // Button up so far, 
    if (button == HIGH) return false; // Nothing happening!
    else { 
      *butnstate = 2;                 // record that is now pressed
      *marker = millis();             // note when was pressed
      return false;                   // and move on
    }

  case 1: // Button down so far, 
    if (button == LOW) return false; // Nothing happening!
    else { 
      *butnstate = 3;                 // record that is now released
      *marker = millis();             // note when was released
      return false;                   // and move on
    }

  case 2: // Button was up, now down.
    if (button == HIGH) {
      *butnstate = 0;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 1;               // jackpot!  update the state
        return true;                  // because we have the desired event!
      }
      else 
        return false;                 // not done yet; just move on
    }

  case 3: // Button was down, now up.
    if (button == LOW) {
      *butnstate = 1;                 // no, not debounced; revert the state
      return false;                   // False alarm!
    }
    else { 
      if (millis() - *marker >= interval) {
        *butnstate = 0;               // Debounced; update the state
        return false;                 // but it is not the event we want
      }
      else 
        return false;                 // not done yet; just move on
    }
  default:                            // Error; recover anyway
    {  
      *butnstate = 0;
      return false;                   // Definitely false!
    }
  }
}

void setup() {
  pinMode(led1Pin, OUTPUT);      
  pinMode(led2Pin, OUTPUT);      
  pinMode(led3Pin, OUTPUT);      
  pinMode(button1, INPUT);      
  digitalWrite(button1,HIGH);        // internal pullup all versions
}

void loop() {
  // Toggle LED if button debounced
  if (butndown(digitalRead(button1), &bcount1, &bstate1, 10UL )) {
    if (led1State == LOW) {
      led1State = HIGH;
    }
    else {
      led1State = LOW; 
    } 
    digitalWrite(led1Pin, led1State);
  } 

  // Act if the latter time (ms) has now passed on this particular counter,
  if (timeout(&count2, 300UL )) {
    if (led2State == LOW) {
      led2State = HIGH;
    }
    else {
      led2State = LOW; 
    } 
    digitalWrite(led2Pin, led2State);
  } 

  if (timeout(&count3, 77UL )) {
    if (led3State == LOW) {
      led3State = HIGH;
    }
    else {
      led3State = LOW; 
    } 
    digitalWrite(led3Pin, led3State);
  } 
}