Hi,
I try to simply transmit any bits/bytes that correspond to measured sensor data on the arduino. So what I write basically on the arduino site are 3 characters (C language), thus 3 * 8 bits.
My mindstorms code looks like this: (shortened a little bit)
- Code: Select all
public class BTTransmitCmd {
public static void main(String[] args) throws Exception {
String name = "SeeedBTSlave";
LCD.drawString("Connecting...", 0, 0);
LCD.refresh();
RemoteDevice btrd = Bluetooth.getKnownDevice(name);
if (btrd == null) {
LCD.clear();
LCD.drawString("No such device", 0, 0);
LCD.refresh();
Thread.sleep(2000);
System.exit(1);
}
BTConnection btc = Bluetooth.connect(btrd);
if (btc == null) {
LCD.clear();
LCD.drawString("Connect fail", 0, 0);
LCD.refresh();
Thread.sleep(2000);
System.exit(1);
}
LCD.clear();
LCD.drawString("Connected", 0, 0);
LCD.refresh();
DataInputStream dis = btc.openDataInputStream();
DataOutputStream dos = btc.openDataOutputStream();
Thread.sleep(500);
// Let Arduino write 2 characters; those will get lost (XXX : this is also a strange artifact)
dos.write('I');
dos.write('I');
dos.flush();
Thread.sleep(1000);
LCD.clear();
LCD.drawString("Boot up done", 0, 0);
LCD.refresh();
State state = State.IDLE;
int fail = 0;
while(!interrupted) {
// Send current State ...
dos.write(state.get(0));
dos.write(state.get(1));
dos.flush();
// Ask for a sensorvalue
Sensor se = Sensor.REMOTE;
dos.write('S');
dos.write(se.getId());
dos.flush();
long timeout = System.currentTimeMillis() + 450;
int cnt = 0;
int number = 0;
while (cnt < 3 && System.currentTimeMillis() < timeout) {
if (dis.available() > 0) {
cnt++;
number = number*10 + ((int) (((char) dis.read()) - '0'));
}
}
if (cnt != 3) fail++;
LCD.drawString("Value..."+number,0,1);
LCD.drawString("Fail..."+fail,0,2);
LCD.refresh();
if (number == RemoteAnswer.FORWARD.getId()) {
forward();
} else if (number == RemoteAnswer.BACKWARD.getId()) {
backward();
} else if (number == RemoteAnswer.IDLE.getId()) {
idle();
} else if (number == RemoteAnswer.LEFT.getId()) {
left();
} else if (number == RemoteAnswer.RIGHT.getId()) {
right();
}
}
try {
LCD.drawString("Closing... ", 0, 0);
LCD.refresh();
dis.close();
dos.close();
btc.close();
} catch (IOException ioe) {
LCD.drawString("Close Exception", 0, 0);
LCD.refresh();
}
}
The line marked with XXX shows also a strange artifact: The first to characters sent from arduino will be lost, while all characters from the NXT reach the arduino.
The output I get for 38400 baud on arduino site is
���BTSTATE:3
CONNECT:OK
+BTSTATE:4
III write two dummy chars
���DS1I will now answer (IR)
IDS1I will now answer (IR)
IDS1I will now answer (IR)
ID����IDS1I will now answer (IR)
IDS1I will now answer (IR)
IDS1I will now answer (IR)
IDS1I will now answer (IR)
IDS1I will now answer (IR)
IDS1I will now answer (IR)
So the question marks should not be there, but something of the obvious pattern (IDS1).
Using any other feasible baud rate delivers a result like this
4���4 �������P��P��P��P�!�P�@B���������������������4
I use an arduino mega with the 2560 processor und use the following shield:
http://www.seeedstudio.com/wiki/index.php?title=Bluetooth_ShieldWhen Is use the same module for PC-Arduino communication everything works. I use screen as serial console by invoking
- Code: Select all
screen /dev/ttyUSB0 38400
[edit] I just rechecked the things: for arduino-PC connection also only 38400 baud rate works which was not apparent to me before. So maybe the problem is really with the arduino code.
Nevertheless, for 38400 everything works out fine, so it also should work perfectly with the NXT, shouldn't it? [/edit]
So, I am actually quite confused about this behaviour.
arduino