I am using Python smbus write_word_data method to send int values from a Raspberry Pi to an Arduino using I²C.
This seems to be the correct method.
When I send values from 0-255, I see the following in my arduinoArduino serial monitor
0,0,0
0,1,0
0,2,0
…
0,255,0
0,0,0
0,1,0
0,2,0
…
0,255,0
Sending 256 I see
:
0,0,1
0,0,1
forFor 257 I see
0,1,1:
0,1,1
etcEtc, etc...
My question is, how would I modify the following arduinoArduino code to properly 'reconstitute' the int values, and then send them back to the RPi?
#include <Wire.h> #define SLAVE_ADDRESS 0x04 int number = 0; int state = 0; bool newData = false; void setup() { pinMode(13, OUTPUT); Serial.begin(9600); // start serial for output // initialize i2c as slave Wire.begin(SLAVE_ADDRESS); // define callbacks for i2c communication Wire.onReceive(receiveData); Wire.onRequest(sendData); Serial.println("Ready!"); } void loop() { Serial.println(receivedValue); newData=false; } } // callback for received data void receiveData(int byteCount){ { while(Wire.available()) { newData = true; Wire.read(); if(Wire.available() == 2) { receivedValue = Wire.read() | Wire.read() << 8; } } } } // callback for sending data void sendData(){ { Wire.write(number); }