Go Down

Topic: Controlador de Aquario - RTC (Read 906 times) previous topic - next topic

DotComBoy

Fala pessoal beleza?
Estou começando agora no mundo do arduino..
comecei a fazer um controlador para meu aquario.. o basico feito ate agora é um relogio RTC hehe.. ja mostra a hora corretamente e tal..

estou me batendo pra fazer o setup do horario..

segue o código feito ate agora... o que posso melhorar?
Code: [Select]


// include the library code:
#include <LiquidCrystal.h>


// these constants won't change.  But you can change the size of
// your LCD using them:
const int numRows = 2;
const int numCols = 16;

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 2, 7, 8, 9, 10);

#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68  // This is the I2C address


// Global Variables

int command = 0;       // This is the command char, in ascii form, sent from the serial port    
int i;
long previousMillis = 0;        // will store last time Temp was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;
byte stemp = -10; //segundo temp

const int bsel = 4;
const int bmais = 5;

int tsel = -1;
int tmais = -1;

// 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, Probably need to put in checks for valid numbers.

void setDateDs1307()                
{

 second = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result.  
 minute = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
 hour  = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
 dayOfWeek = (byte) (Serial.read() - 48);
 dayOfMonth = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
 month = (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
 year= (byte) ((Serial.read() - 48) *10 +  (Serial.read() - 48));
 Wire.beginTransmission(DS1307_I2C_ADDRESS);
 Wire.send(0x00);
 Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
 Wire.send(decToBcd(minute));
 Wire.send(decToBcd(hour));      // If you want 12 hour am/pm you need to set
 // bit 6 (also need to change readDateDs1307)
 Wire.send(decToBcd(dayOfWeek));
 Wire.send(decToBcd(dayOfMonth));
 Wire.send(decToBcd(month));
 Wire.send(decToBcd(year));
 Wire.endTransmission();
}

// Gets the date and time from the ds1307 and prints result
void getDateDs1307()
{
 // Reset the register pointer
 Wire.beginTransmission(DS1307_I2C_ADDRESS);
 Wire.send(0x00);
 Wire.endTransmission();

 Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

 // A few of these need masks because certain bits are control bits
 second     = bcdToDec(Wire.receive() & 0x7f);
 minute     = bcdToDec(Wire.receive());
 hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
 dayOfWeek  = bcdToDec(Wire.receive());
 dayOfMonth = bcdToDec(Wire.receive());
 month      = bcdToDec(Wire.receive());
 year       = bcdToDec(Wire.receive());
 if (second != stemp){/*;*/
   m_hora(hour,minute,second);
   stemp = second;
 }

}



void setup() {
 Wire.begin();
 Serial.begin(9600);
 lcd.begin(numRows, numCols);
 pinMode(bsel, INPUT);           // set pin to input
 pinMode(bmais, INPUT);  // set pin to input
 digitalWrite(bsel, HIGH);
 digitalWrite(bmais,HIGH);


}
void m_hora(int h, int m, int s) {
 lcd.clear();
 lcd.setCursor(0, 1);  
 lcd.print(h,DEC);
 lcd.print(":");
 lcd.print(m,DEC);
 lcd.print(":");
 lcd.print(s,DEC);
}

void teclas(){
 while(digitalRead(bsel) == LOW){
 delay(10);
 }
while(digitalRead(bsel) == HIGH){
 if (digitalRead(bmais) == LOW ){
   second = second+1;
   m_hora(hour,minute,second);
   delay(50);
   
 }
}
delay(50);
}
void loop() {

 getDateDs1307();
 //tsel = digitalRead(bsel);
 if (digitalRead(bsel) == LOW ){
   delay(1000);
   if (digitalRead(bsel) == LOW ){
   teclas();
 }
 }
}



Valeu.. abraços

3d29

Seu código tem vários "Serial.read()" na função de setar o horário, qual é o seu intuito ?

Voce vai setar o horário sempre ou quer setar apenas uma vez? (está usando o ds1307 sozinho ou módulo RTC da sparkfun?)

Abraço

Go Up