collapse

* Posts Recentes

Arame de Estendal por almamater
[Ontem às 16:16]


O que é isto ? por SerraCabo
[12 de Abril de 2024, 14:20]


Amplificador - Rockboard HA 1 In-Ear por almamater
[11 de Abril de 2024, 20:46]


Emulador NES em ESP32 por dropes
[10 de Abril de 2024, 15:30]


Meu novo robô por josecarlos
[29 de Março de 2024, 18:30]


Bateria - Portátil por almamater
[25 de Março de 2024, 22:14]


Escolher Osciloscópio por jm_araujo
[06 de Fevereiro de 2024, 23:07]


TP4056 - Dúvida por dropes
[31 de Janeiro de 2024, 14:13]


Leitura de dados por Porta Serie por jm_araujo
[22 de Janeiro de 2024, 14:00]


Distancia Cabo por jm_araujo
[08 de Janeiro de 2024, 16:30]

Autor Tópico: Iniciante - juntar codigos  (Lida 23010 vezes)

0 Membros e 1 Visitante estão a ver este tópico.

StarRider

  • Visitante
Re: Iniciante - juntar codigos
« Responder #15 em: 16 de Janeiro de 2015, 22:19 »
Já consegui. mas tive de remover da função a palavra" sensor"

Código: [Seleccione]
f for(int x=0;x<sensors;x++) {
    Serial.println(" ");
    buf += " ";

   
    Serial.print(" ");
    buf += " ";
    Serial.print(get_temperature(sensors_address[x]));
    buf += get_temperature(sensors_address[x]);

    Serial.println(" C");
    buf += " C";

E aprendeste alguma coisa ? Percebes porque é que não funcionada com a palavra "sensor" ?

Offline MRData

  • Mini Robot
  • *
  • Mensagens: 81
Re: Iniciante - juntar codigos
« Responder #16 em: 16 de Janeiro de 2015, 22:25 »
Tiveste que apagar a palavra sensor porque esgotavas as letras da primeira linha, porque estas a escrever tudo linear...
Convem fazeres alterações ao teu codigo para poderes escrever em varias linhas.

Já consegui. mas tive de remover da função a palavra" sensor"

Código: [Seleccione]
f for(int x=0;x<sensors;x++) {
    Serial.println(" ");
    buf += " ";

   
    Serial.print(" ");
    buf += " ";
    Serial.print(get_temperature(sensors_address[x]));
    buf += get_temperature(sensors_address[x]);

    Serial.println(" C");
    buf += " C";
"We are legion"

Offline MRData

  • Mini Robot
  • *
  • Mensagens: 81
Re: Iniciante - juntar codigos
« Responder #17 em: 16 de Janeiro de 2015, 22:30 »
Porque no fim da tua função colocaste um delay(2000); que corresponde a teres de esperar 2 segs...
Tenta apagar essa linha..
E deves ter outros delays...
"We are legion"

Offline senso

  • Global Moderator
  • Mini Robot
  • *****
  • Mensagens: 9.733
  • Helpdesk do sitio
Re: Iniciante - juntar codigos
« Responder #18 em: 16 de Janeiro de 2015, 22:39 »
Espero bem que não, porque isso significa que o código é mesmo muito horrivel, por outro lado, não me espanta que demore 15 dias a actualizar o ecrã e que mal mandes escrever no ecrã o estejas a mandar apagar tudo.
Avr fanboy

Offline MRData

  • Mini Robot
  • *
  • Mensagens: 81
Re: Iniciante - juntar codigos
« Responder #19 em: 16 de Janeiro de 2015, 22:44 »
Coloca aqui o codigo pff.
"We are legion"

StarRider

  • Visitante
Re: Iniciante - juntar codigos
« Responder #20 em: 16 de Janeiro de 2015, 22:46 »
Ja tirei mas continua. Não vejo mais onde possa mexer. A frase está mais tempo apagada do que a exibir os valores. Será que é esse o tempo de o arduino correr o codigo?

Em parte sim, tens por ai redundância a dizer chega ...

Começa por retirar o loop "for" para ler os sensores pois se só tens um de nada serve :)

Offline MRData

  • Mini Robot
  • *
  • Mensagens: 81
Re: Iniciante - juntar codigos
« Responder #21 em: 16 de Janeiro de 2015, 22:55 »
Faz uma coisa primeiro, tira a parte de ler a temperatura e ve se actualiza de seg em seg
"We are legion"

Offline MRData

  • Mini Robot
  • *
  • Mensagens: 81
Re: Iniciante - juntar codigos
« Responder #22 em: 16 de Janeiro de 2015, 23:13 »
Substitui o teu projecto por este :
Eu modifiquei, certifica-te que o delay esta a seguir ao escrever para o ecra.

Código: [Seleccione]
#include "Wire.h"
#include <UTFT.h>
#include <OneWire.h>


extern uint8_t SmallFont[];

#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
const int sensors = 1; //number of temperature sensors on the bus
int sensors_pin = 10; //the pin where the bus its connected to arduino
uint8_t sensors_address[sensors][8]; //here will store the sensors addresses for later use

OneWire  sensor_bus(sensors_pin);

float get_temperature (uint8_t *address);


UTFT myGLCD(ITDB32S,38,39,40,41);

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) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  setDS3231time(30,21,19,6,6,01,15);
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(30,40,18,5,26,11,14);
  randomSeed(analogRead(0));


  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);

  int x,y,c=0;
  Serial.println("Starting to look for sensors...");
  for(x=0;x<sensors;x++){
    if(sensor_bus.search(sensors_address[x]))
      c++;
  }
  if(c > 0) {
    Serial.println("Found this sensors : ");
    for(x=0;x<sensors;x++) {
      Serial.print("\tSensor ");
      Serial.print(x+1);
      Serial.print(" at address : ");
      for(y=0;y<8;y++){
        Serial.print(sensors_address[x][y],HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
  }
  else
    Serial.println("Didn't find any sensors");
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

String displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  String buf = "";
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  buf += hour;
  // convert the byte variable to a decimal number when displayed
  Serial.print(" :");
  buf += ":";
  if (minute<10)
  {
    Serial.print("0");
    buf += "0";
  }
  Serial.print(minute, DEC);
  buf += minute;
  Serial.print(":");
  buf += ":";
  if (second<10)
  {
    Serial.print("0");
    buf += "0";
    buf += "0";
  }
  Serial.print(second, DEC);
  buf += second;
  Serial.print(" ");
  buf += " ";
  Serial.print(dayOfMonth, DEC);
  buf += dayOfMonth;
  Serial.print("/");
  buf += "/";
  Serial.print(month, DEC);
  buf += month;
  Serial.print("/");
  buf += "/";
  Serial.print(year, DEC);
  buf += year;
  Serial.print(" ");
  buf += " ";
  switch(dayOfWeek){
  case 1:
    Serial.println("Domingo");
    buf += "Domingo";
    break;
  case 2:
    Serial.println("Segunda");
    buf += "Segunda";
    break;
  case 3:
    Serial.println("Terça");
    buf += "Terça";
    break;
  case 4:
    Serial.println("Quarta");
    buf += "Quarta";
    break;
  case 5:
    Serial.println("Quinta");
    buf += "Quinta";
    break;
  case 6:
    Serial.println("Sexta");
    buf += "Sexta";
    break;
  case 7:
    Serial.println("Sabado");
    buf += "Sabado";
    break;
  }

  for(int x=0;x<sensors;x++) {
    Serial.println(" ");
    buf += " ";

   
    Serial.print(" ");
    buf += " ";
    Serial.print(get_temperature(sensors_address[x]));
    buf += get_temperature(sensors_address[x]);

    Serial.println(" C");
    buf += " C";
  }
 
  return buf;
}


float get_temperature(uint8_t *address) {
  byte data[12];
  int x;
  sensor_bus.reset();
  sensor_bus.select(address);
  sensor_bus.write(0x44,1);

  sensor_bus.reset();
  sensor_bus.select(address);
  sensor_bus.write(0xBE,1);
  for(x=0;x<9;x++){
    data[x] = sensor_bus.read();
  }


  unsigned short tr = data[0];
  tr = tr + data[1]*256;

  if(tr > 0xF000){
    tr = 0 - tr;
  }


  float temperature = (float)tr*(float)0.0625 ;

  return temperature;
}



void loop()
{
  displayTime(); // display the real-time clock data on the Serial Monitor,


  int buf[318];
  int x, x2;
  int y, y2;
  int r;


 

  myGLCD.setColor(255, 0, 0);
  myGLCD.fillRect(0, 0, 319, 13);
  myGLCD.setColor(64, 64, 64);
  myGLCD.setBackColor(255, 0, 0);

  myGLCD.print(displayTime(), CENTER, 1) ;
delay(1000);



 

}


"We are legion"

StarRider

  • Visitante
Re: Iniciante - juntar codigos
« Responder #23 em: 16 de Janeiro de 2015, 23:35 »
Boas novamente,

Preferia dar uma cana em vez de dar um peixe, mas agora também estou curioso.

Experimenta este código:
Tem em atenção que não experimentei o mesmo (não tenho o IDE do arduino instalado nem sequer o
avr-gcc pois uso outra toolchian) , pelo que se der erros de compilação diz qualquer coisa.

Código: [Seleccione]
#include "Wire.h"
#include <UTFT.h>
#include <OneWire.h>

/*----- externals */
extern uint8_t SmallFont[];

#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal

/*----- locals */
int sensors_pin = 10; //the pin where the bus its connected to arduino
uint8_t sensor_addres[8]; //here will store the sensors addresses for later use
OneWire  sensor_bus(sensors_pin);
float get_temperature(uint8_t *address);
char *daynames[] = {"Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sabado"};
UTFT myGLCD(ITDB32S,38,39,40,41);
char timetempbuf[64];
byte sec, min, hour, dow, dom, mon, year;


/*---------------------------------------------------------*/
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}

/*---------------------------------------------------------*/
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}

/*---------------------------------------------------------*/
void setup()
{
Wire.begin();
Serial.begin(9600);
setDS3231time(30,21,19,6,6,01,15);
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
// setDS3231time(30,40,18,5,26,11,14);
randomSeed(analogRead(0));

myGLCD.InitLCD();
myGLCD.setFont(SmallFont);

Serial.println("Starting to look for sensors...");
if(sensor_bus.search(sensor_addres))
Serial.println("Found sensor at address : " + sensor_addres);
else
Serial.println("Didn't find any sensors");
}

/*---------------------------------------------------------*/
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}

/*---------------------------------------------------------*/
void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}


/*---------------------------------------------------------*/
float get_temperature(uint8_t *address)
{
byte data[12];
int x;
sensor_bus.reset();
sensor_bus.select(address);
sensor_bus.write(0x44,1);

sensor_bus.reset();
sensor_bus.select(address);
sensor_bus.write(0xBE,1);
for(x=0;x<9;x++){
data[x] = sensor_bus.read();
}

unsigned short tr = data[0];
tr = tr + data[1]*256;

if(tr > 0xF000){
tr = 0 - tr;
}

float temperature = (float)tr*(float)0.0625 ;
return temperature;
}


/*---------------------------------------------------------*/
void loop()
{
readDS3231time(&sec, &min, &hour, &dow, &dom, &mon, &year);
        sprintf(timetempbuf,"%02d:%02d:%02d  %02d/%02d/%04d %s %4.1fC\r\n",hour, min, sec, dom, mon, year, dayname[dow],get_temperature(sensor_addres));

Serial.println(timetempbuf);
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, 319, 13);
myGLCD.setColor(64, 64, 64);
myGLCD.setBackColor(255, 0, 0);
myGLCD.print(timetempbuf, CENTER, 1) ;

delay(1000); // every second

}

/*---------------------------------------------------------*/
/* eof */
« Última modificação: 16 de Janeiro de 2015, 23:36 por StarRider »

StarRider

  • Visitante
Re: Iniciante - juntar codigos
« Responder #24 em: 16 de Janeiro de 2015, 23:40 »
StarRider, deu erro 

 This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.
Arduino: 1.0.6 (Windows NT (unknown)), Board: "Arduino Mega 2560 or Mega ADK"
sketch_jan16b.ino: In function 'void setup()':
sketch_jan16b:50: error: invalid operands of types 'const char [27]' and 'uint8_t [8]' to binary 'operator+'
sketch_jan16b.ino: In function 'void loop()':
sketch_jan16b:121: error: 'dayname' was not declared in this scope

Ok, deixa-me ver ... estou a apalpar ....

Offline MRData

  • Mini Robot
  • *
  • Mensagens: 81
Re: Iniciante - juntar codigos
« Responder #25 em: 16 de Janeiro de 2015, 23:41 »
Testa isto pff.
Enquanto o Star corrige o erro.

Código: [Seleccione]
#include "Wire.h"
#include <UTFT.h>
#include <OneWire.h>


extern uint8_t SmallFont[];

#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
const int sensors = 1; //number of temperature sensors on the bus
int sensors_pin = 10; //the pin where the bus its connected to arduino
uint8_t sensors_address[sensors][8]; //here will store the sensors addresses for later use

OneWire  sensor_bus(sensors_pin);

float get_temperature (uint8_t *address);


UTFT myGLCD(ITDB32S,38,39,40,41);

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) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  setDS3231time(30,21,19,6,6,01,15);
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(30,40,18,5,26,11,14);
  randomSeed(analogRead(0));


  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);

  int x,y,c=0;
  Serial.println("Starting to look for sensors...");
  for(x=0;x<sensors;x++){
    if(sensor_bus.search(sensors_address[x]))
      c++;
  }
  if(c > 0) {
    Serial.println("Found this sensors : ");
    for(x=0;x<sensors;x++) {
      Serial.print("\tSensor ");
      Serial.print(x+1);
      Serial.print(" at address : ");
      for(y=0;y<8;y++){
        Serial.print(sensors_address[x][y],HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
  }
  else
    Serial.println("Didn't find any sensors");
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

String displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  String buf = "";
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  buf += hour;
  // convert the byte variable to a decimal number when displayed
  Serial.print(" :");
  buf += ":";
  if (minute<10)
  {
    Serial.print("0");
    buf += "0";
  }
  Serial.print(minute, DEC);
  buf += minute;
  Serial.print(":");
  buf += ":";
  if (second<10)
  {
    Serial.print("0");
    buf += "0";
    buf += "0";
  }
  Serial.print(second, DEC);
  buf += second;
  Serial.print(" ");
  buf += " ";
  Serial.print(dayOfMonth, DEC);
  buf += dayOfMonth;
  Serial.print("/");
  buf += "/";
  Serial.print(month, DEC);
  buf += month;
  Serial.print("/");
  buf += "/";
  Serial.print(year, DEC);
  buf += year;
  Serial.print(" ");
  buf += " ";
  switch(dayOfWeek){
  case 1:
    Serial.println("Domingo");
    buf += "Domingo";
    break;
  case 2:
    Serial.println("Segunda");
    buf += "Segunda";
    break;
  case 3:
    Serial.println("Terça");
    buf += "Terça";
    break;
  case 4:
    Serial.println("Quarta");
    buf += "Quarta";
    break;
  case 5:
    Serial.println("Quinta");
    buf += "Quinta";
    break;
  case 6:
    Serial.println("Sexta");
    buf += "Sexta";
    break;
  case 7:
    Serial.println("Sabado");
    buf += "Sabado";
    break;
  }

  for(int x=0;x<sensors;x++) {
    Serial.println(" ");
    buf += " ";

   
    Serial.print(" ");
    buf += " ";
    Serial.print(get_temperature(sensors_address[x]));
    buf += get_temperature(sensors_address[x]);

    Serial.println(" C");
    buf += " C";
  }
 
  return buf;
}


float get_temperature(uint8_t *address) {
  byte data[12];
  int x;
  sensor_bus.reset();
  sensor_bus.select(address);
  sensor_bus.write(0x44,1);

  sensor_bus.reset();
  sensor_bus.select(address);
  sensor_bus.write(0xBE,1);
  for(x=0;x<9;x++){
    data[x] = sensor_bus.read();
  }


  unsigned short tr = data[0];
  tr = tr + data[1]*256;

  if(tr > 0xF000){
    tr = 0 - tr;
  }


  float temperature = (float)tr*(float)0.0625 ;

  return temperature;
}



void loop()
{
  int buf[318];
  int x, x2;
  int y, y2;
  int r;


 

  myGLCD.setColor(255, 0, 0);
  myGLCD.fillRect(0, 0, 319, 13);
  myGLCD.setColor(64, 64, 64);
  myGLCD.setBackColor(255, 0, 0);

  myGLCD.print(displayTime(), CENTER, 1) ;
  delay(1000);
}

"We are legion"

Offline MRData

  • Mini Robot
  • *
  • Mensagens: 81
Re: Iniciante - juntar codigos
« Responder #26 em: 16 de Janeiro de 2015, 23:48 »
Ja agora testa so mais este pff.
Não vai dar os dados da temperatura mas é so para ver se as coisas aparecem no ecra correctamente.

Código: [Seleccione]
#include "Wire.h"
#include <UTFT.h>
#include <OneWire.h>


extern uint8_t SmallFont[];

#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
const int sensors = 1; //number of temperature sensors on the bus
int sensors_pin = 10; //the pin where the bus its connected to arduino
uint8_t sensors_address[sensors][8]; //here will store the sensors addresses for later use

OneWire  sensor_bus(sensors_pin);

float get_temperature (uint8_t *address);


UTFT myGLCD(ITDB32S,38,39,40,41);

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) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  setDS3231time(30,21,19,6,6,01,15);
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(30,40,18,5,26,11,14);
  randomSeed(analogRead(0));


  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);

  int x,y,c=0;
  Serial.println("Starting to look for sensors...");
  for(x=0;x<sensors;x++){
    if(sensor_bus.search(sensors_address[x]))
      c++;
  }
  if(c > 0) {
    Serial.println("Found this sensors : ");
    for(x=0;x<sensors;x++) {
      Serial.print("\tSensor ");
      Serial.print(x+1);
      Serial.print(" at address : ");
      for(y=0;y<8;y++){
        Serial.print(sensors_address[x][y],HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
  }
  else
    Serial.println("Didn't find any sensors");
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

String displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  String buf = "";
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  buf += hour;
  // convert the byte variable to a decimal number when displayed
  Serial.print(" :");
  buf += ":";
  if (minute<10)
  {
    Serial.print("0");
    buf += "0";
  }
  Serial.print(minute, DEC);
  buf += minute;
  Serial.print(":");
  buf += ":";
  if (second<10)
  {
    Serial.print("0");
    buf += "0";
    buf += "0";
  }
  Serial.print(second, DEC);
  buf += second;
  Serial.print(" ");
  buf += " ";
  Serial.print(dayOfMonth, DEC);
  buf += dayOfMonth;
  Serial.print("/");
  buf += "/";
  Serial.print(month, DEC);
  buf += month;
  Serial.print("/");
  buf += "/";
  Serial.print(year, DEC);
  buf += year;
  Serial.print(" ");
  buf += " ";
  switch(dayOfWeek){
  case 1:
    Serial.println("Domingo");
    buf += "Domingo";
    break;
  case 2:
    Serial.println("Segunda");
    buf += "Segunda";
    break;
  case 3:
    Serial.println("Terça");
    buf += "Terça";
    break;
  case 4:
    Serial.println("Quarta");
    buf += "Quarta";
    break;
  case 5:
    Serial.println("Quinta");
    buf += "Quinta";
    break;
  case 6:
    Serial.println("Sexta");
    buf += "Sexta";
    break;
  case 7:
    Serial.println("Sabado");
    buf += "Sabado";
    break;
  } 
  return buf;
}


float get_temperature(uint8_t *address) {
  byte data[12];
  int x;
  sensor_bus.reset();
  sensor_bus.select(address);
  sensor_bus.write(0x44,1);

  sensor_bus.reset();
  sensor_bus.select(address);
  sensor_bus.write(0xBE,1);
  for(x=0;x<9;x++){
    data[x] = sensor_bus.read();
  }


  unsigned short tr = data[0];
  tr = tr + data[1]*256;

  if(tr > 0xF000){
    tr = 0 - tr;
  }


  float temperature = (float)tr*(float)0.0625 ;

  return temperature;
}



void loop()
{
  int buf[318];
  int x, x2;
  int y, y2;
  int r;


 

  myGLCD.setColor(255, 0, 0);
  myGLCD.fillRect(0, 0, 319, 13);
  myGLCD.setColor(64, 64, 64);
  myGLCD.setBackColor(255, 0, 0);

  myGLCD.print(displayTime(), CENTER, 1) ;
  delay(1000);
}
"We are legion"

StarRider

  • Visitante
Re: Iniciante - juntar codigos
« Responder #27 em: 16 de Janeiro de 2015, 23:53 »
Pensei que já tinha postado ... aqui vai:

Código: [Seleccione]
#include "Wire.h"
#include <UTFT.h>
#include <OneWire.h>

/*----- externals */
extern uint8_t SmallFont[];

#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal

/*----- locals */
int sensors_pin = 10; //the pin where the bus its connected to arduino
uint8_t sensor_addres[8]; //here will store the sensors addresses for later use
OneWire  sensor_bus(sensors_pin);
float get_temperature(uint8_t *address);
char *daynames[] = {"Domingo", "Segunda", "Terça", "Quarta", "Quinta", "Sexta", "Sabado"};
UTFT myGLCD(ITDB32S,38,39,40,41);
char timetempbuf[64];
byte sec, min, hour, dow, dom, mon, year;


/*---------------------------------------------------------*/
byte decToBcd(byte val)
{
return( (val/10*16) + (val%10) );
}

/*---------------------------------------------------------*/
byte bcdToDec(byte val)
{
return( (val/16*10) + (val%16) );
}

/*---------------------------------------------------------*/
void setup()
{
Wire.begin();
Serial.begin(9600);
setDS3231time(30,21,19,6,6,01,15);
// set the initial time here:
// DS3231 seconds, minutes, hours, day, date, month, year
// setDS3231time(30,40,18,5,26,11,14);
randomSeed(analogRead(0));

myGLCD.InitLCD();
myGLCD.setFont(SmallFont);

Serial.println("Starting to look for sensors...");
if(sensor_bus.search(sensor_addres)) {
Serial.print("Found sensor at address : ");
//Serial.println(sensor_addres);
} else
Serial.println("Didn't find any sensors");
}

/*---------------------------------------------------------*/
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year)
{
// sets time and date data to DS3231
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set next input to start at the seconds register
Wire.write(decToBcd(second)); // set seconds
Wire.write(decToBcd(minute)); // set minutes
Wire.write(decToBcd(hour)); // set hours
Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
Wire.write(decToBcd(month)); // set month
Wire.write(decToBcd(year)); // set year (0 to 99)
Wire.endTransmission();
}

/*---------------------------------------------------------*/
void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year)
{
Wire.beginTransmission(DS3231_I2C_ADDRESS);
Wire.write(0); // set DS3231 register pointer to 00h
Wire.endTransmission();
Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
// request seven bytes of data from DS3231 starting from register 00h
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}


/*---------------------------------------------------------*/
float get_temperature(uint8_t *address)
{
byte data[12];
int x;
sensor_bus.reset();
sensor_bus.select(address);
sensor_bus.write(0x44,1);

sensor_bus.reset();
sensor_bus.select(address);
sensor_bus.write(0xBE,1);
for(x=0;x<9;x++){
data[x] = sensor_bus.read();
}

unsigned short tr = data[0];
tr = tr + data[1]*256;

if(tr > 0xF000){
tr = 0 - tr;
}

float temperature = (float)tr*(float)0.0625 ;
return temperature;
}


/*---------------------------------------------------------*/
void loop()
{
readDS3231time(&sec, &min, &hour, &dow, &dom, &mon, &year);
   sprintf(timetempbuf,"%02d:%02d:%02d  %02d/%02d/%04d %s %4.1fC",hour, min, sec, dom, mon, year, daynames[dow],get_temperature(sensor_addres));

Serial.println(timetempbuf);
myGLCD.setColor(255, 0, 0);
myGLCD.fillRect(0, 0, 319, 13);
myGLCD.setColor(64, 64, 64);
myGLCD.setBackColor(255, 0, 0);
myGLCD.print(timetempbuf, CENTER, 1) ;

delay(1000); // every second

}

/*---------------------------------------------------------*/
/* eof */
« Última modificação: 17 de Janeiro de 2015, 00:11 por StarRider »

Offline MRData

  • Mini Robot
  • *
  • Mensagens: 81
Re: Iniciante - juntar codigos
« Responder #28 em: 16 de Janeiro de 2015, 23:56 »
Faltava uma coisa
Testa com este pff :

Código: [Seleccione]
#include "Wire.h"
#include <UTFT.h>
#include <OneWire.h>


extern uint8_t SmallFont[];

#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
const int sensors = 1; //number of temperature sensors on the bus
int sensors_pin = 10; //the pin where the bus its connected to arduino
uint8_t sensors_address[sensors][8]; //here will store the sensors addresses for later use

OneWire  sensor_bus(sensors_pin);

float get_temperature (uint8_t *address);


UTFT myGLCD(ITDB32S,38,39,40,41);

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) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  setDS3231time(30,21,19,6,6,01,15);
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(30,40,18,5,26,11,14);
  randomSeed(analogRead(0));


  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);

  int x,y,c=0;
  Serial.println("Starting to look for sensors...");
  for(x=0;x<sensors;x++){
    if(sensor_bus.search(sensors_address[x]))
      c++;
  }
  if(c > 0) {
    Serial.println("Found this sensors : ");
    for(x=0;x<sensors;x++) {
      Serial.print("\tSensor ");
      Serial.print(x+1);
      Serial.print(" at address : ");
      for(y=0;y<8;y++){
        Serial.print(sensors_address[x][y],HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
  }
  else
    Serial.println("Didn't find any sensors");
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

String displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  String buf = "";
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  buf += hour;
  // convert the byte variable to a decimal number when displayed
  Serial.print(" :");
  buf += ":";
  if (minute<10)
  {
    Serial.print("0");
    buf += "0";
  }
  Serial.print(minute, DEC);
  buf += minute;
  Serial.print(":");
  buf += ":";
  if (second<10)
  {
    Serial.print("0");
    buf += "0";
    buf += "0";
  }
  Serial.print(second, DEC);
  buf += second;
  Serial.print(" ");
  buf += " ";
  Serial.print(dayOfMonth, DEC);
  buf += dayOfMonth;
  Serial.print("/");
  buf += "/";
  Serial.print(month, DEC);
  buf += month;
  Serial.print("/");
  buf += "/";
  Serial.print(year, DEC);
  buf += year;
  Serial.print(" ");
  buf += " ";
  switch(dayOfWeek){
  case 1:
    Serial.println("Domingo");
    buf += "Domingo";
    break;
  case 2:
    Serial.println("Segunda");
    buf += "Segunda";
    break;
  case 3:
    Serial.println("Terça");
    buf += "Terça";
    break;
  case 4:
    Serial.println("Quarta");
    buf += "Quarta";
    break;
  case 5:
    Serial.println("Quinta");
    buf += "Quinta";
    break;
  case 6:
    Serial.println("Sexta");
    buf += "Sexta";
    break;
  case 7:
    Serial.println("Sabado");
    buf += "Sabado";
    break;
  } 

  for(int x=0;x<sensors;x++) {
    Serial.println(" ");
    buf += " ";

   
    Serial.print(" ");
    buf += " ";
    Serial.print(get_temperature(sensors_address[x]));
    buf += get_temperature(sensors_address[x]);

    Serial.println(" C");
    buf += " C";
  }

  return buf;
}


float get_temperature(uint8_t *address) {
  byte data[12];
  int x;
  sensor_bus.reset();
  sensor_bus.select(address);
  sensor_bus.write(0x44,1);

  sensor_bus.reset();
  sensor_bus.select(address);
  sensor_bus.write(0xBE,1);
  for(x=0;x<9;x++){
    data[x] = sensor_bus.read();
  }


  unsigned short tr = data[0];
  tr = tr + data[1]*256;

  if(tr > 0xF000){
    tr = 0 - tr;
  }


  float temperature = (float)tr*(float)0.0625 ;

  return temperature;
}



void loop()
{
  int buf[318];
  int x, x2;
  int y, y2;
  int r;


  myGLCD.clrScr();

  myGLCD.setColor(255, 0, 0);
  myGLCD.fillRect(0, 0, 319, 13);
  myGLCD.setColor(64, 64, 64);
  myGLCD.setBackColor(255, 0, 0);

  myGLCD.print(displayTime(), CENTER, 1) ;
  delay(1000);
}
"We are legion"

Offline MRData

  • Mini Robot
  • *
  • Mensagens: 81
Re: Iniciante - juntar codigos
« Responder #29 em: 17 de Janeiro de 2015, 00:01 »
Testa agora pff.

Código: [Seleccione]
#include "Wire.h"
#include <UTFT.h>
#include <OneWire.h>


extern uint8_t SmallFont[];

#define DS3231_I2C_ADDRESS 0x68
// Convert normal decimal numbers to binary coded decimal
const int sensors = 1; //number of temperature sensors on the bus
int sensors_pin = 10; //the pin where the bus its connected to arduino
uint8_t sensors_address[sensors][8]; //here will store the sensors addresses for later use

OneWire  sensor_bus(sensors_pin);

float get_temperature (uint8_t *address);


UTFT myGLCD(ITDB32S,38,39,40,41);

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) );
}
void setup()
{
  Wire.begin();
  Serial.begin(9600);
  setDS3231time(30,21,19,6,6,01,15);
  // set the initial time here:
  // DS3231 seconds, minutes, hours, day, date, month, year
  // setDS3231time(30,40,18,5,26,11,14);
  randomSeed(analogRead(0));


  myGLCD.InitLCD();
  myGLCD.setFont(SmallFont);

  myGLCD.setColor(255, 0, 0);
  myGLCD.fillRect(0, 0, 319, 13);
  myGLCD.setColor(64, 64, 64);
  myGLCD.setBackColor(255, 0, 0);

  int x,y,c=0;
  Serial.println("Starting to look for sensors...");
  for(x=0;x<sensors;x++){
    if(sensor_bus.search(sensors_address[x]))
      c++;
  }
  if(c > 0) {
    Serial.println("Found this sensors : ");
    for(x=0;x<sensors;x++) {
      Serial.print("\tSensor ");
      Serial.print(x+1);
      Serial.print(" at address : ");
      for(y=0;y<8;y++){
        Serial.print(sensors_address[x][y],HEX);
        Serial.print(" ");
      }
      Serial.println();
    }
  }
  else
    Serial.println("Didn't find any sensors");
}
void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte
dayOfMonth, byte month, byte year)
{
  // sets time and date data to DS3231
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set next input to start at the seconds register
  Wire.write(decToBcd(second)); // set seconds
  Wire.write(decToBcd(minute)); // set minutes
  Wire.write(decToBcd(hour)); // set hours
  Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday)
  Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31)
  Wire.write(decToBcd(month)); // set month
  Wire.write(decToBcd(year)); // set year (0 to 99)
  Wire.endTransmission();
}
void readDS3231time(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
  Wire.beginTransmission(DS3231_I2C_ADDRESS);
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.endTransmission();
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec(Wire.read() & 0x7f);
  *minute = bcdToDec(Wire.read());
  *hour = bcdToDec(Wire.read() & 0x3f);
  *dayOfWeek = bcdToDec(Wire.read());
  *dayOfMonth = bcdToDec(Wire.read());
  *month = bcdToDec(Wire.read());
  *year = bcdToDec(Wire.read());
}

String displayTime()
{
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  String buf = "";
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month,
  &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  buf += hour;
  // convert the byte variable to a decimal number when displayed
  Serial.print(" :");
  buf += ":";
  if (minute<10)
  {
    Serial.print("0");
    buf += "0";
  }
  Serial.print(minute, DEC);
  buf += minute;
  Serial.print(":");
  buf += ":";
  if (second<10)
  {
    Serial.print("0");
    buf += "0";
    buf += "0";
  }
  Serial.print(second, DEC);
  buf += second;
  Serial.print(" ");
  buf += " ";
  Serial.print(dayOfMonth, DEC);
  buf += dayOfMonth;
  Serial.print("/");
  buf += "/";
  Serial.print(month, DEC);
  buf += month;
  Serial.print("/");
  buf += "/";
  Serial.print(year, DEC);
  buf += year;
  Serial.print(" ");
  buf += " ";
  switch(dayOfWeek){
  case 1:
    Serial.println("Domingo");
    buf += "Domingo";
    break;
  case 2:
    Serial.println("Segunda");
    buf += "Segunda";
    break;
  case 3:
    Serial.println("Terça");
    buf += "Terça";
    break;
  case 4:
    Serial.println("Quarta");
    buf += "Quarta";
    break;
  case 5:
    Serial.println("Quinta");
    buf += "Quinta";
    break;
  case 6:
    Serial.println("Sexta");
    buf += "Sexta";
    break;
  case 7:
    Serial.println("Sabado");
    buf += "Sabado";
    break;
  } 

  for(int x=0;x<sensors;x++) {
    Serial.println(" ");
    buf += " ";

   
    Serial.print(" ");
    buf += " ";
    Serial.print(get_temperature(sensors_address[x]));
    buf += get_temperature(sensors_address[x]);

    Serial.println(" C");
    buf += " C";
  }

  return buf;
}


float get_temperature(uint8_t *address) {
  byte data[12];
  int x;
  sensor_bus.reset();
  sensor_bus.select(address);
  sensor_bus.write(0x44,1);

  sensor_bus.reset();
  sensor_bus.select(address);
  sensor_bus.write(0xBE,1);
  for(x=0;x<9;x++){
    data[x] = sensor_bus.read();
  }


  unsigned short tr = data[0];
  tr = tr + data[1]*256;

  if(tr > 0xF000){
    tr = 0 - tr;
  }


  float temperature = (float)tr*(float)0.0625 ;

  return temperature;
}



void loop()
{
  int buf[318];
  int x, x2;
  int y, y2;
  int r;


  myGLCD.clrScr();



  myGLCD.print(displayTime(), CENTER, 1) ;
  delay(1000);
}
"We are legion"