Download
Programa para Arduino *.ino
Libraria NewliquidCrystal_1.3.4
Como utilizar o módulo I2C com display LCD - Arduino e Cia
Configura o Módulo I2C
Programa para Arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define DIST_MIN 70
#define OFFSET 101
#define AMOSTRAS 10
// define o n.º dos pinos
const int vermelhoUmPin = 13;
const int vermelhoDoisPin = 12;
const int amareloUmPin = 11;
const int amareloDoisPin = 10;
const int verdeUmPin = 9;
const int verdeDoisPin = 8;
const int trigPin = 7;
const int echoPin = 6;
const int buzzerPin = 5;
// defines variables
long duration,tempo;
int distance,i;
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7,3, POSITIVE);
void setup() {
pinMode(vermelhoUmPin, OUTPUT);
pinMode(vermelhoDoisPin, OUTPUT);
pinMode(amareloUmPin, OUTPUT);
pinMode(amareloDoisPin, OUTPUT);
pinMode(verdeUmPin, OUTPUT);
pinMode(verdeDoisPin, OUTPUT);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzerPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
tempo = millis();
lcd.begin(16,2);
}
void loop() {
if( millis() > tempo + 250 ){
tempo = millis();
distance = 0;
for(i = 0; i < AMOSTRAS; i++){
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance = distance+duration*0.034/2;
// Prints the distance on the Serial Monitor
}
distance/=AMOSTRAS; // distance = distance / AMOSTRAS;
Serial.print("Distance: ");
Serial.println(distance);
lcd.setCursor(0,0);
lcd.print("Distancia: ");
lcd.setCursor(11,0);
lcd.print(distance);
if( distance<=DIST_MIN)
digitalWrite(verdeDoisPin, HIGH);
else
digitalWrite(verdeDoisPin, LOW);
if( distance<=DIST_MIN - OFFSET)
digitalWrite(verdeUmPin, HIGH);
else
digitalWrite(verdeUmPin, LOW);
if( distance<=DIST_MIN - OFFSET*2)
digitalWrite(amareloDoisPin, HIGH);
else
digitalWrite(amareloDoisPin, LOW);
if( distance<=DIST_MIN - OFFSET*3)
digitalWrite(amareloUmPin, HIGH);
else
digitalWrite(amareloUmPin, LOW);
if( distance<=DIST_MIN - OFFSET*4)
digitalWrite(vermelhoDoisPin, HIGH);
else
digitalWrite(vermelhoDoisPin, LOW);
if( distance<=DIST_MIN - OFFSET*5)
digitalWrite(vermelhoUmPin, HIGH);
else
digitalWrite(vermelhoUmPin, LOW);
}
}