Pages: [1]   Go Down
Author Topic: problem using Software Serial  (Read 2170 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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:
#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.
Logged

Regards,
ThunderWiring

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13742
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!
Logged

Regards,
ThunderWiring

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13742
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

-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:
#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;
    }
  } 
}
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset 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..
Logged

Regards,
ThunderWiring

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13742
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Rob Tillaart

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

Offline Offline
Newbie
*
Karma: 0
Posts: 16
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

rx:
Code:
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}
}
Logged

Regards,
ThunderWiring

Pages: [1]   Go Up
Jump to: