Advertisement
AnindyaBiswas

Ultrasound

Feb 27th, 2023
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. int trigger = 7;
  2. int echo = 6;
  3.  
  4. void setup() {
  5.   // put your setup code here, to run once:
  6.   pinMode(trigger, OUTPUT);
  7.   pinMode(echo, INPUT);
  8.   Serial.begin(9600);
  9.   pinMode(11, OUTPUT);
  10. }
  11.  
  12. void loop() {
  13.   // put your main code here, to run repeatedly:
  14.   digitalWrite(trigger, LOW);
  15.   delayMicroseconds(2);
  16.   digitalWrite(trigger, HIGH);
  17.   delayMicroseconds(10);
  18.   digitalWrite(trigger, LOW);
  19.  
  20.   long time = pulseIn(echo, HIGH);
  21.  
  22.   long distance = time * 0.034 / 2;
  23.  
  24.   Serial.println(distance, 3);
  25.   Serial.println(distance % 256, 3);
  26.  
  27.  
  28.   analogWrite(11, distance % 256);
  29.  
  30.   delay(300);
  31.  
  32. }
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement