Pages: [1]   Go Down
Author Topic: multi port reading  (Read 145 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 23
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi.I’m using pins 0,1 of Arduino Uno for xbee shield and it receives gps infos from other arduino.At the same time i want to use pins 8,9 of same arduino for gps module and data. Is it possible to gather datas from both device(xbee shield and gps module) in one sketch and one arduino? ( as you know 0,1 are hardware serial pins,SoftwareSerial is used for 8,9 pins)Which way can you tell or suggest?can you give some details?If you could reply and help i’d be so happy.from now thank you very much for your help.I look forward to your reply.Kind regards,Faruk
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 642
Posts: 50367
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is it possible to gather datas from both device(xbee shield and gps module) in one sketch and one arduino?
Yes, it is.

Quote
Which way can you tell or suggest?
I suggest that you share the code you currently have, and describe what it isn't doing that you want it to do, and/or what it is doing that you don't want it to do.
Logged

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

First of all my project is related to rc car tracking.First RC car is leader.The other is follower. My problem is about follower ,we can call it receiver also. follower's arduino should has two function.

1-It will receive the leader's (GPS) location via xbee and xbee shield(data transfers from pin0 and 1  of Arduino UNO).Succeeded with this code

Code:
void setup() {

Serial.begin(9600);
delay(5000);

}

void loop() {

read_data();

}


void read_data() {

byte data;

while(Serial.available()==0) {}

while(Serial.available()>0) {

data=Serial.read();



write_data(data);

}

Serial.flush();
}

void write_data(byte data)
{

Serial.print(char(data));

}

2-follower should transmit own gps location (found with cooking hacks gps module connected to pin 8 and pin9 of the other Arduino UNO) to another follower via xbee shield.This sketch succeeded also.

Code:
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/*
This sample code demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
It requires the use of SoftwareSerial, and assumes that you have a
4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
static const int RXPin = 9, TXPin = 8;
static const uint32_t GPSBaud = 4800;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(9, 8);

void setup()
{
Serial.begin(9600);
ss.begin(GPSBaud);

Serial.println(F("FullExample.ino"));
Serial.println(F("An extensive example of many interesting TinyGPS++ features"));
Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
Serial.println(F("by Mikal Hart"));
Serial.println();
Serial.println(F("Sats HDOP Latitude Longitude Fix Date Time Date Alt Course Speed Card Distance Course Card Chars Sentences Checksum"));
Serial.println(F(" (deg) (deg) Age Age (m) --- from GPS ---- ---- to London ---- RX RX Fail"));
Serial.println(F("---------------------------------------------------------------------------------------------------------------------------------------"));
}

void loop()
{
static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;

printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
printInt(gps.hdop.value(), gps.hdop.isValid(), 5);
printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
printInt(gps.location.age(), gps.location.isValid(), 5);
printDateTime(gps.date, gps.time);
printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
printStr(gps.course.isValid() ? TinyGPSPlus::cardinal(gps.course.value()) : "*** ", 6);

unsigned long distanceKmToLondon =
(unsigned long)TinyGPSPlus::distanceBetween(
gps.location.lat(),
gps.location.lng(),
LONDON_LAT,
LONDON_LON) / 1000;
printInt(distanceKmToLondon, gps.location.isValid(), 9);

double courseToLondon =
TinyGPSPlus::courseTo(
gps.location.lat(),
gps.location.lng(),
LONDON_LAT,
LONDON_LON);

printFloat(courseToLondon, gps.location.isValid(), 7, 2);

const char *cardinalToLondon = TinyGPSPlus::cardinal(courseToLondon);

printStr(gps.location.isValid() ? cardinalToLondon : "*** ", 6);

printInt(gps.charsProcessed(), true, 6);
printInt(gps.sentencesWithFix(), true, 10);
printInt(gps.failedChecksum(), true, 9);
Serial.println();

smartDelay(5000);

if (millis() > 5000 && gps.charsProcessed() < 10)
Serial.println(F("No GPS data received: check wiring"));
}

// This custom version of delay() ensures that the gps object
// is being "fed".
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}

static void printFloat(float val, bool valid, int len, int prec)
{
if (!valid)
{
while (len-- > 1)
Serial.print('*');
Serial.print(' ');
}
else
{
Serial.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1); // . and -
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i=flen; i<len; ++i)
Serial.print(' ');
}
smartDelay(0);
}

static void printInt(unsigned long val, bool valid, int len)
{
char sz[32] = "*****************";
if (valid)
sprintf(sz, "%ld", val);
sz[len] = 0;
for (int i=strlen(sz); i<len; ++i)
sz[i] = ' ';
if (len > 0)
sz[len-1] = ' ';
Serial.print(sz);
smartDelay(0);
}

static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
{
if (!d.isValid())
{
Serial.print(F("********** "));
}
else
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
Serial.print(sz);
}

if (!t.isValid())
{
Serial.print(F("******** "));
}
else
{
char sz[32];
sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
Serial.print(sz);
}

printInt(d.age(), d.isValid(), 5);
smartDelay(0);
}

static void printStr(const char *str, int len)
{
int slen = strlen(str);
for (int i=0; i<len; ++i)
Serial.print(i<slen ? str[i] : ' ');
smartDelay(0);
}

//xbee
void read_data() {

byte data;

while(Serial.available()==0) {}

while(Serial.available()>0) {

data=Serial.read();



write_data(data);

}

Serial.flush();
}

void write_data(byte data)
{

Serial.print(char(data));

}

As i told before, can we unify this sketches and gather datas from both device(xbee shield and gps module) in one sketch and one arduino? ( as you know 0,1 are hardware serial pins,SoftwareSerial is used for 8,9 pins)Which way can you tell or suggest?I look forward to your reply.Best.Faruk
Logged

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

to gather and showing the locations of leader and follower in the follower's arduino is aim.
Logged

Santiago de Chile
Offline Offline
Sr. Member
****
Karma: 13
Posts: 463
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't like Software serial nor do I know its capabilities, because If I need more than 1 UART either I add another cheap microcontroller to the project to add 1 UART and then send info to the master via SPI or I2C, or just buy a microcontroller with 2 UART (as the atmega1284P).
Logged

Work for what you love

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

Thx buy I can not prefer your method.
I don't like Software serial nor do I know its capabilities, because If I need more than 1 UART either I add another cheap microcontroller to the project to add 1 UART and then send info to the master via SPI or I2C, or just buy a microcontroller with 2 UART (as the atmega1284P).
Logged

UK
Offline Offline
Tesla Member
***
Karma: 135
Posts: 7738
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This demo shows a way to merge two sketches.

I haven't studied your code but you should be able to receive data from the two devices on a single Uno. There is a possibility of the libraries conflicting with each other. If that happens there are other software serial options.

You haven't said if you have more than two R/C cars - and if you have I think the process will get a lot more complex.

...R
Logged

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

Thank you Robin I'll try,but at that time ill be pleased from other solutions.
This demo shows a way to merge two sketches.

I haven't studied your code but you should be able to receive data from the two devices on a single Uno. There is a possibility of the libraries conflicting with each other. If that happens there are other software serial options.

You haven't said if you have more than two R/C cars - and if you have I think the process will get a lot more complex.

...R
Logged

Pages: [1]   Go Up
Jump to: