[SOLVED] Alarm library not working.
hello.
i'm doing little clock , 16x2 lcd learn how use time library , lcd.
i made little project show current time on lcd shield, have buttons.
if 'select' button pressed, enter in editing mode modify time. up/down/right/left used modify hour, minute , second digit. pressing select again, sets new time , show current time again.
i put alarm specifc time, star work in menu set alarm. doesn't work. have tried example library web site, , worked, when put in code, doesn't.
the example code can found here: http://www.pjrc.com/teensy/td_libs_timealarms.html
i'm using arduino mega 2560 - lcd shield has compatible pins it: http://www.dx.com/p/118059
here code:
i'm doing little clock , 16x2 lcd learn how use time library , lcd.
i made little project show current time on lcd shield, have buttons.
if 'select' button pressed, enter in editing mode modify time. up/down/right/left used modify hour, minute , second digit. pressing select again, sets new time , show current time again.
i put alarm specifc time, star work in menu set alarm. doesn't work. have tried example library web site, , worked, when put in code, doesn't.
the example code can found here: http://www.pjrc.com/teensy/td_libs_timealarms.html
i'm using arduino mega 2560 - lcd shield has compatible pins it: http://www.dx.com/p/118059
here code:
code: [select]
#include <liquidcrystal.h>
#include <time.h>
#include <timealarms.h>
int led = 13;
//controla o menu
const int menu_none = 0;
const int menu_hour = 1;
int currentmenu = 0;
// initialize lcd
liquidcrystal lcd(8, 9, 4, 5, 6, 7);
//used read analog buttons
int buttonvoltage = 0;
int buttonpressed = 0;
int lastbutton = 7;
//control button debounce
long lastdebouncetime = 0;
long debouncedelay = 30;
//hour control
int hourediting = 0; // 0 - hour, 1 - minuto, 2
int hour;
int min;
int sec;
boolean wasedited = false; // flag para definir se hour foi ou não editada
void setup() {
serial.begin(9600);
delay(50);
serial.write("init\n\r");
//setup lcd
lcd.begin(16, 2);
lcd.clear();
setupclock(10,10,10);
printnow();
alarm.alarmrepeat(10,10,15, turnledon);
}
void setupclock(int h, int m, int s){
settime(h,m,s,10,04,2013);
}
//prints current time
void printnow(){
printtime(hour(),minute(),second());
}
//print specifictime:
void printtime(int h,int m,int s){
char time[8];
lcd.setcursor(8,0);
sprintf(time,"%02d:%02d:%02d",h, m, s);
lcd.print(time);
}
/*
places cursor on hour, minute or second digit.
the cursor blinking indicating digit will
be changed
*/
void placehourcursor(){
switch(hourediting){
case 0:
lcd.setcursor(8,0);
break;
case 1:
lcd.setcursor(11,0);
break;
case 2:
lcd.setcursor(14,0);
break;
}
}
//stores current time
void savecurrenthour(){
hour = hour();
min = minute();
sec = second();
}
/*
adjusts time. dir direction (up or down);
depending on cursor is, increment or
decrement hour, minute or second digit.
*/
void adjusthour(int dir){
wasedited = true;
char digito[2];
switch(hourediting){
case 0: //hour
hour = getincremento(hour, 23, dir);
break;
case 1: //minuto
min = getincremento(min, 59, dir);
break;
case 2: //segundo
sec = getincremento(sec, 59, dir);
break;
}
printtime(hour,min,sec);
}
/*
general purpose funtion.
increments or decrements number considering minumum (zero) ,
maximum (maxinc) value. dir direction (up or down).
*/
int getincremento(int number, int maxinc, int dir){
if(dir == 0){ //incremento
if(number == maxinc) {
number = 0;
}
else{
number++;
}
}
else{ //decremento
if(number == 0) {
number = maxinc;
}
else{
number--;
}
}
return number;
}
/*
handles button pressed.
we have 5 buttons: up, down, right, left , select.
when selected pressed, , currentmenu none,
we enter in time editing mode. cursor starts blink in
the hour digit. pressing , down increment or decrement
the hour. pressing right , left move cursor other
digits (minute , second).
when select pressed again, if time modified, clock is
adusjted , starts show current time again.
*/
void readbutton(int btn){
switch (btn)
{
case 0: //right
if(currentmenu == menu_hour){
hourediting = getincremento(hourediting, 2, 0);
}
break;
case 1: //up
if(currentmenu == menu_hour){
adjusthour(0);
}
break;
case 2: //down
if(currentmenu == menu_hour){
adjusthour(1);
}
break;
case 3: //left
if(currentmenu == menu_hour){
hourediting = getincremento(hourediting, 2, 1);
}
break;
case 5: //select
if(currentmenu == menu_hour){
lcd.noblink();
currentmenu = menu_none;
if(wasedited){
setupclock(hour, min, sec);
}
wasedited = false;
}
else if (currentmenu == menu_none){
currentmenu = menu_hour;
savecurrenthour();
lcd.blink();
}
break;
}
if(currentmenu == menu_hour){
//if i'm editing hour, place blinking cursor
placehourcursor();
}
else{
//for now.. fo nothing;
}
delay(200);
}
void turnledon(){
serial.write("on\n\r");
}
void loop() {
buttonvoltage = analogread(0);
buttonpressed = buttonvoltage >> 7; //takes significant bit.
if(buttonpressed != lastbutton){
lastdebouncetime = millis();
}
if ((millis() - lastdebouncetime) > debouncedelay) {
readbutton(buttonpressed);
}
lastbutton = buttonpressed;
if(currentmenu == menu_none){
printnow();
}
}
you don't have call alarm.delay() in code.
Arduino Forum > Using Arduino > Programming Questions > [SOLVED] Alarm library not working.
arduino
Comments
Post a Comment