Arduino Uno Alarm Clock Question

Hello,

I am attempting to make an Arduino alarm clock on my UNO and I am having a problem with incorporating a buzzer to go off whenever a certain time is reached. I attempted to write an IF statement but I’m not sure if i have it worded correctly and I keep getting an error message about a variable not being declared. I’ve read that I should include a TIMECHECK in my code to work with the alarm clock.

Thanks.

maybe1.ino (3.12 KB)

#include "Wire.h"
#include <LiquidCrystal.h>
#define DS1307_I2C_ADDRESS 0x68
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}
 
void getDateDs1307(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,byte *year)
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.write(0);
  Wire.endTransmission();
 
  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
 
  *second     = bcdToDec(Wire.read() & 0x7f);
  *minute     = bcdToDec(Wire.read());
  *hour       = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek  = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month      = bcdToDec(Wire.read());
  *year       = bcdToDec(Wire.read());
}
 
 
void setup()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();
  // AMEND IF YOUR USING A DIFFERENT LCD SCREEN //
  lcd.begin(16, 2);
pinMode(7, OUTPUT); // set a pin for buzzer output 
////buzz(7, 2500, 500);
}
void loop()
{
  
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  String s, m, d, mth, h;
   
  getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
 
  if (second < 10) { s = "0" + String(second); } else { s = String(second); }
  if (minute < 10) { m = "0" + String(minute); } else { m = String(minute); }
  h = String(hour);
  if (dayOfMonth < 10) { d = "0" + String(dayOfMonth); } else { d = String(dayOfMonth); }
  if (month < 10) { mth = "0" + String(month); } else { mth = String(month); }
   
  char* days[] = { "NA", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun" };
   
  lcd.clear();
  // JUMP TO CENTER ON A 16X2 SCREEN //
  lcd.setCursor(4,0);
  // CHANGE THE FOLLOWING TO SET THE DATE IN TO YOUR PREFERED ORDER //
  lcd.print(h + ":" + m + ":" + s);
  // NEXT LINE, 1 SPACE IN FROM THE LEFT //
  lcd.setCursor(1,1);
  // PREFIX THE 20 AS THE RTC CHIP ONLY USES 2 DIGITS FOR THE YEAR //
  lcd.print(String(days[dayOfWeek]) + " " + mth + "/" + d + "/20" + year);
  delay(1000); // Wait 1 second
////////////////////Alarm ADDITION////////////////////////
if (h == 17 && m == 13){
buzz(7, 2500, 500); // buzz the buzzer on pin 4 at 2500Hz for 1000 milliseconds

void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait againf or the calculated delay value
}}
}
  
/////////////////////////////////////////////////////////////////////////////////////////////////

keep getting an error message about a variable not being declared.

Pity you couldn't be bothered to share it. I probably could have helped you.

Delta_G:
Pity you couldn’t be bothered to share it. I probably could have helped you.

Arduino: 1.6.11 (Windows 10), Board: “Arduino/Genuino Uno”

C:\Users\James\Documents\Arduino\maybe1\maybe1.ino: In function ‘void loop()’:

C:\Users\James\Documents\Arduino\maybe1\maybe1.ino:52:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

char* days = { “NA”, “Mon”, “Tue”, “Wed”, “Thu”, “Fri”, “Sat”, “Sun” };

^

C:\Users\James\Documents\Arduino\maybe1\maybe1.ino:52:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\James\Documents\Arduino\maybe1\maybe1.ino:52:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\James\Documents\Arduino\maybe1\maybe1.ino:52:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\James\Documents\Arduino\maybe1\maybe1.ino:52:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\James\Documents\Arduino\maybe1\maybe1.ino:52:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\James\Documents\Arduino\maybe1\maybe1.ino:52:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\James\Documents\Arduino\maybe1\maybe1.ino:52:74: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]

C:\Users\James\Documents\Arduino\maybe1\maybe1.ino:66:10: warning: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]

if (h == 17 && m == 13){

^

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:223:0,

from sketch\maybe1.ino.cpp:1:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:143:16: note: initializing argument 1 of ‘unsigned char String::operator==(const char*) const’

unsigned char operator == (const char *cstr) const {return equals(cstr);}

^

C:\Users\James\Documents\Arduino\maybe1\maybe1.ino:66:21: warning: invalid conversion from ‘int’ to ‘const char*’ [-fpermissive]

if (h == 17 && m == 13){

^

In file included from C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/Arduino.h:223:0,

from sketch\maybe1.ino.cpp:1:

C:\Program Files (x86)\Arduino\hardware\arduino\avr\cores\arduino/WString.h:143:16: note: initializing argument 1 of ‘unsigned char String::operator==(const char*) const’

unsigned char operator == (const char *cstr) const {return equals(cstr);}

^

maybe1:67: error: ‘buzz’ was not declared in this scope

buzz(7, 2500, 500); // buzz the buzzer on pin 4 at 2500Hz for 1000 milliseconds

^

maybe1:69: error: a function-definition is not allowed here before ‘{’ token

void buzz(int targetPin, long frequency, long length) {

^

maybe1:82: error: expected ‘}’ at end of input

}

^

maybe1:82: error: expected ‘}’ at end of input

exit status 1
‘buzz’ was not declared in this scope

This report would have more information with
“Show verbose output during compilation”
option enabled in File → Preferences.

It would appear that you messed your braces up and tried to define one function inside another. Use Control-T to format your code and lone up the braces and maybe you'll spot the problem.

What does it mean when it stated that 'buzz' was not declared in this scope?

It means you tried to use a function that doesn't exist. The one you tried to write was defined inside another function, that's the other error. That one doesn't count because that's illegal code.