I'm having trouble using I2C_Anything with Wire.requestFrom, and any help would be greatly appreciated.
I'd like the master to request data from the slave, but all my floats arrive as nan . I've tried sending individual bytes and it works. I'm confident in my wiring because I can get I2C_Anything to work as written in this post. There's something about how I'm using requestFrom which is breaking something!
Here are the files:
Master, an Uno which uses Wire.requestFrom to ask for the data
// Eric Rombokas
// Jan 3 2013
// Arduino is i2c master, arduIMU is i2c slave . arduino dumps requested i2c data to serial
// Using gammon I2C_readAnything.h for translating I2C bytes to other data types
#include <math.h>
#include <Wire.h>
#include "I2C_writeAnything.h"
const int SLAVE_ADDRESS = 10;
volatile float volatileYaw;
void setup() {
Serial.begin(115200);
Wire.begin(); // begin with no address specified joins the bus as a master
}
void loop() {
delay(1000);
if (Wire.requestFrom(SLAVE_ADDRESS, 4, true) == 4) {
I2C_readAnything(volatileYaw);
Serial.println(volatileYaw);
}
}
And slave, which uses I2C_writeAnything in its requestEvent()
// Eric Rombokas
// Jan 3 2013
// ArduIMU runs as an i2c slave, querying freeIMU for yaw, pitch, roll when asked to by the i2c master
// Adapted from Fabio Varesano's FreeIMU_Yaw_pitch_roll RIP
// i2c float/byte stuff adapted from "WanaGo" on arduino forum
#include <ADXL345.h>
#include <bma180.h>
#include <HMC58X3.h>
#include <ITG3200.h>
#include <MS561101BA.h>
#include <I2Cdev.h>
#include <MPU60X0.h>
#include <EEPROM.h>
//#define DEBUG
#include "DebugUtils.h"
#include "CommunicationUtils.h"
#include "FreeIMU.h"
#include "I2C_writeAnything.h"
#include <Wire.h>
#include <SPI.h>
#include <math.h>
volatile float volatileYaw = 42.42;
const int I2CSlaveAddress = 10;
void setup() {
Wire.begin(I2CSlaveAddress);
Wire.onRequest( requestEvent ) ; // declare function handle to be called when i2c master requests
}
void loop() {
}
// called by onRequest interrupt
void requestEvent() {
I2C_writeAnything(volatileYaw);
}
Any ideas?
Thanks,
Eric R
(edited to remove superfluous stuff)