PDA

View Full Version : Teensy 3 issue with arduino compatible code



blitzter
05-25-2013, 09:44 PM
I'm Using teensy 3 with teensyduino 1.14rc2 and Arduino 1.0.5 on Fedora 18

Couple of problems with the following code...
1. No output to Bluetooth serial console although bluetooth commands from my mobile are getting to the program. This works on Arduino with the same Bluetooth module.
2. I'm sending integers as one command but am getting the code executed in the next command.(maybe I'm missing something)
For example .. if I send 2, which should turn the led ON, nothing happens. The led does turn ON after the next command which can be anything valid/invalid 0,1,2,3,4,5, ...

I did a "cat /dev/ttyACM0 > text.log" but it also does not seem to have every thing that should have been printed. (a few lines are there though)
I have tried Serial2,3 with the same result.



/**
* Bluetooth module
**/
HardwareSerial3 bt = HardwareSerial3();
int inByte = 0;
const int led1 = 23;
const int led2 = 12;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(19200);
bt.begin(9600);
bt.println();
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}

void loop() {
if (bt.available() > 0) {
// get incoming byte:
digitalWrite(led1,LOW);
inByte = bt.parseInt();
bt.clear();
bt.print("I received: ");
bt.print(inByte,DEC);
Serial.print("I received: ");
Serial.println(inByte,DEC);
//Serial2.println("Command Recieved");
handleCommand(inByte);
delay(48);
digitalWrite(led1,HIGH);
}
}

void handleCommand(int command) {
switch(command){
case 0: //stop everything
digitalWrite(led2,LOW);
break;
case 1: //Motor1 Forward
digitalWrite(led2,LOW);
break;
case 2: //Motor1 Reverse
digitalWrite(led2,HIGH);
break;
case 3: //Motor2 Forward
digitalWrite(led2,HIGH);
break;
default:
bt.print("Unrecognized Command");
Serial.println("Unrecognized Command");
}
}

blitzter
05-26-2013, 06:15 AM
I changed my code back to the simpler version and started monitoring the serial output
This is the output i get for sending 0,1,2,3,4,5,6,7,8,9,0

I received: 0
I received: 0
I received: 1
I received: 2
I received: 3
I received: 4
Unrecognized Command
I received: 5
Unrecognized Command
I received: 6
Unrecognized Command
I received: 7
Unrecognized Command
I received: 8
Unrecognized Command
I received: 9
Unrecognized Command


You can see that the first 0 is repeated... i tried reading 2 times just for the first byte but it does not work (see the first byte usage)
Seems like teensy repeats the first byte.


Here is the code


/**
* Bluetooth module
**/

int inByte = 0;
boolean firstByte = true;
const int led1 = 23;
const int led2 = 12;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Serial3.begin(9600);
Serial3.println();
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}

void loop() {
if (Serial3.available() > 0) {
// get incoming byte:
digitalWrite(led1,LOW);
inByte = Serial3.parseInt();
if(firstByte) {
firstByte = false;
inByte = Serial3.parseInt();
}
Serial3.clear();
Serial3.print("I received: ");
Serial3.print(inByte,DEC);
Serial.print("I received: ");
Serial.println(inByte,DEC);
handleCommand(inByte);
delay(48);
digitalWrite(led1,HIGH);
}
}

void handleCommand(int command) {
switch(command){
case 0: //stop everything
digitalWrite(led2,LOW);
break;
case 1: //Motor1 Forward
digitalWrite(led2,LOW);
break;
case 2: //Motor1 Reverse
digitalWrite(led2,HIGH);
break;
case 3: //Motor2 Forward
digitalWrite(led2,HIGH);
break;
default:
Serial3.print("Unrecognized Command");
Serial.println("Unrecognized Command");
}
}

blitzter
05-27-2013, 08:20 PM
I found a sort of workaround for both issues,

1. using read() instead parseInt() fixes the issue with a one command late
2. Changing int inByte to char fixed the Serial3.print() (although it still doesn't print strings, just char)