Can't merge I2C and serial streams to USB

Status
Not open for further replies.

amundsen

Well-known member
Hello,

I have a Teensy 3.2 receiving data from an XBee on Serial1 (pins 0/1) at 19.2 kbps. Simultaneously the Teensy receives grabs data from an I2C IR camera at 100 kHz.

Both sources of data are retransmitted to my computer's USB port.

I am sure about the reception on the XBee : if I disable the camera I have all the data from Serial1 retransmitted correctly to the USB port. However as soon as I grab data from the camera, I can't see any data from Serial1 coming in my computer even if I sort the data according to different alphanumerical prefixes.

What's wrong?

This is the main sketch:
Code:
#include <i2c_t3.h>
// data for IR camera

int IRsensorAddress = 0xB0;
int slaveAddress;
int ledPin = 13;
boolean ledState = false;
byte data_buf[16];
int i;

int Ix[4];
int Iy[4];
int Is[4];
int s;
 
void setup()
{
    slaveAddress = IRsensorAddress >> 1;   // This results in 0x21 as the address to pass to TWI

    pinMode(ledPin, OUTPUT);      // Set the LED pin as output
    //Wire.begin();
    Wire.begin(I2C_MASTER, 0x00, I2C_PINS_18_19, I2C_PULLUP_INT, I2C_RATE_200);
    // IR sensor initialize
    Write_2bytes(0x30,0x01); delay(10);
    Write_2bytes(0x30,0x08); delay(10);
    Write_2bytes(0x06,0x90); delay(10);
    Write_2bytes(0x08,0xC0); delay(10);
    Write_2bytes(0x1A,0x40); delay(10);
    Write_2bytes(0x33,0x33); delay(10);
    delay(100);
    Serial.begin(); // USB
    Serial1.begin(19200); // Serial1 connected to XBee
}

void loop()
{
    serialToUSB();
    cameraToUSB();  
}

The serialToUSB function:
Code:
void serialToUSB() 
{
  int incomingByte;
  digitalWrite(13, LOW);  
  unsigned char bytecount = 0;
  while (Serial.available() && bytecount < 10) {
    incomingByte = Serial1.read();
    digitalWrite(13, HIGH);
    Serial.write(incomingByte); // DEC as option
    bytecount++;
  }
}

The cameraToUSB function:
Code:
 void cameraToUSB()
{
    ledState = !ledState;
    if (ledState) { digitalWrite(ledPin,HIGH); } else { digitalWrite(ledPin,LOW); }
 
    //IR sensor read
    Wire.beginTransmission(slaveAddress);
    Wire.write(0x36);
    Wire.endTransmission();
 
    Wire.requestFrom(slaveAddress, 16);        // Request the 2 byte heading (MSB comes first)
    for (i=0;i<16;i++) { data_buf[i]=0; }
    i=0;
    while(Wire.available() && i < 16) { 
        data_buf[i] = Wire.read();
        i++;
    }
 
    Ix[0] = data_buf[1];
    Iy[0] = data_buf[2];
    s   = data_buf[3];
    //Is[0]   = data_buf[3];   
    Ix[0] += (s & 0x30) <<4;
    Iy[0] += (s & 0xC0) <<2;
 
    Ix[1] = data_buf[4];
    Iy[1] = data_buf[5];
    s   = data_buf[6];
    //Is[1]   = data_buf[6];    
    Ix[1] += (s & 0x30) <<4;
    Iy[1] += (s & 0xC0) <<2;
 
    Ix[2] = data_buf[7];
    Iy[2] = data_buf[8];
    s   = data_buf[9];
    //Is[2]   = data_buf[9];
    Ix[2] += (s & 0x30) <<4;
    Iy[2] += (s & 0xC0) <<2;
 
    Ix[3] = data_buf[10];
    Iy[3] = data_buf[11];
    s   = data_buf[12];
    //Is[3]   = data_buf[12];
    Ix[3] += (s & 0x30) <<4;
    Iy[3] += (s & 0xC0) <<2;

    
    for(i=0; i<4; i++)
    {
      Serial.print("I ");
      Serial.print(i);
      Serial.print(" ");
      if (Ix[i] < 1000)
        Serial.print("");
      if (Ix[i] < 100)  
        Serial.print("");
      if (Ix[i] < 10)  
        Serial.print("");
      Serial.print( int(Ix[i]) );
      Serial.print(" ");
      if (Iy[i] < 1000)
        Serial.print("");
      if (Iy[i] < 100)  
        Serial.print("");
      if (Iy[i] < 10)  
        Serial.print("");
      Serial.print( int(Iy[i]) );
      Serial.print(" ");
      Serial.print(0);
      /*
      Serial.print(" ");
            if (Is[i] < 1000)
        Serial.print("");
      if (Is[i] < 100)  
        Serial.print("");
      if (Is[i] < 10)  
        Serial.print("");
      Serial.print( int(Is[i]) );
      */
      //if (i<3)
      Serial.println("\t");
    }
}

void Write_2bytes(byte d1, byte d2)
{
    Wire.beginTransmission(slaveAddress);
    Wire.write(d1); Wire.write(d2);
    Wire.endTransmission();
}
 
The Gamon page on serial processing shows some good tips.

p#1 Code piece #2 is reading Serial1 when Serial is available???:
Code:
while ([B]Serial[/B].available() && bytecount < 10) {
    incomingByte = [B]Serial1[/B].read();

I like the SerialEvent mechanism - it is processed every loop cycle - and on yield() or delay() - when Serial# is available.

In this example : Tutorial/SerialEvent - just make sure you use the Serial# correctly:

Code:
void serialEvent1() {
   while (Serial1.available()) {
     // get the new byte:
     char inChar = (char)Serial1.read();
 
p#1 Code piece #2 is reading Serial1 when Serial is available???:
Code:
while ([B]Serial[/B].available() && bytecount < 10) {
    incomingByte = [B]Serial1[/B].read();

Oops! Thanks defragster!

This should be (of course):
Code:
while (Serial1.available() && bytecount < 10) {
    incomingByte = [B]Serial1[/B].read();
 
Status
Not open for further replies.
Back
Top