by Jsheng » Sat Apr 21, 2018 9:23 am
THIS IS CODING (INFRARED SENSOR)...
//input
int sen1 = 2;
int sen2 = 3;
int sen3 = 4;
int sen4 = 5;
//output
int lampu1 = 8;
int lampu2 = 9;
int lampu3 = 10;
int lampu4 = 11;
int lampu5 = 12;
int sensorState = LOW;
int val = 0;
void setup()
{
pinMode(sen1, INPUT);
pinMode(sen2, INPUT);
pinMode(sen3, INPUT);
pinMode(sen4, INPUT);
pinMode(lampu1, OUTPUT);
pinMode(lampu2, OUTPUT);
pinMode(lampu3, OUTPUT);
pinMode(lampu4, OUTPUT);
pinMode(lampu5, OUTPUT);
Serial.begin(9600);
}
void loop() {
val = digitalRead(sen1); // read input value
if (val == LOW) { // check if the input is HIGH
digitalWrite(lampu1, LOW);
digitalWrite(lampu2, LOW);
digitalWrite(lampu3, LOW);
digitalWrite(lampu4, LOW);
digitalWrite(lampu5, LOW);
if (sensorState == LOW) {
Serial.println("sensor detected!");
sensorState = HIGH;
}
delay(6000);
digitalWrite(lampu1, HIGH);
digitalWrite(lampu2, HIGH);
digitalWrite(lampu3, LOW);
digitalWrite(lampu4, HIGH);
digitalWrite(lampu5, HIGH);
if (sensorState == HIGH) {
Serial.println("sensor ended!");
sensorState = LOW;
}
}
if (digitalRead(sen2) == LOW){
digitalWrite(lampu1, LOW);
digitalWrite(lampu2, LOW);
digitalWrite(lampu3, LOW);
digitalWrite(lampu4, LOW);
digitalWrite(lampu5, LOW);
delay(6000);
digitalWrite(lampu1, HIGH);
digitalWrite(lampu2, HIGH);
digitalWrite(lampu3, LOW);
digitalWrite(lampu4, HIGH);
digitalWrite(lampu5, HIGH);
}
if (digitalRead(sen3) == LOW){
digitalWrite(lampu1, LOW);
digitalWrite(lampu2, LOW);
digitalWrite(lampu3, LOW);
digitalWrite(lampu4, LOW);
digitalWrite(lampu5, LOW);
delay(6000);
digitalWrite(lampu1, HIGH);
digitalWrite(lampu2, HIGH);
digitalWrite(lampu3, LOW);
digitalWrite(lampu4, HIGH);
digitalWrite(lampu5, HIGH);
}
if (digitalRead(sen4) == LOW){
digitalWrite(lampu1, LOW);
digitalWrite(lampu2, LOW);
digitalWrite(lampu3, LOW);
digitalWrite(lampu4, LOW);
digitalWrite(lampu5, LOW);
if (sensorState == LOW) {
Serial.println("sensor detected!");
sensorState = HIGH;
}
delay(6000);
digitalWrite(lampu1, HIGH);
digitalWrite(lampu2, HIGH);
digitalWrite(lampu3, LOW);
digitalWrite(lampu4, HIGH);
digitalWrite(lampu5, HIGH);
if (sensorState == HIGH) {
Serial.println("sensor ended!");
sensorState = LOW;
}
}
}