2 push buttons as switch to control 2 leds,

I want each switch to control its own led, so switch one controls red and switch 2 controls green i am very new to this i just got my arduino have never taken any classes just learning as i go. i got the code from the tutorials for makeing the first switch and thought maybe i could seperate it. i was very wrong.

int swhPin = 2;         // switch pin
int redPin = 13;       // red led
int swh2Pin = 3;       // switch pin #2
int grnPin = 12;       // green led 
int state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int reading2;
int previous = LOW;    // the previous reading from the input pin

// 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()
{
  pinMode(swhPin, INPUT); //switch is an input
  pinMode(redPin, OUTPUT); //red led is an output
  pinMode(swh2Pin, INPUT); //switch2 is an input
  pinMode(grnPin, OUTPUT); //green led is a output
}

void loop()
{
  reading = digitalRead(swhPin); //reading first switch
  reading2 = digitalRead(swh2Pin); //reading second switch

  // 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 == HIGH && previous == LOW && millis() - time > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;

    time = millis();    
  }

  digitalWrite(redPin, state); //changing red pin state to on or off

  previous = reading;
if (reading2 == HIGH && previous ==LOW && millis() - time  > debounce) {
    if (state == HIGH)
      state = LOW;
    else
      state = HIGH;
      
    time = millis();
  }
  
  digitalWrite(grnPin, state); //Changing green pin state to on or off
  
  previous = reading2;
}

both switches ended up controlling both lights and i cant for the life of me figure this out please help me i have looked everywhere and cant understand what im supposed to do none of the tutorials explain this. here is my wiring. i am new to all of this i edited the wiring this is how it really is i was up late when i was creating the sketch on fritzing

You NEED resistors in line with those LEDs, you will burn out your arduino pins otherwise.

Next the switches are wired up wrong. You need to follow the button tutorial in the learning section of the site.

edraker10: have never taken any classes just learning as i go.

Classes are useful.

If you'd taken a class the first thing they would have told you that connecting LEDs to Arduinos without resistors leads to dead LEDs and/or dead Arduino pins.

If you can't remember how those four-leg buttons are wired, use two diaginally opposite legs. That always works.

i know it would be useful im just doing this as a hobby not trying to get any where with it really other then stuff like for wiring lights of push buttons i dont see it going anywhere else :stuck_out_tongue: i have a feeling its most definately my code

i re-posted the wiring diagram

edraker10: i re-posted the wiring diagram

Well it is still wrong. The button must make a circuit to the 5V when pushed for the input. When the button is not pushed the input must make a circuit to ground tyep rough the resistor. You have the 5V and the inputs the wrong way round in that wiring.

In the software I think you need a previous2 to match reading2 ... Havnt checked it through in detail, but it seems an "obvious" error given the structure you have. Have a thought about one "time" variable monitoring two switches. It probably works if you done push them at once...

[/quote] Well it is still wrong. The button must make a circuit to the 5V when pushed for the input. When the button is not pushed the input must make a circuit to ground tyep rough the resistor. You have the 5V and the inputs the wrong way round in that wiring. [/quote] i don't understand at all what you mean, the switches are running the inputs, and its wired exactly like the button tutorial... ground on opposite side of input and 5v diag from input...

the inputs are making a circuit to ground through the resistor it goes through the switch to the ground when switch is no pushed pin 1 and 2 are input to switch opposite of that is the ground through the resistordiagonal of that is the 5 volt

AAAHHHHHHAAAAAAAAA I WIN :P

Had to ad a second of state and previous and it worked fine

edraker10: AAAHHHHHHAAAAAAAAA I WIN :P

Only if you have wired the switches differantly from the diagram you posted.

did nothing of the sort and it works like a dream

edraker10: did nothing of the sort and it works like a dream

Then you success is only temporary because you have what is known as a floating input. When the button is not pressed the input is not connected to anything. It might stop working at any time. The other alternitave is you have not wired it up like that diagram but wired it correctly.

I'm nt understanding how you think the input goes nowhere when switch is not pushed its going to the ground through the resistor as you stated it had to the to pins opposite of each other are always connected... Then when pushed the 5v goes to the input

In which case you should have posted an updated diagram, all we have to go on is your original layout.