This circuit is designed to count the number of balls passing through two IR sensors and display the count on a 16x2 I2C LCD. The circuit also includes a servo motor and a reset button to reset the count. The Arduino UNO microcontroller is used to control the entire setup.
Arduino UNO
Servo
IR Sensor
16x2 I2C LCD
Red Pushbutton
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h> // Include the Servo library
// LCD setup
LiquidCrystal_I2C lcd(0x27, 16, 2); // LCD address 0x27, 16 columns, 2 rows
// IR Sensor Pins
int Ir_In = 11; // IR sensor for ball IN
int Ir_Out = 12; // IR sensor for ball OUT
// Counters
int count_in = 0;
int count_out = 0;
// Variables to store previous states
int prev_in = HIGH;
int prev_out = HIGH;
// Reset Button
int resetButton = 10; // Reset button connected to pin 10
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Set IR sensor pins as input
pinMode(Ir_In, INPUT);
pinMode(Ir_Out, INPUT);
// Set Reset button as input with pull-up resistor
pinMode(resetButton, INPUT_PULLUP);
// Display initial message
lcd.setCursor(0, 0);
lcd.print("IR Ball Counter");
delay(2000);
lcd.clear();
}
void loop() {
// Check for reset button press
if (digitalRead(resetButton) == LOW) {
count_in = 0;
count_out = 0;
lcd.clear(); // Clear LCD display
lcd.setCursor(0, 0);
lcd.print("Session Reset");
delay(1000); // Display reset message for 1 second
lcd.clear(); // Clear the screen again
}
// Read sensor values
int val_in = digitalRead(Ir_In);
int val_out = digitalRead(Ir_Out);
// Check ball IN detection
if (val_in == LOW && prev_in == HIGH) { // Detect transition from HIGH to LOW
count_in++; // Increment IN counter
delay(200); // Debounce delay
}
prev_in = val_in; // Update previous state
// Check ball OUT detection
if (val_out == LOW && prev_out == HIGH) { // Detect transition from HIGH to LOW
count_out++; // Increment OUT counter
delay(200); // Debounce delay
}
prev_out = val_out; // Update previous state
// Display the count on LCD
lcd.setCursor(0, 0);
lcd.print("IN: ");
lcd.print(count_in);
lcd.print(" OUT: ");
lcd.print(count_out);
lcd.setCursor(0, 1);
lcd.print("Remaining: ");
lcd.print(count_in - count_out);
delay(500); // Refresh rate
}
This code initializes the LCD, sets up the IR sensors and the reset button, and continuously monitors the sensors to update the ball count on the LCD. The reset button resets the count when pressed.