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

SCHOOLBELL

This document contains an Arduino sketch for a school bell system using an RTC module to schedule bell rings. It defines various bell types and a schedule for ringing them at specified times, along with functions to handle the ringing patterns. The setup initializes the RTC and outputs the schedule, while the loop checks the current time to trigger the bells accordingly.

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 views4 pages

SCHOOLBELL

This document contains an Arduino sketch for a school bell system using an RTC module to schedule bell rings. It defines various bell types and a schedule for ringing them at specified times, along with functions to handle the ringing patterns. The setup initializes the RTC and outputs the schedule, while the loop checks the current time to trigger the bells accordingly.

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/ 4

#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 schedule structure


struct BellTime {
uint8_t hour;
uint8_t minute;
uint8_t second;
uint16_t duration; // Duration in milliseconds
uint8_t bellType; // Type of bell pattern
};

// Define your bell schedule here (24-hour format)


// Format: {hour, minute, second, duration_in_ms, bell_type}
const BellTime bellSchedule[] = {
{14, 0, 0, 7000, NORMAL_BELL}, // 10:00 AM - School start (7
seconds)
{11, 0, 0, 5000, HOURLY_BELL}, // 11:00 AM - Hourly bell (5 seconds)
{12, 0, 0, 5000, HOURLY_BELL}, // 12:00 PM - Hourly bell (5 seconds)
{13, 0, 0, 10000, NORMAL_BELL}, // 1:00 PM - Special bell (10
seconds)
{14, 0, 0, 5000, HOURLY_BELL}, // 2:00 PM - Hourly bell (5 seconds)
{15, 0, 0, 5000, HOURLY_BELL}, // 3:00 PM - Hourly bell (5 seconds)
{16, 0, 0, 5000, HOURLY_BELL}, // 4:00 PM - Hourly bell (5 seconds)
{16, 30, 0, 5000, PATTERN_BELL} // 4:30 PM - Pattern bell (5 seconds)
};

const int totalBells = sizeof(bellSchedule) / sizeof(bellSchedule[0]);


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

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("School Bell System Ready!");


Serial.println("Current schedule:");

for (int i = 0; i < totalBells; i++) {


Serial.print(bellSchedule[i].hour);
Serial.print(":");
if (bellSchedule[i].minute < 10) Serial.print("0");
Serial.print(bellSchedule[i].minute);
Serial.print(":");
if (bellSchedule[i].second < 10) Serial.print("0");
Serial.print(bellSchedule[i].second);
Serial.print(" - Duration: ");
Serial.print(bellSchedule[i].duration);
Serial.print("ms - Type: ");
Serial.println(bellSchedule[i].bellType);
}
}

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


for (int i = 0; i < totalBells; i++) {
if (now.hour() == bellSchedule[i].hour &&
now.minute() == bellSchedule[i].minute &&
now.second() == bellSchedule[i].second) {

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


// Determine bell type and ring accordingly
switch(bellSchedule[i].bellType) {
case NORMAL_BELL:
ringNormalBell(bellSchedule[i].duration);
break;
case PATTERN_BELL:
ringPatternBell(bellSchedule[i].duration);
break;
case HOURLY_BELL:
ringHourlyBell(bellSchedule[i].duration);
break;
}

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());
}
return; // Exit the loop early once we've found a match
}
}

// Reset the bellRung flag when the second changes


static uint8_t lastBellSecond = 0;
if (lastBellSecond != now.second()) {
lastBellSecond = now.second();
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