I'm trying to use a GPS and the Adafruit thermal printer module with an Arduino Nano in the same project (both use baud 9600) and was wondering if I could do something like in the code below? Basically, in the example I made, I'm getting the time from the GPS and when it's on the minute, it will call the printTime()
method.
What I'm wondering is if it's okay to do what I did in my disableGPS
, enablePrinter
methods by ending the serial connection for GPS and starting it for my printer? I feel like this should work, but I don't fully understand serial connections.
If this doesn't work, is there an alternative I could do? I never need to read from GPS and write to the printer at the same time in code.
#include <Adafruit_Thermal.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#define PrinterTXPin 6 // Arduino transmit YELLOW WIRE labeled RX on printer
#define PrinterRXPin 5 // Arduino receive GREEN WIRE labeled TX on printer
static const uint32_t baud = 9600;
//Printer
SoftwareSerial PrinterSerial(PrinterRXPin, PrinterTXPin);
Adafruit_Thermal printer(&PrinterSerial);
//we only want to print on the minute
boolean havePrinted = false;
//GPS
TinyGPSPlus gps;
TinyGPSDate lastDate;
void setup() {
enableGPS();
}
void loop()
{
if(gps.time.isValid() && checkDate(gps.time)){
printTime(gps.time);
}
}
boolean checkDate(TinyGPSTime theTime)
{
if(theTime.second() == 0 && !havePrinted)
{
havePrinted = true;
return true;
}
if(theTime.second() == 1){
havePrinted = false;
}
}
void printTime(TinyGPSTime theTime)
{
//Disable since we can only have 1 serial connection per baud
disableGPS();
enablePrinter();
printer.doubleHeightOn();
printer.println(theTime.hour() + ":" + theTime.minute());
printer.feed(1);
disablePrinter();
enableGPS();
}
void enablePrinter()
{
PrinterSerial.begin();
printer.begin();
printer.wake();
}
void disablePrinter()
{
printer.sleep();
PrinterSerial.end();
}
void disableGPS()
{
Serial.end();
}
void enableGPS()
{
Serial.begin(baud);
}
Serial.end()
does not take any parameters. SoSerial.end(baud)
is wrongSerial
is done through the serial pins on the nano 0 and 1 whileSoftwareSerial
can use any digital pins (In this case PrinterTXPin and PrinterRXPin). chrisl - noted.