Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. #define ECHO_PIN 2
  2. #define TRIG_PIN 3
  3. #define BUZZER_PIN 4
  4. #define CLK_PIN 13
  5. #define DATA_PIN 11
  6. #define CS_PIN 10
  7.  
  8. bool stop = false;
  9.  
  10. #include <MD_Parola.h>
  11. #include <MD_MAX72xx.h>
  12. #include <SPI.h>
  13.  
  14. const uint16_t WAIT_TIME = 1000;
  15.  
  16. // Define the number of devices we have in the chain and the hardware interface
  17. // NOTE: These pin numbers will probably not work with your hardware and may
  18. // need to be adapted
  19. #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
  20. #define MAX_DEVICES 4
  21.  
  22.  
  23.  
  24. // Hardware SPI connection
  25. MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
  26. // Arbitrary output pins
  27. // MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
  28.  
  29. void setup(){
  30. P.begin();
  31. Serial.begin(9600);
  32. pinMode(TRIG_PIN, OUTPUT);
  33. pinMode(ECHO_PIN, INPUT);
  34. pinMode(BUZZER_PIN, OUTPUT);
  35. digitalWrite(TRIG_PIN, LOW);
  36. }
  37.  
  38. void loop(){
  39.  
  40. int distanza;
  41.  
  42. for(int i=0; i<10; i++){
  43. distanza += getDistanza();
  44. }
  45.  
  46. distanza = distanza/10;
  47. Serial.println("Distanza: " + String(distanza) + "cm");
  48.  
  49.  
  50. if(distanza<=20){
  51. P.print(" STOP");
  52. if(stop==false){
  53.  
  54. for(int i=0; i<10; i++){
  55. tone(BUZZER_PIN, 1500, 500);
  56. delay(1000);
  57. }
  58.  
  59. }
  60.  
  61. stop=true;
  62.  
  63. }else{
  64. stop=false;
  65. P.print(String(distanza) + " cm");
  66. }
  67.  
  68. Serial.println("Distanza: " + String(distanza) + "cm");
  69. delay(200);
  70. }
  71.  
  72. int getDistanza(){
  73. int distanza;
  74. unsigned long tempo;
  75.  
  76. digitalWrite(TRIG_PIN, HIGH);
  77. delayMicroseconds(10);
  78. digitalWrite(TRIG_PIN, LOW);
  79.  
  80. tempo = pulseIn(ECHO_PIN, HIGH);
  81. distanza = (0.033765 * tempo/2);
  82. delayMicroseconds(10);
  83.  
  84. return distanza;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement