Project info: I am trying to make an alarm clock that turns on my coffee maker. I would like to use the atmega328's internal clock instead of an RTC. I will eventually integrate an lcd and the relay and make it standalone, but I am fairly new to arduino so I am taking baby steps.
No errors in compiling or uploading to my UNO board, but the alarmisON function seems to always be true. What am I doing wrong?
//defines
long day = 86460000; // 86400 sec/day adjusted for the atmega328 clock
long hour = 3602500; // 3600 sec/hour " "
long minute = 60041; // 60 sec/min " "
long second = 1000; // 1000 millis/sec, unadjusted
long alarmTime = 60041;
//setup
void setup(){
Serial.begin (9600);
millis() == 0;
}
//todo: add a caseopened function to detect if the system has been loaded since it last went off. If it hasn't been loaded, set the alarm to not go off
void loop(){
time();
if(millis() >= 86460000 ){ //resets the mils counter at "midnight"
millis() == 0;
}
}
void time(){
long timeNow = millis();
int days = timeNow / day ; //days since reset
int hours = (timeNow % day) / hour; //hours since start of day
int minutes = ((timeNow % day) % hour) / minute ; //minutes since start of hour
int seconds = (((timeNow % day) % hour) % minute) / second; //seconds since start of minute
//turning the alarm on
if (alarmTime = timeNow){
boolean alarmisON = true;
}
else{
boolean alarmisON = false;
}
// display current time
Serial.print(hours);
printDigits(minutes);
printDigits(seconds);
if (boolean alarmisON = true){
Serial.print(" boolean alarmisON = true");
}
else {
Serial.print(" boolean alarmisON = false");
}
Serial.println();
}
//pretty stuff
void printDigits(byte digits){
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits,DEC);
delay (1);
}
This doesn't do anything, == is for making comparisons
millis() == 0;
millis() returns a value - don't believe you can set it with millis() = 0;
make these unsigned long and put UL after the number.
long day = 86460000; // 86400 sec/day adjusted for the atmega328 clock
long hour = 3602500; // 3600 sec/hour " "
long minute = 60041; // 60 sec/min " "
long second = 1000; // 1000 millis/sec, unadjusted
long alarmTime = 60041;
make any variables having to do with millis() unsigned long
this needs == for comparison
if (alarmTime = timeNow){
otherwise you are setting alarmTime equal to timeNow, which apparantly equates to being True within the if()
Some small corrections (as noted above by 'CrossRoads')
const unsigned long second = 1000UL;
const unsigned long minute = 60UL * second;
const unsigned long hour = 60UL * minute;
const unsigned long day = 24UL * hour;
const unsigned long alarmTime = 60041UL;
void setup()
{
Serial.begin(9600);
millis() == 0;
}
//todo: add a caseopened function to detect if the system has been loaded since it last went off. If it hasn't been loaded, set the alarm to not go off
void loop()
{
time();
}
void time()
{
unsigned long timeNow = millis();
int days = timeNow / day; // days since reset
int hours = (timeNow % day) / hour; // hours since start of day
int minutes = ((timeNow % day) % hour) / minute; // minutes since start of hour
int seconds = (((timeNow % day) % hour) % minute) / second; //seconds since start of minute
//turning the alarm on
// display current time
Serial.print(hours);
printDigits(minutes);
printDigits(seconds);
bool alarmisON = ((alarmTime == timeNow) ? true : false);
Serial.print(" boolean alarmisON = ");
Serial.println((alarmisON ? "true" : "false"));
}
void printDigits(byte digits)
{
Serial.print(":");
if ( digits < 10 )
{
Serial.print('0');
}
Serial.print(digits, DEC);
delay(1);
}
You don't control the value of millis(). That's a "system" thing. If you want to turn on the coffee machine at a specified wall clock time, you have to use an RTC, otherwise whenever the arduino is reset millis() will restart from zero.
Oh, and please take away that delay(1) inside printDigits()