Hey guys. Pleasure interacting with all of you again. I would like to ask how to convert value of byte(double) to variable(double) back as the master is now working fine thanks to mr.johnwasser addition of conversion from variable to byte. My purpose is I want to write lat & lng variables from master to slave(receiver) so the slave can output to serial monitor. I have done my research but came up confused. Appreciate it someone can give a go at it. Thanks.
Master code(writer)
// Include Arduino Wire library for I2C
#include <Wire.h>
#include <SD.h>
#include <SPI.h>
#include "TinyGPS++.h"
#include "SoftwareSerial.h"
int pinCS = 53;// pin 10 on uno
File myFile;
TinyGPSPlus gps;//This is the GPS object that will pretty much do all the grunt work with the NMEA data
SoftwareSerial serial_connection(62, 63);
#define SLAVE_ADDR 9 // Define Slave I2C Address
void setup() {
// Initialize I2C communications as Master
Wire.begin();
Serial.begin(9600);
serial_connection.begin(9600);//This opens up communications to the GPS
pinMode(pinCS, OUTPUT);
}
void loop()
{
while(serial_connection.available())//While there are characters to come from the GPS
{
gps.encode(serial_connection.read());//This feeds the serial NMEA data into the library one char at a time
}
if(gps.location.isUpdated())//This will pretty much be fired all the time anyway but will at least reduce it to only after a package of NMEA data comes in
{
myFile = SD.open("xtracker.txt", FILE_WRITE); // Create/Open file
myFile.print("Latitude:");
myFile.print(gps.location.lat(), 6);// Latitude in degrees (double)
myFile.print(",");
myFile.print("Longitude:");
myFile.print(gps.location.lng(), 6);// Longitude in degrees (double)
myFile.println();
myFile.close();
}
double lat = gps.location.lat();
double lng = gps.location.lng();
// Send the two 'double' values to the Slave
Wire.beginTransmission(SLAVE_ADDR);
Wire.write((byte *)&lat, sizeof lat);
Wire.write((byte *)&lng, sizeof lng);
Wire.endTransmission();
delay(500);
}// end loop()
Slave code(receiver)
// Include Arduino Wire library for I2C
#include <Wire.h>
// Define Slave I2C Address
#define SLAVE_ADDR 9
// Variable for received data
int double lat;
int double lng;
void setup() {
// Initialize I2C communications as Slave
Wire.begin(SLAVE_ADDR);
// Function to run when data received from master
Wire.onReceive(receiveEvent);
// Setup Serial Monitor
Serial.begin(9600);
Serial.println("I2C Slave Demonstration");
}
void receiveEvent()
{
//Not sure how the conversion goes to achieve wire.read//
}
void loop()
{
delay(50);
}