เรียนรู้วิธีควบคุม Arduino ด้วย เซ็นเซอร์เสียง Voice Sound Detection Sensor Module เราจะควบคุม การ เปิด ปิดไฟ AC 220V ด้วยเสียงตบมือ โดยเราจะใช้เซ็นเซอร์ตรวจจับเสียง + รีเลย์ และแสดงผลด้วยไฟ หลอดไฟบ้าน
อุปกรณ์ที่ใช้
การต่อวงจร ระหว่าง Sensor Shield กับ เซ็นเซอร์เสียง
การต่อวงจร ระหว่าง Sensor Shield กับ รีเลย์
โค้ด Arduino
int sound_sensor = 4;
int relay = 5;
int clap = 0;
long detection_range_start = 0;
long detection_range = 0;
boolean status_lights = false;
void setup() {
pinMode(sound_sensor, INPUT);
pinMode(relay, OUTPUT);
}
void loop() {
int status_sensor = digitalRead(sound_sensor);
if (status_sensor == 0)
{
if (clap == 0)
{
detection_range_start = detection_range = millis();
clap++;
}
else if (clap > 0 && millis()-detection_range >= 50)
{
detection_range = millis();
clap++;
}
}
if (millis()-detection_range_start >= 400)
{
if (clap == 2)
{
if (!status_lights)
{
status_lights = true;
digitalWrite(relay, HIGH);
}
else if (status_lights)
{
status_lights = false;
digitalWrite(relay, LOW);
}
}
clap = 0;
}
}
int relay = 5;
int clap = 0;
long detection_range_start = 0;
long detection_range = 0;
boolean status_lights = false;
void setup() {
pinMode(sound_sensor, INPUT);
pinMode(relay, OUTPUT);
}
void loop() {
int status_sensor = digitalRead(sound_sensor);
if (status_sensor == 0)
{
if (clap == 0)
{
detection_range_start = detection_range = millis();
clap++;
}
else if (clap > 0 && millis()-detection_range >= 50)
{
detection_range = millis();
clap++;
}
}
if (millis()-detection_range_start >= 400)
{
if (clap == 2)
{
if (!status_lights)
{
status_lights = true;
digitalWrite(relay, HIGH);
}
else if (status_lights)
{
status_lights = false;
digitalWrite(relay, LOW);
}
}
clap = 0;
}
}
จากนั้นให้เอาไขควงหมุน ปรับค่าการรับสัญญาณเสียง โดยให้หมุนตัว R ปรับค่าได้ แบบ trimpot สีฟ้า



ไม่มีความคิดเห็น:
แสดงความคิดเห็น