Go Down

Topic: TwoPortRX ...but with multiple software serial and a hardware serial (Read 655 times) previous topic - next topic

Robin2

I thought the recent versions of the IDE use new soft serial by default?

...R

nikki

Apparently they're not quite the same beast:
Quote

THIS IS THE SOFTWARE SERIAL BRANCH
==================================
Note, this branch is intended for use with the NewSoftSerial library. (The core SoftwareSerial library doesn't cut it, because it's missing certain function calls.) (source)

nikki

A quick update...

Well, I decided that I could do without my thermostat for a few hours and give this a go with a Mega, however without success. It seems there's a conflict with the tinyGPS library somewhere - all was working well on multiple serial ports until I started calling the gps functions.

I then switched to working with two adjacent Nanos.

Nano #1
Essentially loaded with the BrainSerial example

Code: [Select]
// Arduino Brain Library
// Serial out example, grabs the brain data and sends CSV out over the hardware serial.
// Eric Mika, 2010

#include <Brain.h>

// Set up the brain parser, pass it the hardware serial object you want to listen on.
Brain brain(Serial);


void setup() {
  // Start the hardware serial.
  Serial.begin(9600);
}

void loop() {
  if (brain.update()) {
    //Serial.println(brain.readErrors());
    Serial.print(brain.readCSV());
  }

}


As well as being connected to the brain thing, this board shares a common ground with Nano #2 and has wires between its hardware RX and TX pins going to SoftwareSerial pins on Nano #2.

Nano #2
Receives the brain csv data from the first Nano over a SoftwareSerial connection. Each time this data comes in it appends data from the GPS and is currently displaying some of this on an OLED display:

Code: [Select]

#include <SoftwareSerial.h>
#include <TinyGPS.h>  // includes the TinyGPS library

#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

TinyGPS gps;
SoftwareSerial gpsmodule(5, 6);
SoftwareSerial brainIn(2, 3); // RX, TX

//define GPS variables

float flat, flon; // +/- latitude/longitude in degrees
unsigned long date, time, age; // what they say on the tin
long lat, lon; // +/- lat/long in 100000ths of a degree


#define OLED_DC 11
#define OLED_CS 12
#define OLED_CLK 10
#define OLED_MOSI 9
#define OLED_RESET 13
Adafruit_SSD1306 display(OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS);

#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif

void setup()  {
  gpsmodule.begin(4800); // serial communication between the gps module and the microcontroller
  Serial.begin(9600); //connection to host computer
  brainIn.begin(9600);    //connection to arduino parsing the brain data

  // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  display.begin(SSD1306_SWITCHCAPVCC);
  // init done


  display.clearDisplay();   // clears the screen and buffer

}

void loop()                     
{

  getGPS(); // get GPS data


  brainIn.listen();

  //give some time for data to come in on the brainIn port
  for (unsigned long start = millis(); millis() - start < 1000;)
  {
  }
  if (brainIn.available()){
    while (brainIn.available())
    {
      Serial.print((char)brainIn.read());  //grab avail data from brain parsing arduino
    }
    Serial.print(",");

    Serial.print(flon, 6); //print lat and lon coordinates
    Serial.print(",");
    Serial.print(flat, 6);
    Serial.print(",");
    Serial.print(date);
    Serial.print(",");
    Serial.println(time);

    display.clearDisplay();   // clears the screen and buffer
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(0,0);
    display.println(time);

    display.setCursor(0,10);
    display.print(flon, 6);
    display.print(", ");
    display.println(flat, 6);

    display.setCursor(0,20);
    display.print(flon, 6);
    display.print(", ");
    display.println(flat, 6);

    display.display();


  }

} //end loop


void getGPS(){
  gpsmodule.listen();
  bool newData = false;
  unsigned long chars;
  unsigned short sentences, failed;
  while (newData == false){
    // For one second we parse GPS data and report some key values
    for (unsigned long start = millis(); millis() - start < 1000;)
    {
      while (gpsmodule.available())
      {
        char c = gpsmodule.read();
        // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
        if (gps.encode(c)) // Did a new valid sentence come in?
          newData = true;
      }
    }

    if (newData)
    {
      gps.get_datetime(&date, &time, &age); // time in hhmmsscc and date in ddmmyy formats

      gps.f_get_position(&flat, &flon, &age);  // latitude and longitude values with a decimal point eg 54.117769


    } //end newData
  }

} //end getGPS




I've not added in the logger yet, but it seems reasonably stable printing to the serial monitor, although there's occasional ingress of garbled characters.

I'd like to be able to print the first 3 values in the comma separated list coming through from the brain thing onto the OLED screen. Typically something like : "0, 23, 85" (The first is a measure of quality of connection, max value 200; and the others are measures of 'concentration' and 'meditation', max values 100)

Would I be right in thinking the way to approach this would involve putting the arriving characters into a buffer array as they arrive so I can then split it into variables and later use them to print out?

PaulS

Quote
Would I be right in thinking the way to approach this would involve putting the arriving characters into a buffer array as they arrive so I can then split it into variables and later use them to print out?

Yes. strtok() can do the tokenizing, then.

Quote
It seems there's a conflict with the tinyGPS library somewhere - all was working well on multiple serial ports until I started calling the gps functions.

Perhaps you could elaborate. What code, what problems, etc.

nikki


Yes. strtok() can do the tokenizing, then.


Thanks, will start with Googling both of those!


Perhaps you could elaborate. What code, what problems, etc.


Only vaguely from memory (it was late last night).
I set up the example I linked to above to work on Serial1, leaving Serial to deal with communication with the PC. That worked fine. When I started incrementally adding in the GPS functionality on Serial2 (previously tested independently), it continued to work (printing out the CSV style list of brain wave values to the serial monitor) with the library and variables added in, but when I added in getGPS() and the getGPS() subroutine (is that the right word for it?) I only got the GPS data and the brain stuff disappeared again.

Possibly even what was happening in the first place (original post)...

I'm curious to find out more about what's going on and why there are problems, but I'll be away for a bit so I'm not likely to get a chance to delve into it for a while.

Go Up