#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define the LCD object with its I2C address, columns, and rows
LiquidCrystal_I2C lcd(0x27, 16, 2);
// Define the button pin
const int buttonPin = 2; // Connect your button to digital pin D8
// Define SR-HC04 pins
const int trigPin = 5; // Trigger pin connected to D5
const int echoPin = 6; // Echo pin connected to D6
// Define the threshold distance in centimeters
const int DISTANCE_THRESHOLD_CM = 10;
// Setup function: runs once when the Arduino starts
void setup() {
Wire.begin(8,9);
Serial.begin(9600);
lcd.init(); // Initialize the LCD
lcd.backlight(); // Turn on the backlight
// Set the button pin as an input with an internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
// Set SR-HC04 pins
pinMode(trigPin, OUTPUT); // Trigger pin as an output
pinMode(echoPin, INPUT); // Echo pin as an input
lcd.print("Hola Diego"); // Display "Hola Diego"
Serial.println("Hola Diego"); // Display "Hola Diego"
delay(2000); // Keep it on for 2 seconds
lcd.clear(); // Clear the screen
}
// Function to measure distance using the SR-HC04
long measureDistance() {
// Clear the trigPin by setting it LOW for 2 microseconds
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin HIGH for 10 microseconds to send a pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, returns the sound wave travel time in microseconds
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in cm (speed of sound ~0.034 cm/microsecond)
// Divide by 2 because the sound travels to the object and back
long distanceCm = duration * 0.034 / 2;
return distanceCm;
}
// Loop function: runs repeatedly after setup
void loop() {
int eventCount = 0; // Variable to count events (button press or distance)
bool lastButtonState = HIGH; // Track the previous state of the button
bool lastDistanceState = false; // Track if distance was previously below threshold
lcd.print("Presiona o Acerca"); // Prompt the user
Serial.println("Presiona o Acerca"); // Prompt the user
lcd.setCursor(0,1);
lcd.println("2 Veces");
Serial.print("2 Veces\n");
// Loop until two events are detected
while (eventCount < 2) {
// Read button state
bool currentButtonState = digitalRead(buttonPin);
// Read distance
long distance = measureDistance();
bool currentDistanceState = (distance > 0 && distance < DISTANCE_THRESHOLD_CM); // True if object is within threshold
// Check for a button press (transition from HIGH to LOW)
if (lastButtonState == HIGH && currentButtonState == LOW) {
eventCount++; // Increment the counter
lcd.clear();
lcd.print("Conteo (Boton):");
Serial.println("Conteo (Boton):\n");
lcd.setCursor(0, 1);
lcd.print(eventCount);
delay(300); // Debounce delay for the button
}
// Check for distance event (transition from NOT within threshold to WITHIN threshold)
if (lastDistanceState == false && currentDistanceState == true) {
eventCount++; // Increment the counter
lcd.clear();
lcd.print("Conteo (Dist):");
Serial.println("Conteo (Dist):\n");
lcd.setCursor(0, 1);
lcd.print(eventCount);
Serial.print(eventCount);
delay(500); // Delay for distance event to avoid rapid counting
}
// Update last states
lastButtonState = currentButtonState;
lastDistanceState = currentDistanceState;
delay(50); // Small general delay to keep things stable
}
// After two events, clear the screen and display "Buenas noches Diego"
lcd.clear();
lcd.print("Buenas noches");
Serial.println("Buenas noches\n");
lcd.setCursor(0,1);
lcd.print("Diego");
Serial.println("Diego\n");
delay(5000); // Display "Buenas noches Diego" for 5 seconds
lcd.clear(); // Clear the screen before repeating the loop
// The loop will then restart, prompting for two more events.
}