Sabtu, Mei 18, 2013

Controlling Led With Morethan 1 Input With AND Logic Using Arduino

Its circuit is exact same with article on controlling Led with more than one input with Arduino using OR logic. Different thing is only on the sketch, we use "||" code for OR logic, as while we use "&&" code for AND logic.

For using 2 inputs (push button) the sketch as follow:

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);
  }
}


The result is if both of push buttons are pressed, then Led will ON

For using 3 inputs (push button), the sketch are follow:

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 && val2 == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}


The result is if three push buttons are pressed, then Led will ON

Now, we have been able to make sketch for 4 push buttons, or 5, 6, 7, and so on... :D

For more detail, please see this video below:


Please contact sahabat if there is something understood less..., may this simple post have useful... ^_^

Assalamu 'alaikum : )

0 komentar: