Blogger Jateng

How to create two way traffic signal using arduino nano

 silahkan ketik sketch program Arduino dibawah ini:

#define trig1 2
#define echo1 3
#define trig2 4
#define echo2 5
#define trig3 6
#define echo3 7
#define trig4 8
#define echo4 9
#define led1 A2
#define led2 A3
#define led3 A4
#define led4 A5
long durasi;
float cm, inch;void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(trig1, OUTPUT);
pinMode(echo1, INPUT);
pinMode(trig2, OUTPUT);
pinMode(echo2, INPUT);
pinMode(trig3, OUTPUT);
pinMode(echo3, INPUT);
pinMode(trig4, OUTPUT);
pinMode(echo4, INPUT);
digitalWrite(trig1, LOW);
digitalWrite(trig2, LOW);
digitalWrite(trig3, LOW);
digitalWrite(trig4, LOW);
}void loop() {

sensor1();
sensor2();
sensor3();
sensor4();
}

void sensor1(){
digitalWrite (trig1, 0);
delayMicroseconds(2);
digitalWrite (trig1, 1);
delayMicroseconds(10);
digitalWrite (trig1, 0);
delayMicroseconds(2);

durasi = pulseIn(echo1, HIGH);
cm = (durasi * 0.0343)/2;
inch = cm / 2.54;

if (cm >= 5){
digitalWrite(led1, 0);
} else { // selain itu maka
digitalWrite(led1, 1);
}
Serial.print(cm);
Serial.print(" Cm");
Serial.print("\t");
Serial.print(inch);
Serial.println(" Inch");
delay(50);
}
void sensor2(){
digitalWrite (trig2, 0);
delayMicroseconds(2);
digitalWrite (trig2, 1);
delayMicroseconds(10);
digitalWrite (trig2, 0);
delayMicroseconds(2);

durasi = pulseIn(echo2, HIGH);
cm = (durasi * 0.0343)/2;
inch = cm / 2.54;

if (cm >= 5){
digitalWrite(led2, 0);
} else {
digitalWrite(led2, 1);
}
Serial.print(cm);
Serial.print(" Cm");
Serial.print("\t");
Serial.print(inch);
Serial.println(" Inch");
delay(50);
}
void sensor3(){
digitalWrite (trig3, 0);
delayMicroseconds(2);
digitalWrite (trig3, 1);
delayMicroseconds(10);
digitalWrite (trig3, 0);
delayMicroseconds(2);

durasi = pulseIn(echo3, HIGH);
cm = (durasi * 0.0343)/2;
inch = cm / 2.54;

if (cm >= 5){
digitalWrite(led3, 0);
} else {
digitalWrite(led3, 1);
}
Serial.print(cm);
Serial.print(" Cm");
Serial.print("\t");
Serial.print(inch);
Serial.println(" Inch");
delay(50);
}
void sensor4(){
digitalWrite (trig4, 0);
delayMicroseconds(2);
digitalWrite (trig4, 1);
delayMicroseconds(10);
digitalWrite (trig4, 0);
delayMicroseconds(2);

durasi = pulseIn(echo4, HIGH);
cm = (durasi * 0.0343)/2;
inch = cm / 2.54;

if (cm >= 5){
digitalWrite(led4, 0);
} else {
digitalWrite(led4, 1);
}
Serial.print(cm);
Serial.print(" Cm");
Serial.print("\t");
Serial.print(inch);
Serial.println(" Inch");
delay(50);
}

Jika ingin mendownload filenya disini





Posting Komentar untuk "How to create two way traffic signal using arduino nano"