I'm making progress but the foo value is not increasing as I though it would here is the codes, might be a bit messy and a better way of doing it but it's working(I will tidy up code in a bit) I'm doing it as building blocks as I want to measure a voltage from 4 locations and send it to one unit but first thought make sure it send float values, My ADC code works so I think it's just the case of adding it bit by bit
Master
#include <Wire.h>
#include <I2C_anyThing.h> // Template from Nick Gammon
long fnum; // volatile Not Need because variable will be Not be accessed during an interrupt ISR
float foo; // same thing.
long fnum1;
long foo1;
#define DATALEN (sizeof fnum) + (sizeof foo)
#define SLAVE_ADDRESS_1 0x58 // slave 1change this for each slave
#define SLAVE_ADDRESS_2 0x59 // slave 2
void setup() {
Wire.begin(); // not a slave
Serial.begin(9600); // if USB is connected to this slave and Serial monitor is on
//we will send debug messages.
}
void debug_out( char msg[]) {
Serial.println(msg);
Serial.print("foo=");
Serial.println(foo);
Serial.print("fnum=");
Serial.println(fnum);
Serial.println("**************");
}
void loop() {
slave_one();
slave_two();
delay(1000);
}
float slave_one() {
Wire.requestFrom(SLAVE_ADDRESS_1, DATALEN);
if (Wire.available() == DATALEN) {
I2C_readAnything(fnum);
I2C_readAnything(foo);
debug_out("SLAVE ONE");
}
else debug_out("Tried to Read from Slave 1, Failed.");
}
float slave_two() {
Wire.requestFrom(SLAVE_ADDRESS_2, DATALEN);
if (Wire.available() == DATALEN) {
I2C_readAnything(fnum);
I2C_readAnything(foo);
debug_out("SLAVE TWO");
}
else debug_out("Tried to Read from Slave 1, Failed.");
}
Slave_one (two is the same apart from address and values)
#include <Wire.h>
#include <I2C_anyThing.h> // Template from Nick Gammon
volatile long fnum; // volatile because variable will be accessed during an interrupt ISR
volatile float foo; // same thing.
volatile uint8_t newData; // flag to detect I2C activity
#define DATALEN (sizeof fnum) + (sizeof foo)
#define SLAVE_ADDRESS 0x58 // change this for each slave
void onRequestEvent(void) {
/* Wire.write(1);
Wire.write(2);
Wire.write(3);
Wire.write(4);
Wire.write(5);
Wire.write(6);
Wire.write(7);
Wire.write(8);
*/
I2C_writeAnything(fnum);
I2C_writeAnything(foo);
// newData = newData | 1; // set bit 0 to mark I2C Sent data
}
void onReceiveEvent(int numbytes) {
if (numbytes == DATALEN) { // Masters beginTransmission(SLAVE_ADDRESS);Write();endTransmission();
// sent the correct number of bytes
I2C_readAnything(fnum);
I2C_readAnything(foo);
// newData = newData | 2; // set bit 1 to mark I2C Received Data.
}
}
void setup() {
//newData = 0;
Wire.begin(SLAVE_ADDRESS);
Wire.onReceive(onReceiveEvent);
Wire.onRequest(onRequestEvent);
Serial.begin(9600); // if USB is connected to this slave and Serial monitor is on
//we will send debug messages.
}
void loop() {
foo = 12.36 + 0.01;
fnum = 24;
}
Why is not the foo value increasing ?