PGLucio

Pablo G. Lucio - Arduino and TouchDesigner Tutorial - Ultrasonic Sensor

Sep 11th, 2021
410
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. // ULTRASONIC SENSOR
  3. int TRIG = 3;
  4. int ECHO = 2;
  5. int DURATION;
  6. int DISTANCE;
  7.  
  8.  
  9.  
  10. void setup() {  
  11.  
  12.   // ULTRASONIC SENSOR
  13.   pinMode(TRIG, OUTPUT);
  14.   pinMode(ECHO, INPUT);
  15.  
  16.   // SERIAL
  17.   Serial.begin(9600);
  18.  
  19. }
  20.  
  21.  
  22. void loop() {
  23.  
  24.   digitalWrite(TRIG,HIGH);
  25.   delay(1);
  26.   digitalWrite(TRIG,LOW);
  27.   DURATION = pulseIn(ECHO,HIGH);
  28.   DISTANCE = DURATION / 58.2;
  29.  
  30.   if(DISTANCE > 0 && DISTANCE < 50 ){
  31.     Serial.println(DISTANCE);
  32.     delay(100);
  33.   }
  34.  
  35. }
RAW Paste Data