Need very simple help with push button

Hi,

So i'm new to Arduino and just start learning.

I can do this easily

void setup() { pinMode(2, INPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); }

void loop() { // read the switch input: if (digitalRead(2) == HIGH) { // if the switch is closed: digitalWrite(3, HIGH); digitalWrite(4, LOW); } else { // if the switch is open: digitalWrite(3, LOW); digitalWrite(4, HIGH); } }

but when i try to put 2 push pin with this code

void setup() { pinMode(2, INPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, INPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); }

void loop() { // read the switch input: if (digitalRead(2) == HIGH) { // if the switch is closed: digitalWrite(3, HIGH); digitalWrite(4, LOW); }

if (digitalRead(5) == HIGH) {

digitalWrite(6, HIGH); digitalWrite(7, LOW); }

else { // if the switch is open: digitalWrite(3, LOW); digitalWrite(4, HIGH); digitalWrite(6, LOW); digitalWrite(7, HIGH); } }

it doesnt work well

here is a video: http://youtu.be/qeMQI2vr410

The code is the same for INPUT 2 and 5 but they act different?

Here's how I connected the Arduino and the breadboard: The resistors for the LEDs are 470 ohms The resistors for the push buttons are 10K

Thank you for your help!

Why only the input 5 have a 'else'?

luisilva: Why only the input 5 have a 'else'?

Thank you so much! I knew there was something wrong with the code but since i didnt write in C a long time ago i could not find my mistake!

luisilva: Why only the input 5 have a 'else'?

Here's the code with input 2 having a 'else' if it can help someone:

void setup() { pinMode(2, INPUT); pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, INPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); }

void loop() { // read the switch input: if (digitalRead(2) == HIGH) { // if the switch is closed: digitalWrite(3, HIGH); digitalWrite(4, LOW); }

else { // if the switch is open: digitalWrite(3, LOW); digitalWrite(4, HIGH); }

if (digitalRead(5) == HIGH) {

digitalWrite(6, HIGH); digitalWrite(7, LOW); }

else { // if the switch is open: digitalWrite(6, LOW); digitalWrite(7, HIGH); } }

If you look at your program one of the outputs always equals the correspond switch input and the other is the inverse of it

Try

void loop()
{
  digitalWrite(3, digitalRead(2));
  digitalWrite(4, !digitalRead(2));

  digitalWrite(6, digitalRead(5));
  digitalWrite(7, !digitalRead(5));
}

This compiles and shows what you can do just with arrays and loops.
Would you rather chisel code in stone or carve it with power tools?

const byte numInputs = 2;
const byte numOutputs = 4;
const byte inputPins[numInputs] = { 
  2, 5 };
const byte outputPins[numOutputs] = { 
  3, 4, 6, 7 };

void setup() 
{
  for ( byte i = 0; i < numInputs; i++ )
  {
    pinMode( i, INPUT );
  }  
  for ( byte i = 0; i < numOutputs; i++ )
  {
    pinMode( i, OUTPUT );
  }  
}

void loop() 
{
  static byte i = 0;
  static byte temp; 

  if ( digitalRead( inputPins[ i ]))
  {
    digitalWrite( i+1, HIGH);    
    digitalWrite( i+2, LOW);
  }
  else
  {
    digitalWrite( i+1, LOW );    
    digitalWrite( i+2, HIGH );
  } 

  if ( ++i >= numInputs ) // increments i then compares to numInputs
  {
    i = 0;
  }
}

ps kaboche, if the inputs are buttons and the circuit is breadboard then consider changing the input mode to INPUT_PULLUP, wire pin to switch to GND. The chip has a built-in pullup resistor of 20K to 50k, in that mode the pin is ground-safe. My Q&D button is a jumper grounded on the bare metal USB box.