serial communication problem

Hi,

So i have two arduino's hooked up to send something from one to the other, it was based on the example file.
But the problem is that when i send two chars it doesnt work, only when i send one. Can anyone figure out why it cant send two?

sender code (uno):

#include <SoftwareSerial.h>

SoftwareSerial mySerial(11, 12); // RX, TX

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
}

void loop() // run over and over
{
  mySerial.println("th");
  delay(1000);
  mySerial.println("tl");
  delay(1000);
}

receiver code (mini pro):

#include <SoftwareSerial.h>

SoftwareSerial mySerial(13, 12); // RX, TX

const int ledPin = 10; // the pin that the LED is attached to
int incomingByte;      // a variable to read incoming serial data into

void setup() {
  // initialize serial communication:
  mySerial.begin(4800);
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
}

void loop() {
  // see if there's incoming serial data:
  if (mySerial.available() > 0) {
    // read the oldest byte in the serial buffer:
    incomingByte = mySerial.read();
    // if it's a capital H (ASCII 72), turn on the LED:
    if (incomingByte == 'th') {
      digitalWrite(ledPin, HIGH);
    } 
    // if it's an L (ASCII 76) turn off the LED:
    if (incomingByte == 'tl') {
      digitalWrite(ledPin, LOW);
    }
  }
}
if (incomingByte == 'th') {

Single quotes denote a single byte. th or tl aren't single bytes, they are strings. When you read from Serial, you need to store single bytes in an array and use something like strcmp() to compare the strings.

Can anyone figure out why it cant send two?

You can send two. You can't pretend that the two characters are one on the receiving end, though. You are not allowed to invent shortcuts without rewriting the code and/or compiler.

Ill give you a hint, your code right now gets a char(single), stores it then compares it. If it has nothing to compare to, it drops out. So you need to make it so that it does NOT drop out, but instead allows another char to be ADDED in, then compares it.

Afterwards you need to clear it.

On the sending end, add a delimiter to the character string being sent. Then it is easier to capture the sent string for extracting data. Simple serial string capture code.

//zoomkat 3-5-12 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

String readString;

void setup() {
  Serial.begin(9600);
  Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}

void loop() {

  //expect a string like wer,qwe rty,123 456,hyre kjhg,
  //or like hello world,who are you?,bye!,
  
  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      Serial.println(readString); //prints string to serial port out
      readString=""; //clears variable for new input      
     }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

thank you all for replying,
So the master is sending "on," "off," now and the serial in on the slave gets the on and off but the led is not going on and off, im guessing another rookie mistake:

#include <SoftwareSerial.h>

SoftwareSerial mySerial(13, 12); // RX, TX
const int ledPin = 10; // the pin that the LED is attached to
String readString;

void setup() {
  Serial.begin(4800);
  mySerial.begin(4800);
  pinMode(ledPin, OUTPUT);
}

void loop() {
  
  if (mySerial.available())  {
    char c = mySerial.read();  //gets one byte from serial buffer
    if (c == ',') {
      //do stuff
      
          // turn on the LED:
    if (readString == "on") {
      digitalWrite(ledPin, HIGH);
    } 
        // turn off the LED:
    if (readString == "off") {
      digitalWrite(ledPin, LOW);
    }
      
      Serial.println(readString); //prints string to serial port out
      readString=""; //clears variable for new input      
     }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

im guessing another rookie mistake:

More than one. First, the serial prints don't define what data you are printing. Second, the serial prints don't show where the data of interest starts and ends. Third, you didn't share the output with us.

Serial.println(readString);

would convey a LOT more information as:

Serial.print("Serial port data: [");
Serial.print(readString);
Serial.println("]");

One final problem is the String class. When you KNOW how much data is to be sent, using NULL terminated char arrays is far better. Quit using the String class crutch.

the output:

on

off

on

off

Im using the string class because i dont know any better, i only program in php.
I dont know c++ language so i base it on lots of examples.

the output:

contains a lot of blank lines. That makes it appear that you are using the Serial Monitor to send data, and that the Serial Monitor is configured to add carriage return and line feed to the string being sent.

Printing the data as I showed would confirm exactly what is in readString.

So, what you should be doing is looking for the String to contain "on" or "off", rather than for the String to BE "on" or "off".

contains a lot of blank lines. That makes it appear that you are using the Serial Monitor to send data, and that the Serial Monitor is configured to add carriage return and line feed to the string being sent.

The master send "on," "off," like the code in the first post, and when i checked the serial monitor i saw those spaces too. I did not had those spaces when i used the example.

So, what you should be doing is looking for the String to contain "on" or "off", rather than for the String to BE "on" or "off".

I was thinking the same thing, will check on how to do this.

I did it like this:

       // turn off the LED:
   if (readString.startsWith("off", 2)) {
      digitalWrite(ledPin, LOW);
    }

It works fine, but i was looking for a "if string contains" function.
Ive hooked it up wireless now and there is lots of junk in the air, and the index 2 wont make sense anymore now.

Some copy/paste mods

//zoomkat 3-5-12 simple delimited ',' string parce 
//from serial port input (via serial monitor)
//and print result out serial port
// CR/LF could also be a delimiter

int ledPin = 13;
String readString;

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT); 
  Serial.println("serial LED on/off test with , delimiter"); // so I can keep track
}

void loop() {

  if (Serial.available())  {
    char c = Serial.read();  //gets one byte from serial buffer
    if (c == ',') {
      if (readString.length() >0) {
        Serial.println(readString); //prints string to serial port out
        //do stuff with the captured readString
        if(readString.indexOf("on") >=0)
        {
          digitalWrite(ledPin, HIGH);
          Serial.println("LED ON");
        }
        if(readString.indexOf("off") >=0)
        {
          digitalWrite(ledPin, LOW);
          Serial.println("LED OFF");
        }       
        readString=""; //clears variable for new input
      }
    }  
    else {     
      readString += c; //makes the string readString
    }
  }
}

It works fine, but i was looking for a "if string contains" function.

That's what the indexOf() method does.

   if (readString.startsWith("off", 2)) {

Why are you skipping the first 2 characters?

Perhaps the trim() method is of interest, too. Carriage return and line feed are white space.

Ah sorry the example showed indexof as an integer thats why it did not make sense.
Trim can clean this up too yeah.

Yeah i figured out that i dont need to skik anything and works fine like this:

if (readString.startsWith("off")) {

So its basicly the same as:

if(readString.indexOf("on") >=0) {

Why the ">=0"?

So it works, now i just have to send it wireless, some going wrong there but at least the code is working now.

Why the ">=0"?

Because indexOf() returns the offset from the beginning of the String to the searched for string. The searched for string could be at the start of the searched String, so offset would be 0. It could be somewhere else, so offset would be > 0. It could not exist, so offset would be -1.

Why the ">=0"?

In this case it is copy/paste carryover from serial code that does not use a delimiter. You should be able to remove it if desired.

thank you for the help and info