Master, Slave, TinyGPS issues, I2C with Lego Mindstorm (issues)

hi

I’m been working with an I2C communication between the Lego Mindstorms NXT and Arduino Uno SMD version, so far I got the i2C protocol running and working with a very simple loop on the NXC code, that queries for some information from the arduino to the NXT via registry addresses

I want now to be able to use a serial connection, to do something else with the arduino- NXT so the NXT will query arduino via I2c (pin analogue 5,6) and then the arduino will receive some data from the serial connection (digital pin 3,4) at the same time.

I did notice that when the i2c is the only connection all works NXT is happy and arduino is happier
but is a start the serial connection all becomes a mess, arduino fails seconds after the serial communication has been setup, the serial connection (digital) need to be setup at 4800

not sure what is wrong and how to fix it, I would appreciate any help

Thanks

My arduino code

#include <NewSoftSerial.h>
#include <Wire.h>
#include <string.h>
#include <ctype.h>
#include <SoftwareSerial.h>
#include <WString.h>
#include <math.h>
#include <stdlib.h>
// Enable/disable debug modes
boolean debug_gps = true;
boolean debug_nxt = true;

#define ADDRESS 0x43

char SensorVersion[15] = “V0.1 /”;
char SensorName[15] = “Test /”;
char SensorType[15] = “Digital /”;
char message_2_NXT [15] =" “;
char message_from_NXT [15] =” ";
char x;

int heading;
boolean newdata = false;

int ledPin = 5; //LED test pin
int ledPin2 = 6; //LED test pin

NewSoftSerial nss(3, 4);//RX, TX

char OK1[15] = “”;
int OK=900;

void setup() {
pinMode(ledPin,OUTPUT);
pinMode(ledPin2,OUTPUT);
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
blinkBothLED ();
nss.begin(4800);
Serial.begin(9600);
Wire.begin(ADDRESS);
Wire.onReceive(receiveEvent);
Wire.onRequest(requestEvent);

}

void loop()
{

}

//---------------------------------I2C Events-------------------------------//

void receiveEvent(int howMany){

digitalWrite(ledPin2, LOW);

if (Wire.available() >0 ) //&& index <15)
{
x= Wire.receive(); // receive byte as an integer
digitalWrite(ledPin2, HIGH);

}

}

void requestEvent(){

if (x == 0x00){
Wire.send((uint8_t*)SensorVersion,15);
digitalWrite(ledPin2, HIGH);
}
else if (x == 0x08){
Wire.send((uint8_t*)SensorName,15);
digitalWrite(ledPin2, HIGH);
}
else if (x == 0x10){
Wire.send((uint8_t*)SensorType,15);
digitalWrite(ledPin2, HIGH);
}
else if (x == 0x42) {
OK++;
itoa (OK,OK1,10);
Wire.send((uint8_t*)OK1, 15);
digitalWrite(ledPin2, HIGH);
}
digitalWrite(ledPin2, LOW);
}

void blinkBothLED()
{
//function to blink both LEDs
digitalWrite(ledPin, HIGH);
digitalWrite(ledPin2, HIGH);
delay(500);
digitalWrite(ledPin, LOW);
digitalWrite(ledPin2, LOW);
delay(500);
}

my NXC code for the lego NXT


// ARDUINO NXT interface
#define ARDUINO_ADDRESS 0x43
#define ADDRESS_SEND (ARDUINO_ADDRESS << 1)
#define ADDRESS_RECV (ADDRESS_SEND + 1)
#define ARDUINO_PORT IN_1

string i2cReadString(byte port, byte adr, byte reg, byte cnt)
{
string temp;
byte outbuf;
byte cmdbuf;
ArrayBuild(cmdbuf,adr, reg);
byte nByteReady = 0;
while (I2CStatus(port, nByteReady) == STAT_COMM_PENDING)
{
}
Wait (100);
if(I2CBytes(port, cmdbuf, cnt, outbuf))
{
temp = ByteArrayToStr(outbuf);
//TextOut(0, LCD_LINE6, temp2, false);
}
return temp;
}

void ShowSensorInfo(byte port, byte address)
{
ClearScreen();
TextOut(0, LCD_LINE2, i2cReadString(port, address, 0x00, 15), false);
TextOut(0, LCD_LINE3, i2cReadString(port, address, 0x08, 15), false);
TextOut(0, LCD_LINE4, i2cReadString(port, address, 0x10, 15), false);
}

task main() {
SetSensorLowspeed(ARDUINO_PORT);
ShowSensorInfo(ARDUINO_PORT,ADDRESS_SEND);
while(true) {
Wait (100);
TextOut(0, LCD_LINE5, i2cReadString(ARDUINO_PORT, ADDRESS_SEND, 0x42, 15), false);
}
}

Thanks

#include <string.h>
#include <ctype.h>
#include <SoftwareSerial.h>
#include <WString.h>
#include <math.h>
#include <stdlib.h>

Why are you including header files you don’t use?

NewSoftSerial nss(3, 4);//RX, TX

Why are you creating instances of classes that you don’t use?

char x;

Great name for a global variable. Makes it real easy to not mistakenly create a local variable of the same name. Not!

the serial connection (digital) need to be setup at 4800

What are you trying to serially communicate with?

Between the missing capital letters, the misspelled words, and the lack of punctuation, it is difficult to understand what you are trying to do/change.

Sorry, as English is my first language =(

I have added the other headers as I need them for the "sensor" as it will perform some mathematical computations and float to string convertions, however I'm just posting the relevant bits :smiley:

I'm using the NewSoftware serial, library and I have defined nss as the instance for the serial communication that is then used on the setup() function

nss.begin(4800)

my "sensor" will communicate with another serial device via pins 3 and 4 at 400 bps and I have notice that when I2C and serial cannot coexist not sure why?

I did test it with the nss.begin and the I2C communication; this two causes the arduino to fail;

without the serial communication it works(the I2C and NXT; however, I need the serial connection to "talk" with the other device.

I have found some informaiton regarding the use of NewSoftSeria, which would be useful

"NewSoftSerial requires nearly 100% CPU time while transmitting or receiving data. Low interrupt latency is also required for reliable reception, which limits all other libraries and code using interrupts. For these reasons, UART Serial should always be used if only a single serial port is needed. "

I will test tonight and post my results

char x;

Great name for a global variable. Makes it real easy to not mistakenly create a local variable of the same name. Not!

x is just a test; :slight_smile: