Arduino – Range Finder Portatile (sketch)

Qui trovate l’articolo
Il video può essere visualizzato anche su youtube
Puoi supportarmi sulla mia pagina Patreon!
Seguimi anche sulla pagina di facebook e sul canale di youtube

di seguito lo sketch (il software che fa funzionare il tutto):

#include

const int contrastPin = A1;
const int tempPin = A0;
const int redLEDPin = 13;
const int greenLEDPin = 10;
const int triggerPin = 7;
const int echoPin = 8;
const int contrast = 25;

LiquidCrystal lcd(12,11,5,4,3,2);

void setup() {
lcd.begin(16, 2);
pinMode(tempPin, INPUT);
pinMode(contrastPin, OUTPUT);
pinMode(redLEDPin, OUTPUT);
pinMode(greenLEDPin, OUTPUT);
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
analogWrite(contrastPin, contrast);
lcd.setCursor(0,0);
lcd.print("Benvenuto");
lcd.setCursor(0,1);
lcd.print("Zamein PRF");
digitalWrite(greenLEDPin, HIGH);
digitalWrite(redLEDPin, HIGH);
delay(1500);
lcd.clear();
lcd.setCursor(0,0);
digitalWrite(greenLEDPin, LOW);
digitalWrite(redLEDPin, LOW);
}

void loop() {
float temperature = getTemperature();
float distance = getDistance(temperature);
if(distance > 390 ) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Distanza superiore al massimo rilevabile");
digitalWrite(redLEDPin, HIGH);
delay(1500);
lcd.clear();
lcd.setCursor(0,0);
digitalWrite(redLEDPin, LOW);
} else {
digitalWrite(greenLEDPin, HIGH);
lcdWrite(temperature, distance);
delay(1000);
digitalWrite(greenLEDPin, LOW);
}

}

float getTemperature() {
int sensorVal = analogRead(tempPin);
float voltage = (sensorVal/1024.0) * 5.0;
float temperature = (voltage - 0.5) *100;
return temperature;
}

float getDistance(float temperature) {
float soundSpeed = 331.5 + (0.6 * temperature);
float microSecondsPerCm = 10000 / soundSpeed;
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
long duration = pulseIn(echoPin, HIGH);
float distance = duration / microSecondsPerCm /2;
return distance;
}

void lcdWrite(float temperature, float distance) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.setCursor(0,1);
lcd.print("Dist: ");
lcd.print(distance);

}

Liked it? Take a second to support Zamein on Patreon!
Precedente Arduino - Interruttore magnetico Successivo Arduino - Range Finder Portatile