/*
Task B — Coding: Use a single button with press-type detection (display the event on the
OLED):
Name: Raja M Ayyaz
Roll No: CS-1278
course embeded iot
*/
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// Pins
const int buttonPin = 32;
const int ledPin = 2;
const int buzzerPin = 25;
// Button handling
bool lastButtonState = HIGH;
unsigned long pressStartTime = 0;
bool isPressing = false;
// Press classification
const unsigned long longPressTime = 1500; // 1.5 sec
bool ledState = false;
void showEvent(const char* text) {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 10);
display.println(text);
display.display();
}
void setup() {
pinMode(buttonPin, INPUT_PULLUP);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
showEvent("Ready!");
}
void loop() {
bool currentState = digitalRead(buttonPin);
if(currentState == LOW && lastButtonState == HIGH) {
pressStartTime = millis();
isPressing = true;
}
if(currentState == HIGH && lastButtonState == LOW) {
unsigned long pressDuration = millis() - pressStartTime;
isPressing = false;
if(pressDuration < longPressTime) {
ledState = !ledState;
digitalWrite(ledPin, ledState);
showEvent("Short Press");
}
}
if(isPressing && (millis() - pressStartTime >= longPressTime)) {
showEvent("Long Press");
tone(buzzerPin, 1000, 200);
isPressing = false;
}
lastButtonState = currentState;
}