//Malaika
//Regno: 1291
//Assignment1-TaskB
#include <Arduino.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Pin setup
#define LED_PIN 18 // LED connected to GPIO 18
#define BUTTON_PIN 33 // Button connected to GPIO 33
#define BUZZER_PIN 27 // Buzzer connected to GPIO 27
Adafruit_SSD1306 display(128, 64, &Wire, -1);
// Variables
bool ledState = false; // LED is initially OFF
unsigned long pressStart = 0; // Time when button was pressed
bool buttonWasPressed = false; // Track if button was pressed
const int longPressTime = 1500; // 1500 milliseconds = 1.5 seconds
// Function to show messages on OLED
void showText(String line1, String line2 = "") {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(10, 20);
display.println(line1);
if (line2 != "") {
display.setCursor(10, 35);
display.println(line2);
}
display.display();
}
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Button connected to GND
pinMode(BUZZER_PIN, OUTPUT);
// Start OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
while (true); // stop if OLED not found
}
showText("System Ready", "Press Button");
}
void loop() {
int buttonState = digitalRead(BUTTON_PIN);
// When button is pressed (LOW because of INPUT_PULLUP)
if (buttonState == LOW && !buttonWasPressed) {
buttonWasPressed = true;
pressStart = millis(); // Record the time when pressed
}
// When button is released
if (buttonState == HIGH && buttonWasPressed) {
buttonWasPressed = false;
unsigned long pressDuration = millis() - pressStart;
if (pressDuration < longPressTime) {
// Short press → toggle LED
ledState = !ledState;
digitalWrite(LED_PIN, ledState);
if (ledState)
showText("Short Press", "LED is ON");
else
showText("Short Press", "LED is OFF");
} else {
// Long press → buzzer sound
showText("Long Press", "Buzzer ON");
tone(BUZZER_PIN, 1000, 1000); // 1kHz tone for 1s
}
delay(1000);
showText("System Ready", "Press Button");
}
delay(10);
}