--- /dev/null
+/* L298n motor
+
+Inclusione delle funzioni nella libreria rover.
+
+Guida 2WD composta da 2 motori
+
+- 2 motori DC
+- L298n module
+- Batteria > 6v
+
+*/
+
+#include <rover.h>
+#include <Servo.h>
+
+void setup() {
+ abilita();
+}
+
+
+void loop() {
+digitalWrite(13,HIGH);
+
+// Avanti
+ avanti();
+ delay(2000);
+
+digitalWrite(13,LOW);
+
+// Stop
+ stop();
+ delay(1000);
+
+// TurnSX
+ giraSX();
+ delay(2000);
+
+// Stop
+ stop();
+ delay(1000);
+
+// indietro
+ indietro();
+ delay(2000);
+
+// Stop
+ stop();
+ delay(1000);
+
+// TurnDX
+ giraDX();
+ delay(2000);
+
+// Stop
+ stop();
+ delay(1000);
+}
--- /dev/null
+/* Rotazione
+
+ Rotazione di un servomotore tramite la librerio Servo.h .
+
+ L'utilizzo della libreria Servo rende inutilizzabile analogWrite()
+ sui pin 9 e 10 dato che utilizza i timer associati a questi PIN.
+
+ Power: un servo da 9g puo' arrivare ad impegnare 750mA sotto carico
+ (se viene opposta resistenza al movimento del servo), un SG90 prende
+ ~52mA se il movimento e' libero. Quindi in fase di test il servo puo'
+ essere alimentato direttamente da una scheda Arduino (200ma dal PIN 5v)
+ ma per l'uso finale dovra' essere alimentato autonomamente.
+
+
+ Rotazione a SX di 90'
+ Rotazione a DC di 90'
+
+ Schema: https://www.arduino.cc/en/uploads/Tutorial/sweep_bb.png
+ http://microbotlabs.com/images/mearm-uno-servo-1.jpg
+ */
+
+
+#include "rover.h"
+#include "Servo.h"
+
+// a maximum of eight servo objects can be created
+
+
+void setup()
+{
+ abilita();
+
+}
+
+
+void loop()
+{
+ // servo DX
+ servoDX(); // Gira il servo a DX (il verso dipende anche da come e' stato montato)
+ delay(1000);
+
+ servoMiddle(); // Torna al centro
+ delay(1000);
+
+ // servo SX
+ servoSX(); // Gira il servo a SX (il verso dipende anche da come e' stato montato)
+ delay(1000);
+
+ servoMiddle();
+ delay(1000);
+}
+
+
+
+
--- /dev/null
+/* Ultrasonic rilevatore distanza
+
+Rilevatore distanza minore di 5cm.
+funzione
+
+HC-SR04 Ping distance sensor
+VCC to arduino 5v - GND to arduino GND
+
+ */
+#define dEBUG
+#include "rover.h"
+#include "Servo.h"
+
+
+void setup() {
+ abilita();
+ //Debug
+#ifdef DEBUG
+Serial.begin(9600);
+#endif
+Serial.begin(9600); // Need for distanceMonitor
+}
+
+void loop() {
+if (distanceCheck()) { // Verifica se la distanza di un oggetto e' minore di minDistance = 10cm
+ digitalWrite(13,HIGH);
+} else {
+ digitalWrite(13,LOW);
+}
+Serial.println(distanceMonitor()); // Ritorna la distanza di un oggetto in cm
+delay(50);
+}
giraSX KEYWORD2
giraDX KEYWORD2
setSpeed KEYWORD2
+
+
+distanceCheck KEYWORD2
+distanceMonitor KEYWORD2
#define dEBUG
-// Configurazione con OUTPUT digitali
+// Configurazione parametri: variabili GLOBALI
// motor one
const int enA = 6;
const int in1 = 7;
const byte dx = 170; // Maz DX
Servo myservo; // Non c'e' bisogno di extern se e' dichiarato in questo scope
+// Ultrasuoni
+const byte trigPIN = 11;
+const byte echoPIN= 12;
+const byte ledPIN = 13;
+long duration;
+int distance;
+const int minDistance = 10;
+
////////////////////////
// Funzioni:
//
void abilita() {
-// Abilita i PINs come OUTPUTS
+// Abilita i PINs come OUTPUTS e attacca un servo
+// Motors
pinMode(enA, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
-
-
+// Servo
pinMode(servoPIN, OUTPUT);
- myservo.attach(servoPIN);
+ myservo.attach(servoPIN); //la libreria servo deve essere sempre inclusa
+// Ultrasonic
+ pinMode(trigPIN, OUTPUT);
+ pinMode(echoPIN, INPUT);
+ pinMode(ledPIN, OUTPUT);
}
delay(spausa);
}
}
+
+
+// Ultrasuoni
+boolean distanceCheck() {
+// Verifica se la distanza di un oggetto e' minore di minDistance
+ digitalWrite(trigPIN, LOW); // Prepare for ping
+ delayMicroseconds(2); //
+ digitalWrite(trigPIN, HIGH); // Send a ping
+ delayMicroseconds(10); //
+ digitalWrite(trigPIN, LOW); // Set down ping
+ duration = pulseIn(echoPIN, 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)
+#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() {
+// Ritorna la distanza di un oggetto in cm
+ digitalWrite(trigPIN, LOW); // Prepare for ping
+ delayMicroseconds(2); //
+ digitalWrite(trigPIN, HIGH); // Send a ping
+ delayMicroseconds(10); //
+ digitalWrite(trigPIN, LOW); // Set down ping
+ duration = pulseIn(echoPIN, 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;
+}
void giraDX() ;
void stop() ;
-
void servoDX() ;
void servoSX() ;
void servoMiddle() ;
+
+boolean distanceCheck() ;
+int distanceMonitor() ;
#endif
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);
//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;
+}
--- /dev/null
+/* Ultrasonic rilevatore distanza
+
+Rilevatore distanza minore di 5cm.
+funzione
+
+HC-SR04 Ping distance sensor
+VCC to arduino 5v - GND to arduino GND
+
+ */
+#define dEBUG
+#include "rover.h"
+#include "Servo.h"
+
+
+void setup() {
+ abilita();
+ //Debug
+#ifdef DEBUG
+Serial.begin(9600);
+#endif
+Serial.begin(9600); // Need for distanceMonitor
+}
+
+void loop() {
+if (distanceCheck()) {
+ digitalWrite(13,HIGH);
+} else {
+ digitalWrite(13,LOW);
+}
+Serial.println(distanceMonitor());
+delay(50);
+}