Pages: [1]   Go Down
Author Topic: Software serial routine  (Read 580 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 28
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

I am working in building a rov. I am going to use a playstation controller connected to an arduino on the shore and a second arduino in the rov controlling the motors, lights servos etc.
I want to use software serial to send all the commands from shore to rov so i will only need 3 wires for control, tx,rx and ground, the other wires will be used for video etc.
my guess is that using 9600 baud should be fast enough and reliable enough but I want to be able to change the baud up and down to test reliability, responsivnes against length of cable.

I have tried using  by Tom Igoe based on Mikal Hart's example but I get funny characters on occasion (not all the time, my baudrate etc is good and I get some of the info correctly)
My guess is that as I am reading the serial port continuosly and I am reading it even when there are no bytes ready to be read so its getting a default null character, its a y with two dots on top, char 152 in the ascii table.

Basically I want to send a command from one arduino to another, for example

Send "(A000001)"  which I will decodeto something like, turn on LED A
or    "(A000000)"  turn off led A
or    "(B000120") set motor B speed to 120
or    "(C000170)" set servo C to 170

etc etc the format of the data is not important as long as I can receive it correctly.
I want to use fixed length of commands to keep it simple.
And I will be sending the commands continuosily.

If anyone can help me with the code to do the above I would be very grateful.

Regards
Albert
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 285
Posts: 25630
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want to use software serial to send all the commands from shore to rov so i will only need 3 wires for control, tx,rx and ground, the other wires will be used for video etc.
Why not use the hardware serial?

You need to post your code.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

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

Hi,

I thought the hardware serial is used when connected via the usb to the pc and I use it to monitor and debug.

Can I use the hardware serial and also use the com port created via the ide at the same time.

My code is a mess as I have been trying several combinations so it does not look to good

Albert
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Can I use the hardware serial and also use the com port created via the ide at the same time.
This question does not make a lot of sense. The IDE uses the COM port, set up by the operating system, to upload code. It does not create the COM port.

The Serial Monitor also uses that COM port, but not at the same time that the IDE is uploading a sketch. It does not create the COM port, either.

The other end of the COM port is what the USB to serial converter chip is connected to, via the USB cable, on one side. The other side of the USB to serial converter chip is connected to pins 0 and 1 (on other than the Leonardo).

You can not use those pins to talk to the GPS and the PC at the same time, unless you think that the GPS cares what you are trying to send to the PC.

If you post some code, we can help you understand what might be going wrong.
Logged

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

Hi,

Ok lets keep it simple.
Forgetting software serial etc

I want to send the data as per my first post via any serial option.

Can someone point me to a link with some code that closely reflects what i require ?

keeping in mind a continous stream of data to be decoded into commands.

regards
Albert
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
I want to send the data as per my first post via any serial option.
First, you need some data to send. Do you have that, or is that really the first step?

If the first step is really to get the data from the GPS, you can hardly "forget software serial for the moment".

The TinyGPS library comes with example code that reads GPS data from a device connected to a set of pins being read by SoftwareSerial. Pretty much seems to fit all your needs.

Quote
keeping in mind a continous stream of data to be decoded into commands.
Where is this continuous stream of data coming from? Not the GPS. It sends data in bursts, when it has something to report.

What commands are you referring to?
Logged

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

Hi,

GPS was not mentioned at all.

I am getting info from joystick at arduino 1 and sending info to ardurino2.

Regards
Albert
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
GPS was not mentioned at all.
Oops. Sorry about that.

Quote
I am getting info from joystick at arduino 1
Successfully? Using what code?

Quote
Basically I want to send a command from one arduino to another, for example

Send "(A000001)"  which I will decodeto something like, turn on LED A
or    "(A000000)"  turn off led A
or    "(B000120") set motor B speed to 120
or    "(C000170)" set servo C to 170
There seems to be no reason to send 6 characters to represent a 1 to 3 digit number.
Logged

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

Hi,

The joystick info I will use ic2 routines that are readily available.

The 6 characters were so I can also send other information that might need more than 3 digits, future proofing my code.

The number of characters is not important its the routine I need to send and receive them correctly.

So any suggestions on the code side of things.

Albert
Logged

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

Hi,

I have got it working with some help of examples.
I have the trasmitter side of the software and then the receiver side.
This code just sends the >A001< over and over as fast as it can, I did this to test data loss.
The A001 I will interpret in the receiver to do something for example Motor A speed =1, or servo A position 1.
in this format I can control servos, motors etc  A,B,C,D etc etc and send a value up to 999, I can always increase it to 4 or more characters.
Although by using less digits I can send more info in less time.

Transmiter code
-----------------------------------------------------------------------------------------------
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3); // RX, TX  <<<<<   if using MEGA for transmitter use pins 10,11
void setup() 
{
  // Open serial communications and wait for port to open:
  Serial.begin(19200);
  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.write(">A001<");

 if (mySerial.available()){ //Something has come into the transmitter serial port
  if(mySerial.read()=='-')// if the remote serial full, send - to slow down
  {
    delay(15);// short pause to let remote receiver time t empty some of the serial buffer
  }
 }
}


--------------------------------------------------------------------------------------------------------
receiver code

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX  <<<<<< receiver on MEGA. If using uno you can use pins 2,3.
  char ByteIn;
  String StringIn;
  int led = 13;
void setup() 
{
  // Open serial communications and wait for port to open:
  Serial.begin(19200);
  pinMode(led, OUTPUT); 

  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // set the data rate for the SoftwareSerial port
  mySerial.begin(4800);
  //mySerial.println("Hello, world local?");
}

void loop() // run over and over
{
  if (mySerial.available()) // something waiting to be read on software serial port
  {
    //Serial.println(StringIn);
    Serial.println(mySerial.available());  //show buffer size, this is for info only
    if (mySerial.available() > 50)   //  serial port has 64 byte buffer so to stop it getting full and dropping bytes slow incoming transmission for a bit
    {
     mySerial.write("-"); // send a - to transmitter, could be any character as long as it matches on both sides
    }
    ByteIn = mySerial.read(); // get a byte from the serial port
    //Serial.write(ByteIn);
    StringIn = StringIn + ByteIn; // build up the incoming bytes into the string
    if (ByteIn == '<') // we have the character that tells me that is the end of that command/data.
   {
    // now do something with the string
    if (StringIn != ">A001<") // here you can now do something with the commands that have arrived the != is for not equal to, I used this for testing to see if I was getting any garbage. you should use something like if(StringIn == ">A001<") or break down the comand. get first Character(removing the >), get last 3 characters.
    {

    digitalWrite(led, LOW); 

    }
    else
    {
     digitalWrite(led, HIGH);
    }
   
    StringIn = ""; // clear the string ready to build the next one
   }
  }

}



That is working for me and I can choose any baud rate for the comms, the handshaking will pause if its coming in to fast to proccess. you can change the pause on the transmitter side if required.

Connect pins 2,3 TX,RX and ground from one arduino to the other.
If you also connect the 5v line then you dont even need to power the remote arduino. Although care must be taken if you plug servos etc in the remote one as the power drawn for both arduino will be via the first one.

Albert


Logged

Pages: [1]   Go Up
Jump to: