Read ASCII String with Arduino Micro

Hello,

I have problems reading an ASCII String with my Arduino Micro.
When I upload the code from the Tutorial (http://arduino.cc/en/Tutorial/ReadASCIIString), I can control the LED's by entering something like "10,250,10" in the serial monitor.
But I want to control one Arcuino Micro by another.

So, for testing I set up the Transmitter-Arduino with following code:

void setup()
{
Serial1.begin(9600);
}
void loop()
{
Serial1.println("1,250,1");
delay(500);
Serial1.println("250,1,250");
delay(500);
}

And I modified the code from the Tutorial to receive serial data from the RX pin (replaced every "serial" with "serial1"):

// pins for the LEDs:
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;

void setup() {
// initialize serial:
Serial1.begin(9600);
// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

}

void loop() {
// if there's any serial available, read it:
while (Serial1.available() > 0) {

// look for the next valid integer in the incoming serial stream:
int red = Serial1.parseInt();
// do it again:
int green = Serial1.parseInt();
// do it again:
int blue = Serial1.parseInt();

// look for the newline. That's the end of your
// sentence:
if (Serial1.read() == '\n') {
// constrain the values to 0 - 255 and invert
// if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
red = constrain(red, 0, 255);
green = constrain(green, 0, 255);
blue = constrain(blue, 0, 255);

// fade the red, green, and blue legs of the LED:
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);

But nothing is happening. I don't know where the mistake is and I'm trying to find out since days now.
I'm grateful for every help.

Thanks
Flo

You have two Arduino Micro boards ?
http://arduino.cc/en/Main/arduinoBoardMicro

How are they connected ? Both with pin 0 and 1 ?

This is what you can do, step by step.

Connect pin 0 to other board pin 1
Connect pin 1 to other board pin 0
Connect GND to other board GND

Use 'Serial1' on both boards. Since 'Serial' is only the communication to the terminal window on the computer.

For the transmitter board, you send debug and status messages to 'Serial', and the commands to the other Arduino via 'Serial1'.
For the receiver board, send also debug and status messages to 'Serial', and receive commands via 'Serial1'.

You can upload your new sketches between the code tags. Click on the '#'-button for code tags.

Thank you, Caltoa,

that's exactly the way I did.

For better understanding I tested a simple code for checking if the receiver receives the data from the transmitter:

void setup()
{
Serial.begin(9600);
Serial1.begin(9600);
}
void loop()
{
if(Serial1.available())
{
Serial.write(Serial1.read());
}
}

And that works...

It seems like serial1.parseint will not work in the code that now looks like that:

const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
int red;
int green;
int blue;

void setup()
{
// initialize serial:
Serial.begin(9600);
Serial1.begin(9600);
// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
}

void loop()
{
// if there's any serial available, read it:
if (Serial1.available())
{
// look for the next valid integer in the incoming serial stream:
red = Serial1.parseInt();
// do it again:
//green = Serial1.parseInt();
// do it again:
//blue = Serial1.parseInt();

// look for the newline. That's the end of your
// sentence:
if (Serial1.read() == '\n')
{
// constrain the values to 0 - 255 and invert
// if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
red = constrain(red, 0, 255);
green = constrain(green, 0, 255);
blue = constrain(blue, 0, 255);

// fade the red, green, and blue legs of the LED:
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
}
}

Could it be possible that arduino only understands Serial.parseInt and not Serial1.parseInt??

Thanks
Flo

It should be the same, since they should both use the 'stream' class.

A command from a PC can be different than a command from an Arduino. The trailing CR, LF can be different.
Perhaps let the transmitter sent the numbers once in 5 seconds. So you have more time to see what is going on.
If you use parsInt() for the three numbers, does that work the first time ?
You can also dump the incoming data as HEX numbers to the serial monitor.

There is a program flow problem.
Suppose something is available, and the three numbers are read. However, there is no '\n' but a '\r' waiting. The loop() function starts again, and executes parseInt() again on the '\r'. The parseInt() waits until a new set of numbers are received and reads those, but the leds are never set.
Perhaps you can add a flag (boolean flagBusy;). Start reading integers if something is available and the flag is false. Set the flag true if the integers are being read. If a '\r' or '\n' or timeout is detected, the leds are set (only if the flag was true) and the flag is set false for a new set of integers.

You have commented out reading 3 integers in the receiver. I hope you did the same for the transmitter.

Now it works!
To tell the truth, I don't know why now and not before.
The only thing I changed is to take the analogWrite-Part out of the if(new line) - phrase.

Now the code looks like that:

// pins for the LEDs:
const int redPin = 9;
const int greenPin = 10;
const int bluePin = 11;
int red;
int green;
int blue ;

void setup() {
// initialize serial:
Serial.begin(9600);
Serial1.begin(9600);
// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);

}

void loop()
{
// if there's any serial available, read it:
while(Serial1.available() > 0)
{
// look for the next valid integer in the incoming serial stream:
red = Serial1.parseInt();
// do it again:
green = Serial1.parseInt();
// do it again:
blue = Serial1.parseInt();

// look for the newline. That's the end of your
// sentence:
if (Serial1.read() == '\n')
{
// constrain the values to 0 - 255 and invert
// if you're using a common-cathode LED, just use "constrain(color, 0, 255);"
red = constrain(red, 0, 255);
green = constrain(green, 0, 255);
blue = constrain(blue, 0, 255);
}

// fade the red, green, and blue legs of the LED:
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);

//print the three numbers in one string as hexadecimal:
Serial.print(red);
Serial.print(",");
Serial.print(green);
Serial.print(",");
Serial.println(blue);
}
}

Now, with a transmitter sending variantly "1,250,1" and "250,1,250" every 0,5sec the LED's are blinking

Thanks
Flo