Skip to main content
This is as much an i2c question as a serial one
Source Link
Bachalo
  • 251
  • 4
  • 12

receiving constantly Proxying streaming seriali2c data(int values) to serial

The following code works correctly in reading single int values. It also works correctly for 3-4 seconds if I 'stream' the values in constantly (Mouse move coordinates) before I get an IO error and the i2C bus connection is dropped. I assume this is some sort of buffer overrun...or? Can anyone get me on the right track here? What changes do I need to make so that I can constantly stream in serial data?

    #include <Wire.h>

#define SLAVE_ADDRESS 0x04
int zeroByte;
int state = 0;
int receivedValue = 0;

int numBytes = 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() {
    delay(100);
    if(newData){
      
      Serial.println(receivedValue);
      newData=false;
    }
}

// callback for received data
void receiveData(int byteCount){

    while(Wire.available()) {
        newData = true;
        zeroByte = Wire.read();
        
         if(Wire.available() == 2)
  {
    receivedValue = Wire.read() | Wire.read() << 8;
    Serial.println(receivedValue);
    
  }
       
     }  
        
      }                 
}

// callback for sending data
void sendData(){
  
    Wire.write(receivedValue);
}

receiving constantly streaming serial data(int values)

The following code works correctly in reading single int values. It also works correctly for 3-4 seconds if I 'stream' the values in constantly (Mouse move coordinates) before I get an IO error and the i2C bus connection is dropped. I assume this is some sort of buffer overrun...or? Can anyone get me on the right track here? What changes do I need to make so that I can constantly stream in serial data?

#include <Wire.h>

#define SLAVE_ADDRESS 0x04
int state = 0;
int receivedValue = 0;

int numBytes = 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()) {
        zeroByte = Wire.read();
        
         if(Wire.available() == 2)
  {
    receivedValue = Wire.read() | Wire.read() << 8;
    Serial.println(receivedValue);
    
  }
       
       
        
      }
}

// callback for sending data
void sendData(){
  
    Wire.write(receivedValue);
}

Proxying streaming i2c data(int values) to serial

The following code works correctly in reading single int values. It also works correctly for 3-4 seconds if I 'stream' the values in constantly (Mouse move coordinates) before I get an IO error and the i2C bus connection is dropped. I assume this is some sort of buffer overrun...or? Can anyone get me on the right track here? What changes do I need to make so that I can constantly stream in serial data?

    #include <Wire.h>

#define SLAVE_ADDRESS 0x04
int zeroByte;
int state = 0;
int receivedValue = 0;

int numBytes = 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() {
    delay(100);
    if(newData){
      
      Serial.println(receivedValue);
      newData=false;
    }
}

// callback for received data
void receiveData(int byteCount){

    while(Wire.available()) {
        newData = true;
        zeroByte = Wire.read();
        
         if(Wire.available() == 2)
  {
    receivedValue = Wire.read() | Wire.read() << 8;
    
    
  }
       
     }                               
}

// callback for sending data
void sendData(){
  
    Wire.write(receivedValue);
}
Source Link
Bachalo
  • 251
  • 4
  • 12

receiving constantly streaming serial data(int values)

The following code works correctly in reading single int values. It also works correctly for 3-4 seconds if I 'stream' the values in constantly (Mouse move coordinates) before I get an IO error and the i2C bus connection is dropped. I assume this is some sort of buffer overrun...or? Can anyone get me on the right track here? What changes do I need to make so that I can constantly stream in serial data?

#include <Wire.h>

#define SLAVE_ADDRESS 0x04
int state = 0;
int receivedValue = 0;

int numBytes = 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()) {
        zeroByte = Wire.read();
        
         if(Wire.available() == 2)
  {
    receivedValue = Wire.read() | Wire.read() << 8;
    Serial.println(receivedValue);
    
  }
       
       
       
     }
}

// callback for sending data
void sendData(){
  
    Wire.write(receivedValue);
}