#include<LiquidCrystal.h>

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

 

long distanza=0;

long tempo=0;

const float c=0.017;

 

void setup()

{

lcd.begin(16,2);

lcd.print(“tempo   dist”);

pinMode(10, OUTPUT); //trig

pinMode(9, INPUT); //echo

pinMode(8, OUTPUT); //verde

pinMode(7, OUTPUT); //rosso

 

}

 

void loop()

{

digitalWrite(10, LOW);

digitalWrite(10, HIGH);

delayMicroseconds(10);

 

  digitalWrite(10, LOW);

tempo=pulseIn(9, HIGH);

distanza=tempo*c;

 

 

lcd.setCursor(0,1);

lcd.print(tempo/1000);

lcd.print(“ms  “);

lcd.setCursor(8,1);

lcd.print(distanza);

lcd.print(“cm    “);

 

if(distanza<200 && distanza>60){

digitalWrite(8,HIGH);

digitalWrite(7, LOW);

}

else{

digitalWrite(8, LOW);

digitalWrite(7, HIGH);

}

}