Senin, Mei 06, 2013

Controlling Led With Morethan 1 Input With OR Logic Using Arduino

If yesterday we could controll Led with a push button, now we want to try controll Led with using 2 push buttons, where LED WILL ON WHEN ONE OF 2 PUSH BUTTONS IS PRESSED.

Normal circuit for controlling Led on Arduino approsimately as follows:

Picture 1
Explanation:
  1. One of digital pin (begin from 0 to 13, at this example we will use digital pin 13)
  2. Resistor
  3. Led
  4. Ground of digital pin (next to digital pin number 13)

As while push button circuit on Arduino as foloows:

Gambar 2
  1. Power pin 5 Volt
  2. Resistor
  3. Push button
  4. One of digital pin (at this example we will use digital pin 2)
  5. Ground of power pin

cause we will now use 2 push buttons then we need make 1 push button circuit again, suppose as follows:

Gambar 3
  1. Power pin 5 Volt
  2. Resistor
  3. Push button
  4. One of digital pin (at this example we will use digital pin 3)
  5. Ground of power pin

Sketch for controlling Led with 2 inputs (push butoon) as follows:

int ledPin = 13;
int inPin1 = 2;
int inPin2 = 3;
int val1 = 0;
int val2 = 0;

void setup {
  pinMode(ledPin, OUTPUT);
  pinMode(inPin1, INPUT);
  pinMode(inPin2, INPUT);
}

void loop() {
  val1 = digitalRead(inPin1);
  val2 = digitalRead(inPin2);
  if (val1 == HIGH || val2 == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}


Its result is if one of 2 push buttons is pressed, then Led will ON

If we want to add one push button again or want to make input be 3 inputs, or in this case total of push button be 3 push buttons, we enought add one push button circuit again, at this example for third push button we use digital pin number 4.

And sketch for controlling Led with 3 inputs (push button) as follows:

int ledPin = 13;
int inPin1 = 2;
int inPin2 = 3;
int inPin3 = 4;
int val1 = 0;
int val2 = 0;
int val3 = 0;

void setup {
  pinMode(ledPin, OUTPUT);
  pinMode(inPin1, INPUT);
  pinMode(inPin2, INPUT);
  pinMode(inPin3, INPUT);
}

void loop() {
  val1 = digitalRead(inPin1);
  val2 = digitalRead(inPin2);
  val3 = digitalRead(inPin3);
  if (val1 == HIGH || val2 == HIGH || val3 == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}


Its result is if one of 3 push buttons is pressed then Led will ON

Now, we can controlling Led with push button with amount of push button we wanted, seem its maximum is only 14push button, but the important point, now we have understood...

For more detail, please see this video below:


Please correction if there is something wrong, and don't hesitate for asking if there is something not understood...

Hopefully useful... ^_^

Assalamu 'alaikum :)

0 komentar: