Hi all, I'm putting together something to ring a bell automatically. It's a massive bell at the top of a church with a diameter of about 40 inches.
I've got the hardware side of things good to go to. I'll basically going to trigger a relay with the Arduino every hour, on the hour for the hour specified.
I will ONLY ring it from 9:00 AM to 6:00 PM. So a total of 10 cycles per day. I'm using a DS3231 RTC.
I've got the board working correctly. I'm currently just testing it with the board's LED. (I do know the relay and solenoid that I'm using works, I just am using the LED currently for testing purposes.)
Can you guys please give a quick review of my code and give me any suggestions to clean it up or other ideas? Code is posted below. Also attached. Not sure which is the most appropriate way to share it.
Please be gentle! Not an expert coder here! I've spent hours on this and even though I could have just had my son (who's studying CS) just code it for me, I wanted to do it myself. Dusting off the cobwebs from that part of my brain was the biggest hurdle.
Okay, my MO:
- Check the "strike hours" (set up as an array) via a simple for loop.
- If current hour equals one of the predetermined "strike hours" then go to "strike" subroutine.
- After strike subroutine is complete, return to continue main loop.
- I will have a manual button so the end user (i.e. me) can ring the bell once at any time of the day by just simply pushing the button.
I haven't set an LCD yet. I know I need to include that at some point.
THANK YOU in advance!!!
#include "RTClib.h"
RTC_DS3231 rtc;
int buttonState = 0; // variable for reading the pushbutton status
const int buttonPin = 2; // the number of the pushbutton pin
char days[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
const int ledPin = 13;
int strike_hours[10] = {9,10,11,12,13,14,15,16,17,18};
int number_of_strikes = 0;
void strike_bell() {
number_of_strikes = 0;
DateTime now = rtc.now();
number_of_strikes = number_of_strikes + now.hour();
if (now.hour() >= 13) {
number_of_strikes = number_of_strikes - 12;
}
for(int y = 0; y < number_of_strikes; y++){
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(2000);
}
}
void setup () {
Serial.begin(9600);
rtc.begin();
pinMode(ledPin, OUTPUT); // Initialize the LED_BUILTIN pin as an output
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input:
//rtc.adjust(DateTime(2020, 02, 19, 17, 9, 00));
}
void loop () {
DateTime now = rtc.now();
for(int x = 0; x < 10; x++) {
if (now.hour() == strike_hours[x] && now.minute() == 00 && now.second() == 00) {
strike_bell(); // strike the bell
delay(1);
} else {
digitalWrite(ledPin, LOW); // keep LED off:
}
Serial.print(now.year(), DEC);
Serial.print('/');
Serial.print(now.month(), DEC);
Serial.print('/');
Serial.print(now.day(), DEC);
Serial.print(" (");
Serial.print(days[now.dayOfTheWeek()]);
Serial.print(") ");
Serial.print(now.hour(), DEC);
Serial.print(':');
Serial.print(now.minute(), DEC);
Serial.print(':');
Serial.print(now.second(), DEC);
Serial.println();
Serial.print("Temperature: ");
Serial.print(rtc.getTemperature());
Serial.println(" C");
Serial.println();
delay(100);
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
delay(100);
digitalWrite(ledPin, LOW);
delay(2000);
} else {
digitalWrite(ledPin, LOW); // keep LED low
delay(1);
}
}
}
Clock_Code_for_Bell_Ringer_4.ino (2.12 KB)