MINI Projact
MINI Projact
Code
int trigger = 8;
int echo = 9;
int relay = 7;
float time=0,distance=0;
void setup()
{
Serial.begin(9600);
pinMode(trigger,OUTPUT);
pinMode(echo,INPUT);
pinMode(relay,OUTPUT);
}
void loop()
{
GGWP();
if(distance<10)
{
digitalWrite(relay,LOW);
}
else
{
digitalWrite(relay,HIGH);
}
delay(1000);
}
void GGWP()
{
digitalWrite(trigger,LOW);
delayMicroseconds(2);
digitalWrite(trigger,HIGH);
delayMicroseconds(10);
digitalWrite(trigger,LOW);
delayMicroseconds(2);
time=pulseIn(echo,HIGH);
distance=time*340/20000;
}
อุปกรณ์ที่ใช้
1.Arduino UNO R3
2.HCSR04 – Ultrasonic Sensor
3.Relay
ความคิดเห็น
แสดงความคิดเห็น