#include <TM1637Display.h>
#define CLK 2 // TM1637 CLK pin
#define DIO 3 // TM1637 DIO pin
#define IR_PIN 4 // IR sensor output
#define BUZZER_PIN 5
#define BUTTON_PIN 6
TM1637Display display(CLK, DIO);
int count = 0;
bool detected = false;
void setup() {
pinMode(IR_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Using internal pull-up resistor
display.setBrightness(0x0f); // Max brightness
display.showNumberDec(0, true);
}
void loop() {
int sensorValue = digitalRead(IR_PIN);
int buttonState = digitalRead(BUTTON_PIN);
// Object detected
if (sensorValue == LOW && !detected) {
count++;
if (count > 9999) count = 0;
display.showNumberDec(count, true);
// Beep buzzer once
tone(BUZZER_PIN, 1000, 200); // 1kHz for 200ms
detected = true;
delay(300); // Debounce delay for IR sensor
}
// Reset button pressed (active LOW because of pull-up)
if (buttonState == LOW) {
count = 0;
display.showNumberDec(count, true);
delay(300); // Debounce delay for button
}
if (sensorValue == HIGH) {
detected = false;
}
}