From ead54f309e696c5befa132582375f54dd97b8b10 Mon Sep 17 00:00:00 2001 From: Andrea Manni Date: Thu, 30 Mar 2017 16:57:52 +0200 Subject: [PATCH] Ultra --- .../servo/rotation_func/rotation_func.ino | 22 ++++--- prototypes/ultra/check_func/check_func.ino | 58 +++++++++++++++++++ .../ultrasonic_distance_simple.ino | 46 +++++++++++++++ 3 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 prototypes/ultra/check_func/check_func.ino create mode 100644 prototypes/ultra/ultrasonic_distance_simple/ultrasonic_distance_simple.ino diff --git a/prototypes/servo/rotation_func/rotation_func.ino b/prototypes/servo/rotation_func/rotation_func.ino index ac463ea..5a660c3 100644 --- a/prototypes/servo/rotation_func/rotation_func.ino +++ b/prototypes/servo/rotation_func/rotation_func.ino @@ -29,12 +29,16 @@ int pos = 0; // variable to store the servo position const byte servo =9 ; const byte middle = 90; // Centratura servo const int spausa = 30; // Pausa movimenti servo +const byte left = 170; +const byte right = 10; void setup() { myservo.attach(servo); // attaches the servo on pin 9 to the servo object - myservo.write(middle); // tell servo to go to position in variable 'pos' - delay(2000); + + // Centratura iniziale + myservo.write(middle); // tell servo to go to position in variable 'pos' + delay(2000); } @@ -48,24 +52,24 @@ void loop() delay(1000); // Turn SX - turnSX(); - delay(1000); - - turnMiddle(); - delay(1000); + turnSX(); + delay(1000); + + turnMiddle(); + delay(1000); } // Functions void turnDX() { // TurnDX - myservo.write(170); + myservo.write(right); delay(spausa); } void turnSX() { // TurnSX - myservo.write(10); + myservo.write(left); delay(spausa); } 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; + } +} diff --git a/prototypes/ultra/ultrasonic_distance_simple/ultrasonic_distance_simple.ino b/prototypes/ultra/ultrasonic_distance_simple/ultrasonic_distance_simple.ino new file mode 100644 index 0000000..dc0e8ad --- /dev/null +++ b/prototypes/ultra/ultrasonic_distance_simple/ultrasonic_distance_simple.ino @@ -0,0 +1,46 @@ +/* Ultrasonic rilevatore distanza + +Rilevatore distanza minore di 5cm. + +HC-SR04 Ping distance sensor +VCC to arduino 5v - GND to arduino GND + + + */ + +// Ultrasuoni +const byte trig = 11; +const byte echo = 12; +const byte led = 13; +long duration, distance; + +void setup() { + pinMode(trig, OUTPUT); + pinMode(echo, INPUT); + pinMode(led, OUTPUT); + + + //Debug + Serial.begin (9600); +} + +void loop() { + 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 + digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off + } + else { + digitalWrite(led,LOW); + } + delay(200); + Serial.println(distance); +} + -- 2.39.2