/*
==== Task 4 - LED Chase Sequence Using a Function ====
Author: Roberto Palozzo
======================================================
*/
#include <Wire.h> // I2C communication library
#include <Adafruit_GFX.h> // graphics library for the OLED
#include <Adafruit_SSD1306.h> // driver library for the OLED display
#define SCREEN_WIDTH 128 // OLED width in pixels
#define SCREEN_HEIGHT 64 // OLED height in pixels
#define OLED_SDA 35 // I2C data pin
#define OLED_SCL 0 // I2C clock pin
int buzzerPin = 10; // pin connected to the buzzer
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // create OLED display object
// Turns on a single LED for "delayTime" ms, then turns it off
void lightLED(int ledPin, int delayTime) {
digitalWrite(ledPin, HIGH); // turn LED on
delay(delayTime); // keep it on
digitalWrite(ledPin, LOW); // turn LED off
}
// Runs once, every time the chase completes a full lap
void endOfSequence() {
tone(buzzerPin, 1000); // play a short beep
delay(200);
noTone(buzzerPin); // stop the beep
display.clearDisplay(); // clear old content
display.setTextSize(2); // set text size
display.setCursor(0, 20); // set text position
display.println("Lap done!"); // write message
display.display(); // show it on screen
delay(500); // keep message visible
display.clearDisplay(); // clear message
display.display(); // update screen (now blank)
}
void setup() {
pinMode(4, OUTPUT); // set pin 4 as output
pinMode(5, OUTPUT); // set pin 5 as output
pinMode(6, OUTPUT); // set pin 6 as output
pinMode(7, OUTPUT); // set pin 7 as output
pinMode(buzzerPin, OUTPUT); // set buzzer pin as output
Wire.begin(OLED_SDA, OLED_SCL); // start I2C on custom pins
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize OLED at I2C address 0x3C
display.clearDisplay(); // clear display buffer
display.setTextColor(SSD1306_WHITE); // set text color
display.display(); // apply initial blank screen
}
void loop() {
lightLED(4, 200); // chase LED 1
lightLED(5, 200); // chase LED 2
lightLED(6, 200); // chase LED 3
lightLED(7, 200); // chase LED 4
endOfSequence(); // buzzer beep + OLED message at the end of each full chase
}