Hello,
I have a Uno making a random number, separate it in 4 nibbles, and adding 0xB to it (This is just to match the behavior of a sensor). The Leonardo sends 0x61 and 0x62 as a request for the data, gets the data and finally the random number from the Uno.
This works sometimes, in a pattern, wrong data 2 times, and correct data 5 times, in a loop. Can you help me with this error? I have the Uno's Tx connected to the Leonardos Rx, and viceversa.
Master code (Leonardo):
#include <LiquidCrystal.h> //Libreria para el manejor del LCD
#include <SdFat.h>//Libreria para el manejo de la Tarjeta SD
SdFat sd;
SdFile myFile;
#include "Wire.h"
#define DS1307_I2C_ADDRESS 0x68 // Direccion I2C del RTC DS1307
// Global Variables
const int chipSelect = 4; //ChipSelect de la memoria SD
const int SensorAl = 6; // Numero de Pin para la conexion AL del sensor.
const int SwitchInit = 5; // Numero de Pin para la conexion de switch de inicializacion.
const int SwitchStart = A0; // Numero de Pin para la conexion de switch de inicio. Se hace luego de completar el proceso de inicializacion.
int command = 0; // This is the command char, in ascii form, sent from the serial port
int altura=250; //Variable para guardar la altura del cilindro a medir. Se inicializa en 250 mm, ya que el sistema nocuanta con el sensor, para poder realizar los movimientos sin problemas
int radioextra=3; //VAriable para almacenar el radio extra debido al movimiento en el eje X del sensor para alcanzar la pared del cilindro
int second, minute, hour, dayOfWeek, dayOfMonth, month, year; //Varibles para guardar los datos del RTC
byte zero=0x00; //Reset de la direccion de I2C
char nombrearchivo[13]; //Guarda el nombre del archivo con el formato DDMMYYYY.txt
int medicion; //Variable para guardar el resultado enviado por el sensor
byte med[4]; //Arreglo de bytes para recibir los datos que manda el sensor
// Inicializacion de los pines requeridos por el LCD
LiquidCrystal lcd(13,12, 11, 9, 8, 7);
// Convierte numeros decimales normales a BCD
byte decToBcd(byte val)
{ return ( (val/10*16) + (val%10) );}
// Convierte BCD a numeros decimales normales
byte bcdToDec(byte val)
{ return ( (val/16*10) + (val%16) );}
// setDateDs1307() solo se necesita una vez, se mantiene en el codigo, por si se debe cambiar la bateria del RTC o modificar la fecha u hora.
void setDateDs1307()
{ //Asume el uso de numeros validos.Formato 24h.
/* Comandos para la parte RTC
// * String a enviar por el monitor serial:
* T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99) - T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year) -
* T - Sets the date of the RTC DS1307 Chip.
* Example to set the time for 25-Jan-2012 @ 19:57:11 for the 4 day of the week, use this command - T1157194250112
* --------------------------------------------------------------------------------------------------------------
* R - Read/display the time, day and date
*/
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.write(zero);
Wire.write(decToBcd(second) & 0x7f); // 0 to bit 7 starts the clock
Wire.write(decToBcd(minute));
Wire.write(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.write(decToBcd(dayOfWeek));
Wire.write(decToBcd(dayOfMonth));
Wire.write(decToBcd(month));
Wire.write(decToBcd(year));
Wire.endTransmission();
}
// Toma la fecha del RTC y la guarda en la variable nombrearchivo, con el formato DDMMYYYY.txt
void getDateDs1307(){
// Reset al puntero de registro
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
//Toma los datos del RTC en orden.
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());
//Guarda el nombre del archivo en el formato deseado.
snprintf(nombrearchivo,13,"%02d%02d20%02d.txt", dayOfMonth, month, year);
}
float MedicionSensor(){
byte test[4];
Serial.println("Inicio de medicion");
int medsens=0;
int medsens1=0;
float medicion=0.0;
byte comando0='a';
byte comando1='b';
Serial.print("Comando0:");
Serial.print(comando0,HEX);
Serial.print("Comando2:");
Serial.println(comando1,HEX);
//Serial.print(comando0);// manda los comandos de "Inquiring for result"
//Serial.print(comando1);
Serial1.write(comando0);// manda los comandos de "Inquiring for result"
Serial1.write(comando1);
while(!Serial1.available()){}//Lectura de los 4 bytes enviados por el sensor
med[0]=Serial1.read();
Serial.print("med[0]: ");
Serial.println(med[0],HEX);
test[0]=med[0]&0x0F;
Serial.print("test[0]: ");
Serial.println(test[0],HEX);
med[1]=Serial1.read();
Serial.print("med[1]: ");
Serial.println(med[1],HEX);
test[1]=med[1]&0x0F;
Serial.print("test[1]: ");
Serial.println(test[1],HEX);
med[2]=Serial1.read();
Serial.print("med[2]: ");
Serial.println(med[2],HEX);
test[2]=med[2]&0x0F;
Serial.print("test[2]: ");
Serial.println(test[2],HEX);
med[3]=Serial1.read();
Serial.print("med[3]: ");
Serial.println(med[3],HEX);
test[3]=med[3]&0x0F;
Serial.print("test[3]: ");
Serial.println(test[3],HEX);
//Se eliminan los primeros 4 bits de cada byte, y se concatenan de LSB a MSB.
medsens=((med[3] & 0x0F)<<12)|((med[2] & 0x0F)<<8)|((med[1] & 0x0F)<<4)|((med[0] & 0x0F));
medsens1=(test[3]<<12)|(test[2]<<8)|(test[1]<<4)|test[0];
Serial.print("Enviado por el sensor: ");
Serial.println(medsens,HEX);
medicion=(medsens*5.0)/16384;
Serial.print("Resultado de la medicion: ");
Serial.println(medicion);
Serial.print("Enviado por el sensor: ");
Serial.println(medsens1,HEX);
medicion=(medsens1*5.0)/16384;
Serial.print("Resultado de la medicion: ");
Serial.println(medicion);
return medicion;
}
//Inicializacion del sistema
void setup() {
Serial.begin(9600);//Inicializa el puerto serial de software para debugging
while(!Serial){}
Serial1.begin(9600);//Inicializa el puerto serial para la comunicacion con el sensor (UNO)
pinMode(SensorAl, INPUT);//Inicializa el pin SensorAL para saber si el sensor esta midiendo algo en el rango
Serial.print("Inicializando...");
delay(500);
// Initialize SdFat or print a detailed error message and halt
// Use half speed like the native library.
// change to SPI_FULL_SPEED for more performance.
while (!sd.begin(chipSelect, SPI_HALF_SPEED)){
sd.initErrorHalt();
Serial.print("SD Error!!! ");
delay(500);
Serial.print("Revisar SD ");
}
Serial.print("SD Lista!");
Wire.begin(); //Inicia la libreria Wire para la comunicacion I2C
getDateDs1307();
Serial.print("Archivo: ");
Serial.println(nombrearchivo);
}
void loop() {
float resultado=0;
if(digitalRead(SensorAl)==HIGH){
Serial.println("Preguntando resultado:");
resultado=MedicionSensor();
Serial.print("El resultado es: ");
Serial.println(resultado);
Serial.print("**********************************************************************************");
}
delay(2000);
}
I know test[] and med[] and that is the same thing, just did it to try and debug it.