ใบงานที่2
ระบบเปิดปิดถังขยะอัตโนมัติ
Code Program Arduino
#define pingTrig 6
#define pingEcho 7
#include <Servo.h>
Servo myservo;
void setup()
{
pinMode(pingTrig, OUTPUT);
pinMode(pingEcho, INPUT);
myservo.attach(9);
}
void loop()
{
long duration, inches, cm;
digitalWrite(pingTrig, LOW);
delayMicroseconds(2);
digitalWrite(pingTrig, HIGH);
delayMicroseconds(10);
digitalWrite(pingTrig, LOW);
duration = pulseIn(pingEcho, HIGH);
cm = duration / 29 / 2;
if(cm < 10 ){
myservo.write(90);
delay(100);
}else{
myservo.write(0);
}
delay(1000);
}
Code Program Arduino
#define pingTrig 6
#define pingEcho 7
#include <Servo.h>
Servo myservo;
void setup()
{
pinMode(pingTrig, OUTPUT);
pinMode(pingEcho, INPUT);
myservo.attach(9);
}
void loop()
{
long duration, inches, cm;
digitalWrite(pingTrig, LOW);
delayMicroseconds(2);
digitalWrite(pingTrig, HIGH);
delayMicroseconds(10);
digitalWrite(pingTrig, LOW);
duration = pulseIn(pingEcho, HIGH);
cm = duration / 29 / 2;
if(cm < 10 ){
myservo.write(90);
delay(100);
}else{
myservo.write(0);
}
delay(1000);
}
ความคิดเห็น
แสดงความคิดเห็น