Skip to main content
Bumped by Community user
Bumped by Community user
deleted 302 characters in body; edited title
Source Link
dda
  • 1.6k
  • 1
  • 12
  • 18

Receiving 2bytetwo-byte int values over serial

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);  } 

Receiving 2byte int values over serial

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 arduino serial monitor

0,0,0
0,1,0
0,2,0

0,255,0

Sending 256 I see
 

0,0,1

for 257 I see
0,1,1

etc etc

My question is how would I modify the following arduino 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);  } 

Receiving two-byte int values over serial

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 Arduino serial monitor

0,0,0
0,1,0
0,2,0

0,255,0

Sending 256 I see:

0,0,1

For 257 I see:

0,1,1

Etc, etc...

My question is, how would I modify the following Arduino 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); // 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); } 
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
tag, clarified heading and code highlighting
Source Link

I am using Python smbus write_word_datawrite_word_data method to send int values from a Raspberry Pi to an arduinoArduino using I²C.

0,0,0
0,1,0
0,2,0
etc
0,255,0

My question is how would I modify the following arduino code to properly 'reconsitute''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); } 
 #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); } 

I am using Python smbus write_word_data method to send int values from a Raspberry Pi to an arduino.

0,0,0
0,1,0
0,2,0
etc
0,255,0

My question is how would I modify the following arduino code to properly 'reconsitute' 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); } 

I am using Python smbus write_word_data method to send int values from a Raspberry Pi to an Arduino using I²C.

0,0,0
0,1,0
0,2,0

0,255,0

My question is how would I modify the following arduino 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); } 
Bumped by Community user
Bumped by Community user
deleted 271 characters in body
Source Link
Bachalo
  • 251
  • 4
  • 12
 #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.onRequestonReceive(sendDatareceiveData);   Serial.println("Ready!"); } void loop() {  delayWire.onRequest(100sendData); }  // callback for received data void receiveData(int byteCount){  while(Wire.available()) {  number = WireSerial.readprintln("Ready!");   }    void Serial.printloop("data received: ");  {  Serial.println(numberreceivedValue);     newData=false;  if (number == 1){} }     // callback for received data  ifvoid receiveData(state ==int 0byteCount){      digitalWritewhile(13, HIGHWire.available(); // set the LED) on{   statenewData = 1;true;      }Wire.read();   if(Wire.available() == 2)  else{   receivedValue = digitalWriteWire.read(13, LOW); // set| theWire.read() LED<< off8;   state = 0;}   }     }     } } // callback for sending data  void sendData(){     Wire.write(number);  } 
#include <Wire.h> #define SLAVE_ADDRESS 0x04 int number = 0; int state = 0; 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() {  delay(100); }  // callback for received data void receiveData(int byteCount){  while(Wire.available()) {  number = Wire.read();   Serial.print("data received: ");  Serial.println(number);     if (number == 1){ if (state == 0){   digitalWrite(13, HIGH); // set the LED on   state = 1;   }   else{   digitalWrite(13, LOW); // set the LED off   state = 0;   }  }     } } // callback for sending data void sendData(){ Wire.write(number); } 
 #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);  } 
Source Link
Bachalo
  • 251
  • 4
  • 12
Loading