0% found this document useful (0 votes)
11 views3 pages

School Bell 2

This document contains an Arduino sketch for a modified school bell system using an RTC module. The system rings a bell every 5 minutes starting from 2:05 PM, with options for normal, pattern, and hourly bell types. It includes functions to control a buzzer and relay for the bell sound and timing logic to prevent multiple triggers within the same minute.

Uploaded by

atharvchavan0911
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

School Bell 2

This document contains an Arduino sketch for a modified school bell system using an RTC module. The system rings a bell every 5 minutes starting from 2:05 PM, with options for normal, pattern, and hourly bell types. It includes functions to control a buzzer and relay for the bell sound and timing logic to prevent multiple triggers within the same minute.

Uploaded by

atharvchavan0911
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <Wire.

h>
#include <RTClib.h>

// Pin definitions
#define BUZZER_PIN 9 // Connected to buzzer
#define RELAY_PIN 8 // Connected to relay module

// Create RTC object


RTC_DS3231 rtc;

// Bell types
#define NORMAL_BELL 1
#define PATTERN_BELL 2
#define HOURLY_BELL 3

// Bell duration in milliseconds (how long the bell rings each time)
#define BELL_DURATION 5000 // 5 seconds

void setup() {
Serial.begin(9600);

// Initialize pins
pinMode(BUZZER_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);

// Make sure relay is off at startup


digitalWrite(RELAY_PIN, LOW);

// Initialize RTC
if (!rtc.begin()) {
Serial.println("Couldn't find RTC");
while (1);
}

// Uncomment the line below to set the RTC to the date & time when the
sketch was compiled
// rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));

// Check if RTC lost power and warn if it did


if (rtc.lostPower()) {
Serial.println("RTC lost power, let's set the time!");
// Following line sets the RTC to the date & time when the sketch was
compiled
rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

Serial.println("Modified School Bell System Ready!");


Serial.println("Bells will ring every 5 minutes starting from 2:05
PM");
}

void loop() {
DateTime now = rtc.now();

// Display current time every second


static uint8_t lastSecond = 0;
if (lastSecond != now.second()) {
lastSecond = now.second();
Serial.print(now.hour());
Serial.print(":");
if (now.minute() < 10) Serial.print("0");
Serial.print(now.minute());
Serial.print(":");
if (now.second() < 10) Serial.print("0");
Serial.println(now.second());
}

// Check if it's time to ring the bell


// Conditions:
// 1. We're past 2:05 PM (hour >= 14)
// 2. Minutes mod 5 equals 0 (for :00, :05, :10, etc.)
// 3. Or minutes mod 5 equals 5 (for :05, :15, :25, etc.)
// 4. Seconds equal 0

static bool bellRung = false; // Flag to prevent multiple triggers

if (now.hour() >= 14 &&


now.second() == 0 &&
(now.minute() % 5 == 0 || (now.hour() == 14 && now.minute() == 5)))
{

if (!bellRung) { // Prevent multiple triggers in the same minute


ringNormalBell(BELL_DURATION);

bellRung = true;
Serial.print("Bell ringing at: ");
Serial.print(now.hour());
Serial.print(":");
if (now.minute() < 10) Serial.print("0");
Serial.print(now.minute());
Serial.print(":");
if (now.second() < 10) Serial.print("0");
Serial.println(now.second());
}
} else {
// Reset the bellRung flag when the second changes from 0
if (now.second() != 0) {
bellRung = false;
}
}

delay(50); // Short delay to prevent excessive CPU usage


}

// Ring both buzzer and relay continuously for the specified duration
void ringNormalBell(uint16_t duration) {
// Activate both buzzer and relay
digitalWrite(RELAY_PIN, HIGH);
tone(BUZZER_PIN, 1000); // 1kHz tone

delay(duration); // Keep active for specified duration

// Turn off both


digitalWrite(RELAY_PIN, LOW);
noTone(BUZZER_PIN);
}

// Ring both buzzer and relay in on/off pattern for the specified
duration
void ringPatternBell(uint16_t duration) {
unsigned long startTime = millis();
bool isOn = false;
int patternInterval = 500; // 0.5 second intervals for on/off pattern

while (millis() - startTime < duration) {


if (isOn) {
digitalWrite(RELAY_PIN, LOW);
noTone(BUZZER_PIN);
} else {
digitalWrite(RELAY_PIN, HIGH);
tone(BUZZER_PIN, 1000); // 1kHz tone
}

isOn = !isOn;
delay(patternInterval);
}

// Ensure everything is off when done


digitalWrite(RELAY_PIN, LOW);
noTone(BUZZER_PIN);
}

// Ring hourly bell with specific tone


void ringHourlyBell(uint16_t duration) {
// Activate both buzzer and relay
digitalWrite(RELAY_PIN, HIGH);
tone(BUZZER_PIN, 1500); // Different tone (1.5kHz) for hourly bells

delay(duration); // Keep active for specified duration

// Turn off both


digitalWrite(RELAY_PIN, LOW);
noTone(BUZZER_PIN);
}

You might also like