Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define ECHO_PIN 2
- #define TRIG_PIN 3
- #define BUZZER_PIN 4
- #define CLK_PIN 13
- #define DATA_PIN 11
- #define CS_PIN 10
- bool stop = false;
- #include <MD_Parola.h>
- #include <MD_MAX72xx.h>
- #include <SPI.h>
- const uint16_t WAIT_TIME = 1000;
- // Define the number of devices we have in the chain and the hardware interface
- // NOTE: These pin numbers will probably not work with your hardware and may
- // need to be adapted
- #define HARDWARE_TYPE MD_MAX72XX::FC16_HW
- #define MAX_DEVICES 4
- // Hardware SPI connection
- MD_Parola P = MD_Parola(HARDWARE_TYPE, CS_PIN, MAX_DEVICES);
- // Arbitrary output pins
- // MD_Parola P = MD_Parola(HARDWARE_TYPE, DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
- void setup(){
- P.begin();
- Serial.begin(9600);
- pinMode(TRIG_PIN, OUTPUT);
- pinMode(ECHO_PIN, INPUT);
- pinMode(BUZZER_PIN, OUTPUT);
- digitalWrite(TRIG_PIN, LOW);
- }
- void loop(){
- int distanza;
- for(int i=0; i<10; i++){
- distanza += getDistanza();
- }
- distanza = distanza/10;
- Serial.println("Distanza: " + String(distanza) + "cm");
- if(distanza<=20){
- P.print(" STOP");
- if(stop==false){
- for(int i=0; i<10; i++){
- tone(BUZZER_PIN, 1500, 500);
- delay(1000);
- }
- }
- stop=true;
- }else{
- stop=false;
- P.print(String(distanza) + " cm");
- }
- Serial.println("Distanza: " + String(distanza) + "cm");
- delay(200);
- }
- int getDistanza(){
- int distanza;
- unsigned long tempo;
- digitalWrite(TRIG_PIN, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIG_PIN, LOW);
- tempo = pulseIn(ECHO_PIN, HIGH);
- distanza = (0.033765 * tempo/2);
- delayMicroseconds(10);
- return distanza;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement