I am wiring some LED's with buttons to trigger them but i don't know if i have the wiring right?

enter image description here

Code

    /* 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 state = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
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(inPin, INPUT);
  pinMode(outPin, OUTPUT);
}

void loop()
{
  reading = digitalRead(inPin);

  // 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(outPin, state);

  previous = reading;
}

note The code is not set to the pins yet.

  • Its not working or what? The schematic looks right. Only you have 2 inputs and 2 outputs. And in code you define wrong pins – Martynas Oct 22 '14 at 6:13
  • @Martynas So i have the wiring right? – Timothy Oct 22 '14 at 6:22
  • Yes. Only thing. Don't use voltage from batteries 4x1.5=6 for input. Feed arduino from batteries, and use 5V output to feed buttons – Martynas Oct 22 '14 at 6:24
  • @So you mean plug the battery to Ground and Vin then run the 5 volt rail to the breadboard? – Timothy Oct 22 '14 at 6:25
  • 1
    Let us continue this discussion in chat. – Martynas Oct 22 '14 at 6:40

The wiring is correct, but inPin in your code is set to 2, which is D4 on the Nano, while you have your signal wires going to D7 and D6 in your schematic.

Similarly, outPin is set to 13 in your code, which is D9 on the Nano, but your schematic shows the LEDs connected to D5 and D4.

I would also put the resistor going from the other pin on the switch to 5V, having the digital input connected directly to GND, but I'm not sure if that will affect power consumption, so it's probably more just a matter of personal preference.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.