วันพุธที่ 14 มีนาคม พ.ศ. 2561

ส่งงานครั้งที่ 8

Code โปรเจค Arduino เปิด ปิดไฟด้วยเสียง


int sound_sensor = 4;
int relay = 5;

int clap = 0; //กำหนดตัวแปรว่าง
long detection_range_start = 0; // กำหนดตัวแปร detection_range_start ให้มีค่า = 0
long detection_range = 0;
boolean status_lights = false; // กำหนดตัวแปร status_lights มีค่าเป็น false หรือ 0

void setup() {
  pinMode(sound_sensor, INPUT);
  pinMode(relay, OUTPUT);
}

void loop() {
  int status_sensor = digitalRead(sound_sensor); //กำหนดตัวแปร status_sensor อ่านค่าดิจิตอลจาก sound_sensor
  if (status_sensor == 0)
  {
    if (clap == 0)
    {
      detection_range_start = detection_range = millis();  //  detection_range_start เท่ากับ  detection_range
      clap++; //เพิ่ม clap ทีละ 1
    }
    else if (clap > 0 && millis()-detection_range >= 50) // ต้องมากกว่าหรือเท่ากับ 500
    {
      detection_range = millis();
      clap++;
    }
  }
  if (millis()-detection_range_start >= 400) // 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;
  }
}

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

แสดงความคิดเห็น

Mini Project

 Control Lamp 220v  With LDR const int lamp = 12; const int ldrPin=A0; #include <Wire.h> #include <LiquidCrystal_I2C.h&g...