Advertisement
Guest User

Sensores

a guest
Nov 18th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.09 KB | None | 0 0
  1. static const int sensor1 = 7;
  2. static const int sensor2 = 8; //voy a suponer que coloque mal este rastreador
  3.  
  4. void setup() {
  5.   Serial.begin(9600);
  6. }
  7.  
  8. void loop() {
  9.   long duracion1, cm1;
  10.   long duracion2, cm2;
  11.  
  12.   duracion1 = ActivarSensor(sensor1);
  13.   duracion2 = ActivarSensor(sensor2);
  14.   // convertimos el tiempo en distancia!!!
  15.   cm1 = microsegundosACentimetros(duracion1);
  16.   cm2 = microsegundosACentimetros(duracion2);
  17.   Serial.print(cm1);
  18.   Serial.print("cm de sensor 1");
  19.   Serial.println();
  20.   Serial.print(cm2);
  21.   Serial.print("cm de sensor 2");
  22.   Serial.println();
  23.  
  24.   long diferencia = cm1-cm2;
  25.   Serial.print("Hay un error en la captura de: ");
  26.   Serial.print(abs(diferencia));
  27.   Serial.print("cm.");
  28.   Serial.println();
  29.  
  30.   delay(100);
  31. }
  32.  
  33. long microsegundosACentimetros(long microseconds) {
  34.   return microseconds / 29 / 2;
  35. }
  36.  
  37. long ActivarSensor(int pin)
  38. {
  39.   pinMode(pin, OUTPUT);
  40.   digitalWrite(pin, LOW);
  41.   delayMicroseconds(2);
  42.   digitalWrite(pin, HIGH);
  43.   delayMicroseconds(5);
  44.   digitalWrite(pin, LOW);
  45.   pinMode(pin, INPUT);
  46.   return pulseIn(pin, HIGH);
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement