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=815e3527a19922d17a2df47db48b9366a690493a;hp=40481b1e86d6d5a0e484358d7df6b3d559a06254;hb=e064d1ab908e7672e00c721a76360276ebcb6d35;hpb=b04cb93a188fc26c518839b956b17e10dcc96de2 diff --git a/prototypes/ultra/check_func/check_func.ino b/prototypes/ultra/check_func/check_func.ino index 40481b1..815e352 100644 --- a/prototypes/ultra/check_func/check_func.ino +++ b/prototypes/ultra/check_func/check_func.ino @@ -7,14 +7,15 @@ HC-SR04 Ping distance sensor VCC to arduino 5v - GND to arduino GND */ -# define DEBUG +# define dEBUG // Ultrasuoni const byte trig = 11; const byte echo = 12; const byte led = 13; -long duration, distance; -boolean allarm = 0; +long duration; +int distance; +const int minDistance = 10; void setup() { pinMode(trig, OUTPUT); @@ -23,36 +24,57 @@ void setup() { //Debug - Serial.begin (9600); +#ifdef DEBUG +Serial.begin(9600); +#endif +Serial.begin(9600); // Need for distanceMonitor } void loop() { -if (check()) { +if (distanceCheck()) { digitalWrite(led,HIGH); } else { digitalWrite(led,LOW); } - -#ifdef DEBUG -Serial.begin(9600); -#endif +Serial.println(distanceMonitor()); +delay(50); } -boolean check() { +boolean distanceCheck() { 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, + //distance = (duration / 2) / 29.1; // Speed is ~300m/s, // so it takes ~29.1 milliseconds for a cm. + distance = (duration / 58.2); // Atmegas are not found of divisions // Distance is half of (out + back) - if (distance < 5) { // This is where the LED On/Off happens +#ifdef DEBUG +Serial.print("Distanza oggetto: "); +Serial.println(distance); +#endif + if (distance < minDistance) { // This is where the LED On/Off happens return 1; } else { return 0; } } + +int distanceMonitor() { + 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 = (duration / 58.2); // Atmegas are not found of divisions + // Distance is half of (out + back) + + return distance; +}