#include <OneWireHub.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int pirPin = 13; // PIR sensor output pin
volatile unsigned long lastPirTime = 0;
volatile unsigned long pirInterval = 1000; // Set the PIR interval in milliseconds
volatile int pirCount = 0;
void IRAM_ATTR handlePirInterrupt() {
if (millis() - lastPirTime > pirInterval) {
pirCount++;
lastPirTime = millis();
}
}
void setup() {
Serial.begin(9600);
// Initialize PIR sensor pin
pinMode(pirPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(pirPin), handlePirInterrupt, FALLING);
// Initialize OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
// Clear the buffer
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
// Set text size and display message
display.setTextSize(1);
display.setCursor(0, 0);
display.println("Speed Detector");
display.display();
delay(2000);
}
void loop() {
// Clear the buffer
display.clearDisplay();
// Display PIR count
display.setTextSize(2);
display.setCursor(0, 0);
display.print("Count: ");
display.println(pirCount);
// Calculate speed (count per second)
float speed = pirCount / 2.0; // Assuming interval of 0.5 seconds
display.setTextSize(2);
display.setCursor(0, 20);
display.print("Speed: ");
display.println(speed, 1);
// Reset PIR count if necessary
if (millis() - lastPirTime > 500) {
pirCount = 0;
}
// Display on OLED
display.display();
delay(100);
}