Steveiboy:
I've started to play around again with this again, Not sure what's happened as the last time it was working but now I just get it displaying -1 and nan on the LCD and serial port, I've done nothing to the code since it was working last time other than reloaded it into the board's. I'm only using one slave at the moment and highlighted out slave two if/when I get it working I will see about adding 3 other slaves.
I'm using version 1.67 and replaced the twi.c file as I did the last time to get it working when I add the same issue with the previous version of the IDE
Master code
#include <Wire.h>
#include <I2C_anyThing.h> // Template from Nick Gammon
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
float fnum; // volatile Not Need because variable will be Not be accessed during an interrupt ISR
long foo; // same thing.
float fnum1;
float 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
lcd.begin (20,4); // for 16 x 2 LCD module
lcd.setBacklightPin(3, POSITIVE);
lcd.setBacklight(HIGH);
lcd.clear();
//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("**************");
lcd.setCursor(0, 0);
lcd.print(foo);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(fnum);
lcd.print(" ");
}
void debug_out1( char msg1[]) {
Serial.println(msg1);
Serial.print("foo=");
Serial.println(foo1);
Serial.print("fnum=");
Serial.println(fnum1);
Serial.println("**************");
lcd.setCursor(0, 2);
lcd.print(foo1);
lcd.print(" ");
lcd.setCursor(0, 3);
lcd.print(fnum1);
lcd.print(" ");
}
void loop() {
slave_one();
// slave_two();
delay(200);
}
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(fnum1);
I2C_readAnything(foo1);
debug_out1("SLAVE TWO");
}
else debug_out1("Tried to Read from Slave 2, Failed.");
}
Slave code
#include <Wire.h>
#include <I2C_anyThing.h> // Template from Nick Gammon
const int numReadings = 10;
int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average
float vout = 0.0;
int inputPin = A0;
volatile float fnum; // volatile because variable will be accessed during an interrupt ISR
volatile long foo; // same thing.
volatile uint8_t newData; // flag to detect I2C activity
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
#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.
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
}
void loop() {
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(inputPin);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings)
// ...wrap around to the beginning:
readIndex = 0;
// calculate the average:
vout = total / numReadings;
foo = (vout * 5.0) / 1024.0; // see text
fnum = 12;
delay(200);
}
Steveiboy,
I haven't looked at any of the newer Arduino's past 1.65, you should install the newest version and compare my version of twi.c with the 'new' library, all of my edits are commented, you should be able to see the differences.
Also,I changed Wire.h, Wire.cpp, and twi.c. if you just updated twi.c I can see where this problem would rear it's head. In the Wire.write() is were the buffer count is overwritten if the write() is during a onRequest() event.
Instead of overwriting twi.c inside of the Arduino environment, just use the Arduino Environment to install whole modified Wire library.
Sketch\include Library\add .Zip Library...
That way, you can easily remove it by just deleting the whole subdirectory.
The Arduino environment will create a directory {sketchesFolder}\libraries\Wire
My {sketchesFolder} happens to be "C:\Users\User\Documents\Arduino".
When you have My wire library install, any sketch that uses Wire will display this warning when it is compiled:
Multiple libraries were found for "Wire.h"
Used: C:\Users\user\Documents\Arduino\libraries\Wire
Not used: C:\Program Files\Arduino\hardware\arduino\avr\libraries\Wire
Your locations may be different, mine are under 32bit Win7.
Chuck.