X-Git-Url: http://git.piffa.net/web?p=rover;a=blobdiff_plain;f=prototypes%2Fultra%2Fcheck_func%2Fcheck_func.ino;fp=prototypes%2Fultra%2Fcheck_func%2Fcheck_func.ino;h=40481b1e86d6d5a0e484358d7df6b3d559a06254;hp=0000000000000000000000000000000000000000;hb=ead54f309e696c5befa132582375f54dd97b8b10;hpb=bf3cd5f43d63b7cf20a5cd37002fea4d246dcd89 diff --git a/prototypes/ultra/check_func/check_func.ino b/prototypes/ultra/check_func/check_func.ino new file mode 100644 index 0000000..40481b1 --- /dev/null +++ b/prototypes/ultra/check_func/check_func.ino @@ -0,0 +1,58 @@ +/* Ultrasonic rilevatore distanza + +Rilevatore distanza minore di 5cm. +funzione + +HC-SR04 Ping distance sensor +VCC to arduino 5v - GND to arduino GND + + */ +# define DEBUG + +// Ultrasuoni +const byte trig = 11; +const byte echo = 12; +const byte led = 13; +long duration, distance; +boolean allarm = 0; + +void setup() { + pinMode(trig, OUTPUT); + pinMode(echo, INPUT); + pinMode(led, OUTPUT); + + + //Debug + Serial.begin (9600); +} + +void loop() { +if (check()) { + digitalWrite(led,HIGH); +} else { + digitalWrite(led,LOW); +} + +#ifdef DEBUG +Serial.begin(9600); +#endif + +} + +boolean check() { + digitalWrite(trig, LOW); // Prepare for ping + delayMicroseconds(2); // + digitalWrite(trig, HIGH); // Send a ping + delayMicroseconds(10); // + digitalWrite(trig, LOW); // Set down ping + duration = pulseIn(echo, HIGH); + distance = (duration/2) / 29.1; // Speed is ~300m/s, + // so it takes ~29.1 milliseconds for a cm. + // Distance is half of (out + back) + if (distance < 5) { // This is where the LED On/Off happens + return 1; + } + else { + return 0; + } +}