Pages: [1]   Go Down
Author Topic: RTC causing program problems  (Read 308 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm experiencing some program running problem when i uncomment the RTC function to read date and time for program save it with data to a log file. I'm getting the data by RF with virtualwire and if program runs with RTC uncomment i can't even get the data it shows in serial monitor something like "ggggggggggggggggggggggggggggggg..." and keeps going for eternity.
I i comment in everithing works just fine.

Code:
#include <SPI.h>
#include <avr/pgmspace.h>
#include <Ethernet.h>
#include <VirtualWire.h>
#include <Wire.h>
#include "SdFatUtil.h"

#define DS1307_I2C_Endereco 0x68  //SCL<--> Analog Input 5  SDA<--> Analog Input 4
#define I2C_Write Wire.write
#define I2C_Read Wire.read

prog_char noMes_0[] PROGMEM = "";
prog_char noMes_1[] PROGMEM = "Jan";
prog_char noMes_2[] PROGMEM = "Fev";
prog_char noMes_3[] PROGMEM = "Mar";
prog_char noMes_4[] PROGMEM = "Abr";
prog_char noMes_5[] PROGMEM = "Mai";
prog_char noMes_6[] PROGMEM = "Jun";
prog_char noMes_7[] PROGMEM = "Jul";
prog_char noMes_8[] PROGMEM = "Ago";
prog_char noMes_9[] PROGMEM = "Set";
prog_char noMes_10[] PROGMEM = "Out";
prog_char noMes_11[] PROGMEM = "Nov";
prog_char noMes_12[] PROGMEM = "Dez";
PROGMEM const char* noMes[] = {
  noMes_0, noMes_1, noMes_2, noMes_3, noMes_4, noMes_5, noMes_6, noMes_7, noMes_8, noMes_9, noMes_10,
  noMes_11, noMes_12};

byte segundo, minuto, hora, diaSemana, diaMes, mes, ano;
char nomeMes[10];
char ficheiroLer[10];
char mensagem[30];
unsigned int dadosAgua = 0;
float dadosBat = 0.0;
float percentagem = 0.0;
unsigned long litros = 0;

void setup() {

  Serial.begin(9600);
  delay(100);

  //DEFINE OS PINOS DE ENTRADA E DE SAÍDA
  pinMode(ledInfo, OUTPUT);
  pinMode(ledRececao, OUTPUT);
  pinMode(10, OUTPUT);

  //INICIA A COMUNICAÇÃO I2C
  Wire.begin();

  //LIGA E DESLIGA OS 2 LEDS PARA INDICAR QUE ESTÃO OPERACIONAIS
  digitalWrite(ledInfo, HIGH);
  digitalWrite(ledRececao, HIGH);
  digitalWrite(10, HIGH);
  delay(1000);
  digitalWrite(ledInfo, LOW);
  digitalWrite(ledRececao, LOW);
Ethernet.begin(mac, ip);
  server.begin();
  delay(100);

  //CONFIGURA E INICIA O RECEPTOR
  vw_set_tx_pin(6);
  vw_set_rx_pin(8);
  vw_set_ptt_pin(5);
  vw_setup(2000);
  vw_rx_start();

void loop() {

  //actDataHora();
  rececaoRF();
  clienteLigado();
  
}

void actDataHora() {

  //FAZ O STACK POINTER IR PARA O INÍCIO
  Wire.beginTransmission(DS1307_I2C_Endereco);
  I2C_Write(0x00);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_I2C_Endereco, 7);

  segundo  = bcdToDec(I2C_Read() & 0x7f); //MASCARA PQ É BIT DE CONTROLE
  minuto   = bcdToDec(I2C_Read());
  hora     = bcdToDec(I2C_Read() & 0x3f); //MÁSCARA PQ É BIT DE CONTROLE
  diaSemana= bcdToDec(I2C_Read());
  diaMes   = bcdToDec(I2C_Read());
  mes      = bcdToDec(I2C_Read());
  ano      = bcdToDec(I2C_Read());
}

//CONVERSÃO DOS DADOS DE BCD PARA DECIMAL - RTC
byte bcdToDec(byte val)
{
  return ((val/16*10) + (val%16));
}

//RECEPÇÃO DOS DADOS POR RF 433MHz
void rececaoRF() {

  byte buf[VW_MAX_MESSAGE_LEN];
  byte buflen = VW_MAX_MESSAGE_LEN;
 
  if (vw_get_message(buf, &buflen)) {
    digitalWrite(ledRececao, HIGH);
    for (int j = 0; j<buflen; j++) {
      mensagem[j] = char(buf[j]);
    }
    mensagem[buflen] = '\0';

    if (mensagem[0] == 'R') {
      dadosAgua = atoi(&mensagem[1]);
      if (dadosAgua > 0 && dadosAgua <= 465) {
        calcAgua();
        //Serial.println(dadosAgua);
      }
    }
    else if (mensagem[0] == 'B') {
      dadosBat = atof(&mensagem[1]);
      if (dadosBat > 0.00 && dadosBat <= 9.99) {
        gravarDados();
        //Serial.println(dadosBat);
      }
    }
    Serial.println(mensagem);
    memset(&buf, 0, sizeof(buf));
    memset(&mensagem, 0, sizeof(mensagem));
    digitalWrite(ledRececao, LOW);
  }
}


void gravarDados() {

  char ficheiroLog[10];
  strcpy_P(nomeMes, (char*)pgm_read_word(&(noMes[mes])));
  sprintf(ficheiroLog,"%s.csv", nomeMes);
  boolean Log = false;
  Serial.println(ficheiroLog);
  Log = file.open(&root, ficheiroLog, O_WRITE | O_APPEND);

  if (Log) {
    actDataHora();
    //DATA E HORA
    file.print(diaMes, DEC);
    file.print(" ");
    file.print(nomeMes);
    file.print(" ");
    file.print("20");
    if (ano < 10) {
      file.print("0");
    }
    file.print(ano, DEC);
    file.print(" , ");
    if (hora < 10) {
      file.print("0");
    }
    file.print(hora, DEC);
    file.print(":");
    if (minuto < 10) {
      file.print("0");
    }
    file.print(minuto, DEC);
    file.print(":");
    if (segundo < 10) {
      file.print("0");
    }
    file.print(segundo, DEC);
    //DADOS    
    file.print(" , ");
    file.print(percentagem, 1);
    file.print(" , ");
    file.println(litros);
    file.close();
    Log = false;
  }
  else {
    Serial.println(F("ERRO grav LOG!"));
    for (int i = 0; i<5; i++) {
      digitalWrite(ledRececao, HIGH);
      delay(250);
      digitalWrite(ledRececao, LOW);
      delay(250);
    }
  }
  memset(&ficheiroLog, 0, sizeof(ficheiroLog));
}
I haven't posted the whole code because it is a little big, but i think that i've posted the most important parts.
I'm using RTC library from http://combustory.com/wiki/index.php/RTC1307_-_Real_Time_Clock
Thanks in advance for your help!
« Last Edit: May 14, 2013, 01:03:42 pm by blastboot » Logged

0
Offline Offline
Shannon Member
****
Karma: 215
Posts: 12525
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If the code is big it could simply be you've run out of RAM space when using the RTC code as well...
Logged

[ I won't respond to messages, use the forum please ]

Offline Offline
Full Member
***
Karma: 0
Posts: 106
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm using arduino mega. it is not ram problem because i've already do a lots of debugg and it only happens when i "turn on" rtc. It could be some variable overload but i'm using everything lalmost like it is on the rtc page i've posted. If there is any better library to use please tell me
Logged

Sydney, Australia
Offline Offline
Sr. Member
****
Karma: 6
Posts: 397
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are using the mega shouldn;t you be using pin 20 and pin 21 for i2c - not pin4 and 5 ?

I just went through a whole heap of stuff over the last two days trying to find THE RIGHT library to use with the 1307

It does work when the pins are right

Craig
Logged

Pages: [1]   Go Up
Jump to: