// Constants for IR sensor and reset button pins
const int irSensorPin = 5; // Input pin for MH IR sensor
const int resetButtonPin = 4; // Input pin for the reset button
// Constants for 7-segment display pins
int digitPins[] = {2, 3, 4};
int segmentPins[] = {6, 7, 8, 9, 10, 11, 12, 13};
// Number-to-segment mapping
int numbers[][7] = {
{0, 0, 0, 0, 0, 0, 1}, // zero
{1, 0, 0, 1, 1, 1, 1}, // one
{0, 0, 1, 0, 0, 1, 0}, // two
{0, 0, 0, 0, 1, 1, 0}, // three
{1, 0, 0, 1, 1, 0, 0}, // four
{0, 1, 0, 0, 1, 0, 0}, // five
{0, 1, 0, 0, 0, 0, 0}, // six
{0, 0, 0, 1, 1, 1, 1}, // seven
{0, 0, 0, 0, 0, 0, 0}, // eight
{0, 0, 0, 0, 1, 0, 0}, // nine
};
// Variables
int count = 0; // Counter value
int lastIrState = LOW; // Previous state of the IR sensor
int resetButtonState; // Current state of the reset button
int lastButtonState = LOW; // Previous state of the reset button
unsigned long lastDebounceTime = 0; // Last debounce time
const unsigned long debounceDelay = 50; // Debounce delay (in ms)
// Initialize pins
void initializePins() {
pinMode(irSensorPin, INPUT);
pinMode(resetButtonPin, INPUT);
for (int i = 0; i < 7; i++) {
pinMode(segmentPins[i], OUTPUT);
}
for (int i = 0; i < 3; i++) {
pinMode(digitPins[i], OUTPUT);
}
}
// Turn off all segments of the display
void turnOffDisplay() {
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], HIGH); // Turn off segment (active low)
}
}
// Display a number on a specific digit
void displayNumber(int number, int digitPin) {
digitalWrite(digitPins[digitPin], HIGH); // Enable the digit
for (int i = 0; i < 7; i++) {
digitalWrite(segmentPins[i], numbers[number][i]);
}
delay(5); // Small delay for stability
digitalWrite(digitPins[digitPin], LOW); // Disable the digit
turnOffDisplay(); // Turn off the segments
}
// Display the counter value across 3 digits
void displayToThreeDigit(int num) {
int hundreds = num / 100; // Extract hundreds place
int tens = (num / 10) % 10; // Extract tens place
int ones = num % 10; // Extract ones place
displayNumber(hundreds, 0); // Display hundreds digit
displayNumber(tens, 1); // Display tens digit
displayNumber(ones, 2); // Display ones digit
}
// Increment the counter using the IR sensor
void incrementCounter() {
int currentIrState = digitalRead(irSensorPin);
// Check if IR sensor detects motion and the state changed from LOW to HIGH
if (currentIrState == HIGH && lastIrState == LOW) {
if (count < 20) { // Increment counter only if below 20
count++;
}
}
lastIrState = currentIrState; // Update the last state
}
// Reset the counter using the button
void resetCounter() {
int reading = digitalRead(resetButtonPin);
// Debounce the button input
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != resetButtonState) {
resetButtonState = reading;
// Reset the counter if the button is pressed
if (resetButtonState == HIGH) {
count = 0;
}
}
}
lastButtonState = reading;
}
void setup() {
initializePins();
turnOffDisplay();
Serial.begin(9600);
}
void loop() {
incrementCounter(); // Increment counter using IR sensor
resetCounter(); // Reset counter using button
displayToThreeDigit(count); // Display the counter on the 7-segment display
}