Boa tarde pessoal, não sou programador, mas estou projetando um relógio digital para corrida de orientação, aquele com BIP de advertencia aos 55, ...60 segundos. Bom, primeiro, consegui um código de relógio, porém, após alguns minutos os leds se apagam, tenho que resetar o arduino para voltar a mostrar a hora, não o que pode ser. Depois, inseri uma emenda no código, rs, como vocês poderão ver mais abaixo que não funciona como planejo, quero que nos segundos 55 até o 60 toque um BIP (tom), só que quando este comando roda os leds param voltando com a hora correta após os tons. Alguém poderi me ajudar com esta empreitada? segue abaixo o código:
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68
#define NOTA_SI 600
#include "LedControl.h" // need the library
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
float t=0;
int a,b;
int pinoFalante = 8;
LedControl lc=LedControl(12,11,10,1); // lc is our object
// pin 12 is connected to the MAX7219 pin 1
// pin 11 is connected to the CLK pin 13
// pin 10 is connected to LOAD pin 12
// 1 as we are only using 1 MAX7219
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
return ( (val/10*16) + (val%10) );
}
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
return ( (val/16*10) + (val%16) );
}
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers
void setDateDs1307(byte second, // 0-59
byte minute, // 0-59
byte hour, // 1-23
byte dayOfWeek, // 1-7
byte dayOfMonth, // 1-28/29/30/31
byte month, // 1-12
byte year) // 0-99
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.write(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour));
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.write(0x10); // sends 0x10 (hex) 00010000 (binary) to control register - turns on square wave
Wire.endTransmission();
}
// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void setup()
{
// the zero refers to the MAX7219 number, it is zero for 1 chip
lc.shutdown(0,false);// turn off power saving, enables display
lc.setIntensity(0,8);// sets brightness (0~15 possible values)
lc.clearDisplay(0);// clear screen
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
// Change these values to what you want to set your clock to.
// You probably only want to set your clock once and then remove
// the setDateDs1307 call.
second = 0;
minute = 56;
hour = 9;
dayOfWeek = 5;
dayOfMonth = 9;
month = 7;
year = 10;
// setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}
void loop()
{
lc.setChar(0,2,'-',false);
lc.setChar(0,5,'-',false);
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
if (hour<10)
{
lc.setDigit(0,0,0,false);
lc.setDigit(0,1,hour,false);
} else if (hour>=10)
{
t=hour/10;
a=int(t);
lc.setDigit(0,0,a,false);
t=hour%10;
lc.setDigit(0,1,t,false);
}
if (minute<10)
{
lc.setDigit(0,3,0,false);
lc.setDigit(0,4,minute,false);
} else if (minute>=10)
{
t=minute/10;
a=int(t);
lc.setDigit(0,3,a,false);
t=minute%10;
lc.setDigit(0,4,t,false);
}
if (second<10)
{
lc.setDigit(0,6,0,false);
lc.setDigit(0,7,second,false);
} else if (second>=10)
{
t=second/10;
a=int(t);
lc.setDigit(0,6,a,false);
t=second%10;
lc.setDigit(0,7,t,false);
}
if (second>=55)
{
pinMode(pinoFalante, OUTPUT);
tone(pinoFalante, NOTA_SI);
delay(200);
noTone(pinoFalante);
delay(800);
}
if(second==00)
{
pinMode(pinoFalante, OUTPUT);
noTone(pinMode);
}