아두이노 ULTRA SONIC 소스코드

    아두이노 ULTRA SONIC 소스코드 1

     

    void callUltrasonic()
    {
      digitalWrite(TRIGGER_PIN, LOW);
      delayMicroseconds(2);
      digitalWrite(TRIGGER_PIN, HIGH);
      delayMicroseconds(10);
      digitalWrite(TRIGGER_PIN, LOW);
      long duration = pulseIn(ECHO_PIN, HIGH);  /*high가들어올때까지 기다림*/
      const unsigned long distance_mm = (duration/ 2.9)/2;
    /**
    *  거리가 200mm 인 경우 아랫코드 실행
     */ 
     if(distance_mm > 100UL && distance_mm < 1000UL)
     {
      Serial.println("Alert");
      servo.write(90);
     }
      Serial.println(distance_mm);
      lcd.print(distance_mm);
      BT.println(distance_mm);
    }

     

    아두이노 ULTRA SONIC 소스코드 2

    #include <Servo.h>
    #include <LiquidCrystal_I2C.h>
    #include <SoftwareSerial.h>
    
    const int ECHO_PIN = 12;
    const int TRIGGER_PIN = 13;
    const int Water_Sensor = 4;
    
    Servo servo = Servo();
    SoftwareSerial BT = SoftwareSerial(2,3);
    LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16,2);
    
    
    void setup() {
      // put your setup code here, to run once:
      lcd.init();
      lcd.begin(16, 2);
      lcd.clear();
      lcd.backlight();
      Serial.begin(115200UL);
      BT.begin(9600UL);
      servo.attach(9);
      pinMode(TRIGGER_PIN, OUTPUT);
      pinMode(ECHO_PIN, INPUT);
    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    callUltrasonic();
    }

    '코딩 > Arduino(c++)' 카테고리의 다른 글

    아두이노 LCD 소스코드  (0) 2019.11.29
    아두이노 CDS 조도센서 소스코드  (0) 2019.11.28
    아두이노 Serial 처음 시작  (0) 2019.11.26
    아두이노 RGB_LED 소스코드  (0) 2019.11.25
    이떄까지 해본 아두이노  (0) 2019.05.22

    댓글

    Designed by JB FACTORY