#include <LiquidCrystal_I2C.h>
// Constants for PIR sensor and display pins
const int pirPin = 4;
const int displayClkPin = 3;
const int displayDioPin = 2;
// Initialize the TM1637 display
LiquidCrystal_I2C lcd(0x27, 16, 2);
long counter = 0; // Use a long data type for an 8-digit counter
void setup() {
pinMode(pirPin, INPUT);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("IR count: ");
Serial.println(counter);
char buffer[9]; // Buffer to store the formatted count (8 digits + null terminator)
lcd.setCursor(8, 1); // Move the cursor to the 2nd line
lcd.print(buffer); // Print the formatted count
Serial.begin(9600);
}
void loop() {
int motionValue = digitalRead(pirPin);
if (motionValue == HIGH) {
counter++; // Increment the motion count
Serial.print("Motion Detected! Counter is at: ");
Serial.println(counter);
char buffer[9]; // Buffer to store the formatted count (8 digits + null terminator)
sprintf(buffer, "%08ld", counter); // Format with leading zeros
lcd.setCursor(8, 1); // Move the cursor to the 2nd line
lcd.print(buffer); // Print the formatted count
}
}