Jumat, Juli 05, 2013

Controlling Led With More Than One Input Using OR and AND Logic Using Arduino

The circuit is still same with Controlling Led with more than 1 input using OR or AND logic. Now we can try to vary of using of OR and AND logic. At this example we want to use 4 inputs (the inputs are still push button), we will make programming so that Led will ON when 2 of 4 push buttons are pressed.

The sketch is as follows:

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

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

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


Now, friends already can vary it, can't you...?

The sketch above is controlling Led in which Led will ON if AT LEAST 2 push buttons are pressed, it means if 3 push buttons are pressed or 4 push buttons are pressed then LED will still ON, how if we want LED will ON if only 2 push buttons are pressed? so if 1 push buttons is pressed, or 3 push buttons are pressed or 4 push buttons are pressed then LED will OFF, approximately the what is the sketch like...?

Its logic is just easy, suppose Led will ON only if PB (push button) 1 and PBS 2 are pressed, then its logic is Led will ON if PB1 has HIGH value, PB2 has HIGH value, PB3 has LOW value, and PB4 has LOW value, and like that so on... Sketch for controlling Led in which Led will ON if only 2 PB are pressed is as follows:

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

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

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


ok friends, not difficult aren't you? just need little precision? hehe... : )

For more detail, please see this video below:


Happy trying...

Assalamu 'alaikum : )

0 komentar: