Indice
Descrizione
In questo video tutorial, scoprirai come creare un sensore di parcheggio con Arduino. Questo progetto fai-da-te ti permette di evitare fastidiosi graffi e urti mentre parcheggi la tua auto.
Imparerai a costruire un sistema che utilizza ultrasuoni per rilevare la distanza tra il veicolo e gli ostacoli circostanti.
Inoltre, vedrai come collegarlo tramite WiFi ad Arduino IoT Cloud per un controllo comodo e facile da remoto.
#define TRIG_PIN 2
#define ECHO_PIN 3
#define TEMPERATURE_PIN A5
#define BUZZER_PIN 5
#define FREQUENCY 523
#define LEVELS 3
int distances[] = {10, 20, 30};
int leds[] = {8, 9, 10};
unsigned long delays[] = {100, 150, 200};
unsigned long durations[] = {80, 100, 120};
unsigned long lastChange = 0;
int lastLevel = -1;
int state = LOW;
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
for (int i = 0; i < LEVELS; i++) {
pinMode(leds[i], OUTPUT);
}
}
void loop() {
float distance = readDistance();
int level = selectLevel(distance);
if (level != lastLevel && lastLevel != -1) {
noTone(BUZZER_PIN);
digitalWrite(leds[lastLevel], LOW);
state = LOW;
}
lastLevel = level;
if (level == -1) {
return;
}
if ((millis() - lastChange) > delays[level]) {
state = !state;
digitalWrite(leds[level], state);
if (state) {
tone(BUZZER_PIN, FREQUENCY, durations[level]);
}
lastChange = millis();
}
}
float readDistance() {
float temperature = analogRead(TEMPERATURE_PIN) / 2.046;
float speed = 0.03314 + 0.000062 * temperature;
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
return pulseIn(ECHO_PIN, HIGH) * speed / 2;
}
int selectLevel(float distance) {
for (int i = 0; i < LEVELS; i++) {
if (distance < distances[i]) {
return i;
}
}
return -1;
}
#include "thingProperties.h"
#define TRIG_PIN 2
#define ECHO_PIN 3
#define TEMPERATURE_PIN A5
#define BUZZER_PIN 5
#define FREQUENCY 523
#define LEVELS 3
#define ENABLE_LED_PIN 7
int distances[] = {10, 20, 30};
int leds[] = {8, 9, 10};
unsigned long delays[] = {100, 150, 200};
unsigned long durations[] = {80, 100, 120};
unsigned long lastChange = 0;
int lastLevel = -1;
int state = LOW;
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
for (int i = 0; i < LEVELS; i++) {
pinMode(leds[i], OUTPUT);
}
pinMode(ENABLE_LED_PIN, OUTPUT);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
enable = false;
}
void loop() {
ArduinoCloud.update();
if (!enable) {
return;
}
float distance = readDistance();
int level = selectLevel(distance);
if (level != lastLevel && lastLevel != -1) {
noTone(BUZZER_PIN);
digitalWrite(leds[lastLevel], LOW);
state = LOW;
}
lastLevel = level;
if (level == -1) {
return;
}
if ((millis() - lastChange) > delays[level]) {
state = !state;
digitalWrite(leds[level], state);
if (state) {
tone(BUZZER_PIN, FREQUENCY, durations[level]);
}
lastChange = millis();
}
}
float readDistance() {
float temperature = analogRead(TEMPERATURE_PIN) / 2.046;
float speed = 0.03314 + 0.000062 * temperature;
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
return pulseIn(ECHO_PIN, HIGH) * speed / 2;
}
int selectLevel(float distance) {
for (int i = 0; i < LEVELS; i++) {
if (distance < distances[i]) {
return i;
}
}
return -1;
}
/*
Since Enable is READ_WRITE variable, onEnableChange() is
executed every time a new value is received from IoT Cloud.
*/
void onEnableChange() {
digitalWrite(ENABLE_LED_PIN, enable);
if (!enable && lastLevel != -1) {
digitalWrite(leds[lastLevel], LOW);
noTone(BUZZER_PIN);
state = LOW;
lastLevel = -1;
}
}