KingOfWesteros

Aula 03 - Código Sensor de Distância

Apr 21st, 2021
832
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # ---Sensor de Distância---
  2.  
  3. float distancia;
  4. float tempo;
  5.  
  6. void leitura();
  7. void paraFrente();
  8. void paraTras();
  9.  
  10. void setup()
  11. {
  12.   pinMode(13, OUTPUT);
  13.   pinMode(12, OUTPUT);
  14.   pinMode(7, OUTPUT);
  15.   digitalWrite(7, LOW);
  16. }
  17.  
  18. void loop()
  19. {
  20.   leitura();
  21.   tempo = tempo/2; //Tempo de Ida e volta por 2 /Apenas 1 dos tempos
  22.   distancia = tempo * 0.034029; // d = v*t
  23.   delay(1000);
  24.   if ( distancia >= 50 ) {
  25.     paraFrente();
  26.   } else {
  27.     paraTras();
  28.     }
  29. }
  30.  
  31. void leitura() {
  32.   pinMode(7, OUTPUT);
  33.   digitalWrite(7, HIGH);
  34.   delayMicroseconds(5);
  35.   digitalWrite(7, LOW);
  36.  
  37.   pinMode(7, INPUT);
  38.   tempo = pulseIn(7, HIGH);
  39. }
  40.  
  41.   void paraFrente() {
  42.     digitalWrite(13, HIGH);
  43.     digitalWrite(12, LOW);
  44.   }
  45.  
  46.   void paraTras() {
  47.     digitalWrite(13, LOW);
  48.     digitalWrite(12, HIGH);
  49.   }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×