Show Posts
Pages: [1] 2 3
1  Using Arduino / Project Guidance / Re: Using SoftwareSerial from within a sketch and a library? on: September 07, 2014, 07:31:25 am
Can you share part of code. How did you configure the Software serial library.
Have you checked your GPS module individually working fine??

I haven't yet integrated the SoftwareSerial library into the code for what I want to do, because I didn't know how to go about printing to it from the main sketch and within my library's .cpp file.

The GPS hardware is working okay on it's own, as is the code. It's when I start adding other things in that I get those strange values. I need to do more testing to try and identify if this is because of the hardware and something electrical, or because of the code...

I don't understand why you want to use SoftwareSerial on a Mega?

Because:
Serial will be used for frequent updates to the code (with awkward access to the board, so I don't want to use this for communicating with hardware too)
Serial1 is used for a GPS module
Serial2 is used for a GPS module
Serial3 I want to use for an xbee

SoftwareSerial for writing to a logger.


Yes, it is possible and quite easy.

You only need to create an instance of SoftwareSerial at your main code and then hand over a pointer to this instance to your library.

I have written a library to handle two serial lines within a library and have done exactly what you want. You can have a look (and of course use) my code at github: https://github.com/solick/twoSerialComm


Thanks solick, I think that will indeed help!

I've had a look around twoSerialComm and I'm up for having a go at pulling out the SoftwareSerial elements and applying them to my needs. Not sure If I'll get a chance to do that for a day or two, but am optimistic  smiley-grin
2  Using Arduino / Project Guidance / Using SoftwareSerial from within a sketch and a library? on: September 04, 2014, 03:38:37 am
Hi,

I'm building a system based on an Arduino Mega 2560 that reads from two EM406 GPS modules, reads and writes to an XBee and logs to an OpenLog module. There are also a servo and a few solenoids in the mix too. Actually, it's currently based on 2 Megas, but if possible I'd like to bring it all on to the one board...

I've worked with the 2 GPS modules via SoftwareSerial before, but I've moved to the hardware serial ports on the Mega to avoid delays caused by having to spend time listening on one port and then on the other. (I'm comparing two calculated positions for a given point in time - it's an art project, don't worry if it sounds loony!) First iterations of the build have GPS1 on Serial1, GPS2 on Serial2 and then I'm writing to the logger on Serial3. I was having problems with the GPS modules failing in a way I don't understand (see below), so I offloaded control of the servo and solenoids to a second Mega via simple commands sent over SoftwareSerial from the first micro controller.

Here's what happens when the GPS falls over - kind of a sub-issue, but posting in case anyone can shed any light on it:

Code:
GPS1Label,GPS1date,GPS1time,GPS1hdop,GPS1sats,GPS1flat,GPS1flon,GPS1alt,GPS1course,GPS1speed,GPS2Label,GPS2date,GPS2time,GPS2hdop,GPS2sats,GPS2flat,GPS2flon,GPS2alt,GPS2course,GPS2speed,GPSDifference,currentConfined,confinedTimer,distressedFlag,solenoidDriver


GPS1: ,0,4294967295,4294967295,255,1000.000000,1000.000000,1000000.00,1000.00,-1.00,GPS2: ,0,4294967295,4294967295,255,1000.000000,1000.000000,1000000.00,1000.00,-1.00,0,0
GPS1: ,0,4294967295,4294967295,255,1000.000000,1000.000000,1000000.00,1000.00,-1.00,GPS2: ,0,4294967295,4294967295,255,1000.000000,1000.000000,1000000.00,1000.00,-1.00,0,0
GPS1: ,0,4294967295,4294967295,255,1000.000000,1000.000000,1000000.00,1000.00,-1.00,GPS2: ,0,4294967295,4294967295,255,1000.000000,1000.000000,1000000.00,1000.00,-1.00,0,0


I'm about to start adding in the XBee functionality and ideally that would be connected to Serial3 and, since I'm only writing to the logger, that will be connected via SoftwareSerial to the first (and hopefully only) Mega.

Here's the complicating bit:
I'm using the TinyGPS library to read the GPS modules, however I've incorporated this into a library of my own to make the double GPS stuff tidier.

Please note: this makes my programming skills seem way, way more advanced than they really are! I've had a lot of help with the library-making process and have fumbled through the bits I've done on my own. I've yet to really master the vocabulary, so please bear that in mind in any replies - examples and phrasing things in ways that I can then search for in order to understand them would be greatly appreciated!

So anyway, the logger currently gets written to from within my library's .cpp file and within the main sketch itself.

My (first) question is: is it possible to bring SoftwareSerial functionality into the .cpp file of my library, whilst still also being able to write to the logger from the main sketch as well?

Secondary questions are along the lines of how complex this would be to do and whether it would be feasible for me to undertake with my level of skills!


I think I could probably continue to work around things by using multiple microcontrollers, but it feels like an inelegant cludge.

I'm away from home for a few days now, unsure if I'll be able to post replies, but I can post example code if that helps when I get back. In the meantime though, what do you reckon? What sort of thing might be involved in bringing everything back onto the same board?

3  Using Arduino / Programming Questions / Re: TwoPortRX ...but with multiple software serial and a hardware serial on: December 04, 2013, 07:34:02 pm
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.
4  Using Arduino / Programming Questions / Re: TwoPortRX ...but with multiple software serial and a hardware serial on: December 04, 2013, 06:14:49 pm
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:
// 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:
#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?
5  Using Arduino / Programming Questions / Re: TwoPortRX ...but with multiple software serial and a hardware serial on: December 04, 2013, 03:47:08 am
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)
6  Using Arduino / Programming Questions / Re: TwoPortRX ...but with multiple software serial and a hardware serial on: December 03, 2013, 03:47:54 pm
a Mega, with multiple hardware serial ports

Hmm, that's an idea, I had forgotten about those, thanks. Sounds like it would be a tidy solution.
I don't have a spare Mega to hand at the moment, but I've one on order and may try to liberate one from an existing project if I get too impatient waiting for that!

In the meantime I might have a stab at some Serial.print() statements in the class, or, alternatively, a convoluted work-around with some xbees I have, just so I can get to a proof of concept stage.

Why not write a short sketch to test reading the brain thing with hardware serial then, when that works try transferring it to software serial.

...R

The example sketch reading the brain thing with hardware serial works okay, but there are issues with transferring it to software serial. For starters, the library documentation says that the SoftwareSerial included in the Arduino IDE isn't quite up to the task and that the NewSoftSerial library is better. Trying to use this causes compiling errors with v1.0+. These kick in as soon as you add #include <NewSoftSerial.h> to the working code, so I've not pursued that any further.

I got compiling errors when I tried to use the NewSoftSerial branch of the library too, although IIRC slightly different ones in this case. Same when I tried it with an older (022) version of the IDE. I dropped that line of enquiry because I wanted to be able to use up-to-date libraries etc for the other components and I felt using an older version of the IDE would be likely to cause more problems further down the line...
7  Using Arduino / Programming Questions / Re: TwoPortRX ...but with multiple software serial and a hardware serial on: December 03, 2013, 05:40:13 am
"I'm struggling..." doesn't tell us squat.

Heh,  well, I can't argue with that statement...

Here's some complete code if anyone would like to see it:
Code:
#include <SoftwareSerial.h> //includes the software serial library
#include <TinyGPS.h>  // includes the TinyGPS library

#include <Brain.h>


TinyGPS gps;
SoftwareSerial gpsmodule(3, 4); 
Brain brain(Serial);


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


void setup() 
{

  gpsmodule.begin(4800); // serial communication between the gps module and the microcontroller

  Serial.begin(9600);  // serial communication between the microcontroller and the computer


} //end setup



void loop()
{
  getGPS(); // get some data to work with

  Serial.print(flon, 6);
  Serial.print(",");
  Serial.print(flat, 6);
  Serial.print(",");

  if (brain.update()) {

    Serial.println("brain updated");

    //Serial.println(brain.readErrors());
    Serial.println(brain.readCSV());
  }

} //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); // Use this line of code to get the time in hhmmsscc and date in ddmmyy formats

      gps.f_get_position(&flat, &flon, &age);  // Use this line of code to get the latitude and longitude values with a decimal point eg 54.117769


    } //end newData
  }

} //end getGPS


As per original post the GPS coordinates are printed out okay (-x.xxxxxx,xx.xxxxxx,-x.xxxxxx,xx.xxxxxx,-x.xxxxxx,xx.xxxxxx, ...   format), but I'm not getting any of the brain data or the Serial.println("brain updated"); debugging statement and I'm confused as to why that section of the code isn't being executed.

That if statement is lifted from this example that comes with the library. That runs fine by itself, and prints new data every second or so - about the same as the GPS data.

I don't have the programming training to know what sort of thing might be causing the problem, but the feeling based on my limited experience is that it's a similar situation to what I've encountered before that turned out to be the port.listen(), hence starting with the question of is there an equivalent for this situation?


8  Using Arduino / Programming Questions / Re: TwoPortRX ...but with multiple software serial and a hardware serial on: December 03, 2013, 04:06:57 am
Hi Robin2,

You've found the abridged bit smiley

The gpsModule.listen() bit itself is like in this example for TwoPortReceive with SoftwareSerial. When I start to add in other components communicating with the arduino over SoftwareSerial, each will need to be prefaced with a listen()

Quote
In order to listen on a software port, you call port.listen().  When using two software serial ports, you have to switch ports  by listen()ing on each one in turn.

Below it,
Code:
// TinyGPS code here
is a placeholder for TinyGPS library functions that get the data, assign them to variables and print them out over serial etc. They're there in the code I'm running, but I snipped them out to make my post more concise.
9  Using Arduino / Programming Questions / Re: TwoPortRX ...but with multiple software serial and a hardware serial on: December 03, 2013, 03:22:17 am
Hi PaulS,

During use (not uploading a sketch) the microcontroller's listening to the brain thing, but not talking to it. It can therefore talk to the PC, but not listen.

From the brain library readme:

Quote
Note that the connection to the Neurosky headset is half-duplex — it will use up the RX pin on your Arduino, but you will still be able to send data back to a PC via USB. (Although in this case you won't be able to send data to the Arduino from the PC.)

Hope that clarifies.

10  Using Arduino / Programming Questions / TwoPortRX ...but with multiple software serial and a hardware serial on: December 02, 2013, 06:21:45 pm
I'm hoping to build a system that logs brain wave and GPS data to an Openlog and ideally also shows some of the output on an SPI OLED display.

The brain wave set-up is as detailed here: http://frontiernerds.com/brain-hack
A mindflex toy with wires soldered onto ground and TX points on its circuitboard and then connected to the arduino (I'm using a nano 328) via ground and the RX pin (removed during programming).

The brain library that parses the raw data coming out of the toy is available in 2 versions: hardware serial (for the set-up described above) and also a software serial version (which I've been unable to get to work - error messages relating to the library when I try and compile the example).

I can get the hardware serial version of the library working and parsing the data to a csv style output on the serial monitor. But I'm struggling to combine this in a sketch with the GPS module.

Here's a slightly abridged version of the code:

Code:
#include <SoftwareSerial.h> //includes the software serial library
#include <TinyGPS.h>  // includes the TinyGPS library
#include <Brain.h>


TinyGPS gps;
SoftwareSerial gpsmodule(3, 4);  
Brain brain(Serial);


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



void setup()  
{
  Serial.begin(9600);  // serial communication between the microcontroller and the computer
  gpsmodule.begin(4800); // serial communication between the gps module and the microcontroller

} //end setup



void loop()
{
  getGPS(); // get some data to work with


// prints GPS coordinates to serial monitor
  Serial.print(flon, 6);Serial.print(","); Serial.print(flat, 6); Serial.print(",");

  
if (brain.update()) {
  
   Serial.println("brain updated");

   //Serial.println(brain.readErrors());
Serial.println(brain.readCSV());
}

} //end loop.

void getGPS(){
gpsmodule.listen();
  
// TinyGPS code here

} //end getGPS


This will give me the GPS coordinates as expected, but none of the brain data or the  Serial.println("brain updated");

Why might that part of the code not be executing?

I know that if I'm using two software serial instances I need to put  
Code:
[i]name[/i].listen();
before I start working with each data stream, is there something similar I need to do before
Code:
if (brain.update()) {
in order to switch which one the arduino is listening to?

Thanks

11  Using Arduino / Programming Questions / Re: 2 instances of Metro causing problems with ethernet shield web server on: November 25, 2013, 11:12:08 am
Okay, back online after moving to Mega!

Thanks for your help, PaulS.
12  Using Arduino / Programming Questions / Re: 2 instances of Metro causing problems with ethernet shield web server on: November 25, 2013, 07:43:10 am
*nods*
I was wondering about that.

I've just soldered some headers onto an old Seeduino Mega I've got and will experiment with transferring the shield/code onto that later today.

Thanks for the link to the page for getting values for available memory - I wasn't aware of those.
13  Using Arduino / Programming Questions / Re: 2 instances of Metro causing problems with ethernet shield web server on: November 24, 2013, 06:22:59 pm
I've since tried the Repeating Timer using elapsedMillis approach from http://www.forward.com.au/pfod/ArduinoProgramming/TimingDelaysInArduino.html and had the same problem with the webpage not showing if I had two instances of elapsedMillis.

Moving away from libraries, the top of my sketch now looks like

Code:
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

float minTemp = 13.5;

unsigned long ReadTempInterval = 3000;
unsigned long previousReadTempMillis =0;
unsigned long HeaterSwitchInterval = 30000;
unsigned long previousHeaterSwitchMillis =0;

And the relevant part of void loop():

Code:
  unsigned long currentReadTempMillis = millis();
 
  if(currentReadTempMillis - previousReadTempMillis> ReadTempInterval) {
    // save the last time
    previousReadTempMillis = currentReadTempMillis;
    // do stuff here each interval (interval -- an unsigned long)

 getTemp();
    Serial.print(temperatureC);     
    Serial.println("");

    // subtract the last reading:
    total= total - readings[index];         
    // read from the sensor: 
    readings[index] = temperatureC;
    // add the reading to the total:
    total= total + readings[index];       
    // advance to the next position in the array: 
    index = index + 1;                   

    // if we're at the end of the array...
    if (index >= numReadings) {             
      // ...wrap around to the beginning:
      index = 0;   
    }                       

    // calculate the average:
    average = total / numReadings;         
    // send it to the computer as ASCII digits

      Serial.print("average temp: ");   

    Serial.println(average);   

 
  }


  unsigned long currentHeaterSwitchMillis = millis();
 
  if(currentHeaterSwitchMillis - previousHeaterSwitchMillis > HeaterSwitchInterval) {
    // save the last time
    previousHeaterSwitchMillis = currentHeaterSwitchMillis;
    // do stuff here each interval (interval -- an unsigned long)
 

    if (average < minTemp){
      simulate_button(1, 3, 1);
      delay(1000);
      Serial.print("ON");
      Serial.print(",");
    }

    else {
      simulate_button(1, 3, 0);
     delay(1000);

      Serial.print("OFF");
      Serial.print(",");

    }
 
  }


Same problem: with both timer sections in there the web page is blank, but the thermostat code seems to run. If I comment either one out then the web page displays and updates as expected.

What could be causing the ethernet part of the sketch to fail? I'm not changing any of the code related to the html or xml generation...
14  Using Arduino / Programming Questions / Re: 2 instances of Metro causing problems with ethernet shield web server on: November 24, 2013, 04:52:34 pm
I don't have much experience with coding and I tend to get in a bit of a tangle with approaches that use millis() to keep track of time intervals (I assume this is the alternative method you're suggesting?).

A friend mentioned the Metro library to me last week and I find it much easier to understand and implement. I understand that this may not be the most elegant or efficient way of doing it, but as my project gains complexity, keeping as much as I can on an intuitive level will be very valuable to me. ...so that's why I'm trying Metro!
15  Using Arduino / Programming Questions / 2 instances of Metro causing problems with ethernet shield web server on: November 24, 2013, 03:46:19 pm

Hi,
I'm slowly putting together an automated heater project, so far mostly by chaining together examples I've found online.

The foundation is this Arduino Ethernet Shield Web Server Tutorial with an UNO R2 and an Ethernet Shield. So far I've also added in temperature sensing (TMP35, output displayed on the webpage), and interfacing with remote control sockets to control mains appliances (fairy lights as a useful debugging tool initially, and now the heater too).

I've hit a problem when I've started adding timers for checking the temperature (every few seconds) and for switching the heater on/off according to the current temperature (at longer intervals).

If I have two instances of Metro, the web page no longer displays - I just get a blank white screen.
Activity in the serial monitor shows that the temperature logging and heater switching is still taking place thought.

I'm new to AJAX and working with the ethernet shield, can anyone explain to me what's causing this problem, please? Could there be some sort of conflict between the Metro library and the ethernet shield?
Workarounds also welcomed smiley

I've attached the sketch I'm using, rather than posting it in-thread, as it's over the character limit...
Pages: [1] 2 3