Hello everyone, so I'm doing a project which involves a Buzzer, the HCSR04 (ultrasonic sensor), an LCD display and a matrix keyboard.
My idea is to make the led light up when the distance read by the HCSR04 is smaller than the one
pressed on the keyboard, however the buzzer would only buzz with the predefined distance.
Sorry if I didn't know how to express myself, I'm not a fluent English speaker.
(obs: i'm ussing 2 HCSR04)
//teclado = keyboard
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#define LED 13
int lcdColumns = 16;
int lcdRows = 2;
int buzzer_pin = 23;
int buzzer_channel = 0;
int trigPin1 = 5;
int EchoPIN1 = 19;
int trigPin2 = 12;
int EchoPIN2 = 18;
#define SOUND_SPEED 0.034
#define CM_TO_INCH 0.393701
LiquidCrystal_I2C lcd(0x27, lcdColumns, lcdRows);
const byte linhas = 4;
const byte colunas = 4;
char teclado[linhas][colunas] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte pinos_linhas[linhas] = {14, 27, 26, 25};
byte pinos_colunas[colunas] = {33, 32, 35, 34};
Keypad tecla_pressionada = Keypad( makeKeymap(teclado), pinos_linhas, pinos_colunas, linhas, colunas);
void setup(){
pinMode(LED, OUTPUT);
ledcAttachPin(buzzer_pin, buzzer_channel);
lcd.begin();
lcd.backlight();
pinMode(trigPin1,OUTPUT);
pinMode(EchoPIN2,INPUT);
pinMode(trigPin2,OUTPUT);
pinMode(EchoPIN2,INPUT);
Serial.begin(9600);
lcd.print("ligando...");
}
void loop(){
digitalWrite(trigPin1,LOW);
delayMicroseconds(2);
digitalWrite(trigPin1,HIGH);
delayMicroseconds(2);
digitalWrite(trigPin1,LOW);
char teclado = tecla_pressionada.getKey();
if (teclado){
Serial.println(teclado);
}
long timedelay = pulseIn(EchoPIN1,HIGH);
int distance1 = 0.0343 * (timedelay/2);
Serial.print("Sensor 1 : ");
Serial.println(distance1);
delayMicroseconds(2);
digitalWrite(trigPin2,LOW);
delayMicroseconds(2);
digitalWrite(trigPin2,HIGH);
delayMicroseconds(2);
digitalWrite(trigPin2,LOW);
long td = pulseIn(EchoPIN2,HIGH);
int distance2 = 0.0343 * (td/2);
Serial.print("Sensor 2 : ");
Serial.println(distance2);
if (distance1 <=5){
ledcWriteNote(buzzer_channel, (note_t)NOTE_D, 8);
delay(10);
}
else if (distance2 <=5){
ledcWriteNote(buzzer_channel, (note_t)NOTE_D, 8);
delay(10);
}
else if (teclado){
digitalWrite(LED,HIGH);
}
else if (teclado){
digitalWrite(LED,HIGH);
}
else
{
ledcWrite(0, 0);
delay(10);
digitalWrite(LED, LOW);
}
lcd.setCursor(0, 0);
lcd.print(distance1);
lcd.print(" cm direita ");
lcd.setCursor(0,1);
lcd.print(distance2);
lcd.print(" cm esquerda");
delay(1000);
}
