Advertisement
pardal_eletrico

IR sensor

Jul 30th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1.  
  2.  
  3. // define pinos. Usei os pinos 4 e 5
  4. #define irLedPin 4 // Ir Led neste pino
  5. #define irSensorPin 5 // Sensor IR neste pino
  6.  
  7. int irRead(int readPin, int triggerPin); // protótipo de função
  8.  
  9. void setup()
  10. {
  11. pinMode(irSensorPin, INPUT);
  12. pinMode(irLedPin, OUTPUT);
  13. Serial.begin(9600);
  14. // imprime o título com intervalo de linha final
  15. Serial.println("Program Starting");
  16. // aguarde que a string longa seja enviada
  17. delay(100);
  18. }
  19.  
  20. void loop()
  21. {
  22. Serial.println(irRead(irSensorPin, irLedPin)); // exibe os resultados
  23. delay(10); //wait for the string to be sent
  24. }
  25.  
  26. /******************************************************************************
  27. * Esta função pode ser usada com um sensor panasonic pna4602m ir
  28.   * Retorna um zero se algo for detectado pelo sensor, e um 1 caso contrário
  29.   * A função bit bangs uma forma de onda de 38.5khZ para um led IR conectado ao
  30.   * TriggerPin durante 1 milissegundo e, em seguida, lê o pino do sensor IR para ver se
  31.   * O IR refletido foi detectado
  32. ******************************************************************************/
  33. int irRead(int readPin, int triggerPin)
  34. {
  35. int halfPeriod = 13; // um período de 38.5khZ é de aproximadamente 26 microssegundos
  36. int cycles = 38; // 26 microseconds * 38 é mais ou menos 1 milissegundo
  37. int i;
  38. for (i=0; i <=cycles; i++)
  39. {
  40. digitalWrite(triggerPin, HIGH);
  41. delayMicroseconds(halfPeriod);
  42. digitalWrite(triggerPin, LOW);
  43. delayMicroseconds(halfPeriod - 1); // - 1 para compensar DigitaWrite sobrecarga
  44. }
  45. return digitalRead(readPin);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement