728x90
반응형
const int trig_pin = 11;
const int echo_pin = 12;
const int ACTIVE_BUZZER = 10;
const int ledPin = 8;

void setup()
{
  pinMode(trig_pin, OUTPUT);
  pinMode(echo_pin, INPUT);
  pinMode(ACTIVE_BUZZER, OUTPUT);
  pinMode(ledPin, OUTPUT);

  Serial.begin(115200);
}

void loop()
{
  digitalWrite(trig_pin, LOW);
  delayMicroseconds(2);
  digitalWrite(trig_pin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trig_pin, LOW);

  long duration = pulseIn(echo_pin, HIGH);
  long distance = (duration/2) / 29.1;
  int buttonInput = digitalRead(echo_pin);
  if (distance < 10)
    {
      tone(ACTIVE_BUZZER, 2093, duration);
      delay(1000/10);
      analogWrite(ledPin, 200);
    }
  else
  {
    analogWrite(ledPin, 0);
  }
  
  Serial.print(distance);
  Serial.println("cm");  
}

20221014_114719.mp4
9.61MB

 

728x90
반응형

'Firmware & Embedded > Components' 카테고리의 다른 글

Arduino Serial (to Python)  (0) 2022.10.31
Sequential Brightness LED using Button  (0) 2022.10.14
Ultrasonic sensor  (0) 2022.10.14
BUZZER Control  (0) 2022.09.22
Servo  (0) 2022.09.21

+ Recent posts