printf("ho_tari\n");

10_Ultrasonic 본문

Arduino

10_Ultrasonic

호타리 2023. 9. 2. 12:36
#include <Arduino.h>
#include <TimerOne.h>

// ECHO pin
#define ECHO_INPUT      2
// TRIG pin
#define TRIG_OUTPUT     3
// update interval, make sure to keep it above 20ms
#define ULTRASONIC_TIMER_US       500000 

// the time the pulse was sent
volatile unsigned long ultrasonic_echo_start = 0;
// the distance once it's been measured
volatile unsigned long ultrasonic_distance = 0;

void ultrasonicPulse(){
    digitalWrite(TRIG_OUTPUT, HIGH);
    delayMicroseconds(10);
    digitalWrite(TRIG_OUTPUT, LOW);
}

void ultrasonicEcho(){
    // don't do anything if no pulse has been sent
    int echoVal = digitalRead(ECHO_INPUT);

    if(echoVal == HIGH){  // RISING
      ultrasonic_echo_start = micros(); // 프로그램이 시작하고 나서 
    }
    else{  // FALLING
        // calculate the distance by measuring how long it took to return the sound
        // The speed of sound is 343 m/s and we need half the time it took (since
        // the sound has to travel towards the object _AND_ back). So a single echo does
        // 1/(343/2) = 0.005831 seconds per meter
        ultrasonic_distance = (micros() - ultrasonic_echo_start) / 58;

        //ultrasonic_echo_start = 0;
    }
}

void setup(){
    Serial.begin(115200);
  
    // set the echo pin to receive interrupts, on an arduino uno only pin 2 and 3 can link to interrupts
    pinMode(ECHO_INPUT, INPUT);
    pinMode(TRIG_OUTPUT, OUTPUT);
  
    Timer1.initialize(ULTRASONIC_TIMER_US);
    Timer1.attachInterrupt(ultrasonicPulse);
    attachInterrupt(digitalPinToInterrupt(ECHO_INPUT), ultrasonicEcho, CHANGE);
}

void loop() {
  Serial.print("Distance: ");
  Serial.println(ultrasonic_distance);
  delay(1000);
}

'Arduino' 카테고리의 다른 글

12_Timer1_Piezo  (0) 2023.09.02
11_Timer1_LED  (0) 2023.09.02
09_Tone  (0) 2023.09.02
08_Joystick  (0) 2023.09.02
07_Analog_Read  (0) 2023.09.02