Go Down

Topic: problem using Software Serial (Read 2284 times) previous topic - next topic

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: http://imageshack.us/photo/my-images/10/21sz.jpg/

code for tx board:
Code: [Select]
#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:
Code: [Select]

#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.
Regards,
ThunderWiring

robtillaart


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.
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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!!
Regards,
ThunderWiring

robtillaart

-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)

Code: [Select]

#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;
    }
  } 
}
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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..
Regards,
ThunderWiring

robtillaart

Quote
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?
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

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:
Code: [Select]
if(value<450)
  {
    mySerial.write('f');
   
  }
  if(value >900)
  {
    mySerial.write('t');
   
  }


rx:
Code: [Select]
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}
}
Regards,
ThunderWiring

Go Up