problem using Software Serial

hello ,
im having a problem using the software serial library.
basically, i want to send an analog value from one arduino board to another(both are uno)

circuit: ImageShack - Best place for all of your image hosting and image sharing needs

code for tx board:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(12,13); // RX, TX
void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600); 
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600); 
  
  pinMode(0,INPUT); // pot analog value.
  pinMode(12,INPUT);//rx
  pinMode(13,OUTPUT); //tx
}

void loop() 
{
  Serial.write(analogRead(0));
  if(Serial.available()>0)
  {
    mySerial.write(Serial.read());   
  }
}

code for rx board:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(12,13); // RX, TX
void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600); 
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600); 

  pinMode(12,INPUT);//rx
  pinMode(13,OUTPUT); //tx
}

void loop() 
{
 mySerial.listen();
  if (mySerial.available()>0)
  {
    if(mySerial.read()<400)
    {
      //do something
    }
    else if(mySerial.read()>900)
    {
      //do something else
    }
  }  
}

any idea what went wrong?

thanks.

The receiving arduino receives the character representation, not the numeric value

you must make a parser that builds up the number one digit at a time.

i dont think thats the problem, because even if it is, the data which is being recieved is corrupted; it shows -1 all the time!!

-1 means that there are no characters received,

Have you connected the TX of the sender to the receiver RX ?

Furthermore the line if(mySerial.read()<400) is always true
as serialRead() returns a int representing a char which is in the range 0..255 (and -1 if there is none)

#include <SoftwareSerial.h>
SoftwareSerial mySerial(12,13); // RX, TX

int val = 0;

void setup()  
{
  // Open serial communications and wait for port to open:
  Serial.begin(9600); 
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600); 

  pinMode(12,INPUT);//rx
  pinMode(13,OUTPUT); //tx
}

void loop() 
{
  bool ready = false;
  mySerial.listen();
  if (mySerial.available()>0)
  {
    int c = mySerial.Read();
    switch(c)
    {
      case '0'..'9': val *= 10 + c - '0'; break;  // add up individual chars to a numeric value
      default: ready = true;
    }
    if (ready)
    {
      if (val < 400)
      {
        //do something
      }
      else if (val > 900)
      {
        //do something else
      }
      else
      {
        //do something completely different
      }
      val = 0; // reset;
    }
  }  
}

thanks man! i manipulated your code a little and got mine to function properly!
you were right, if(mySerial.read()<400) is always true statement..
i used your approach of storing the char, switch/case it and then setting the boolean value for the 'if' statements..

thanks man! i manipulated your code a little and got mine to function properly!

Maybe post your working code for future reference for people who find this thread interesting?

since i wanted to compare the analog value, i did the comparison on the TX board, and sent a char over mySerial('f' for false or 't' for true) to the RX board, there on the RX i stored that char, then used switch\case to set a boolean flag for the 'if' statements..
it worked fine!
tx:

if(value<450)
  {
    mySerial.write('f'); 
    
  }
  if(value >900) 
  {
    mySerial.write('t'); 
    
  }

rx:

char RXdata; boolean flag=false;
void loop()
{
   
  mySerial.listen();
  if (mySerial.available()>0)
  {
    RXdata = mySerial.read();
    switch(RXdata)
    {
      case 't':
                flag=true;
                break;
      case 'f':
                //flag = false;
                break;
   }
  }
  if(flag)  
  {// do something}
  else{//do something else}
}