Halaman

Pengikut

Jumat, 05 Juli 2013

ControlLed morethan1 Input OrAndLogic 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...?

read more...

Comments :

0 komentar to “ControlLed morethan1 Input OrAndLogic Arduino”

Posting Komentar

 

Copyright © 2009 by coretan sahabat