GPS clock almost working right
i have built gps clock plus temperature display, using up501d gps module.
i have display seperate gps unit, , send numbers displayed via cable using virtualwire ( running avoid cable capacitance problems ) waveform @ display end identical gps unit end thats fine.
at switch on, no data sent display until first valid sentance received.
to avoid occaisional spurious data gps being displayed, read minutes , hours every second, , every time minute changes, add minute make " nextminute" , " nexthour" compare following minutes data.
if match, freerunning clock updated, , millis timing reset zero.
initially lose minute @ switch on, takes while gps lock on anyway @ power on.
if gps loses lock, free running clock carries on updating display every minute using 16 mhz xtal ( accurate within few seconds day, havn't bothered rtc , gps stays locked on virtually time )
at display end, have micro receiving vw data, , sending serially display digits latched.
to avoid showing wrong time day if data stops, have timer blanks display after 130 seconds of no data received.
my problem once in blue moon, display blanks , shows lack of data.
the led stays on ( not flashing ) shows valid sentance being received... , if did lose lock, freerun clock should transmitting every minute anyway..
unfortunately both times has happened, didnt leave on see if come on after minute, , hasn't happened in last day or two. waiting scope ready, watched pot never boils ....
can see in code below, if have stupid mistakes, or situations make hang ?
i have display seperate gps unit, , send numbers displayed via cable using virtualwire ( running avoid cable capacitance problems ) waveform @ display end identical gps unit end thats fine.
at switch on, no data sent display until first valid sentance received.
to avoid occaisional spurious data gps being displayed, read minutes , hours every second, , every time minute changes, add minute make " nextminute" , " nexthour" compare following minutes data.
if match, freerunning clock updated, , millis timing reset zero.
initially lose minute @ switch on, takes while gps lock on anyway @ power on.
if gps loses lock, free running clock carries on updating display every minute using 16 mhz xtal ( accurate within few seconds day, havn't bothered rtc , gps stays locked on virtually time )
at display end, have micro receiving vw data, , sending serially display digits latched.
to avoid showing wrong time day if data stops, have timer blanks display after 130 seconds of no data received.
my problem once in blue moon, display blanks , shows lack of data.
the led stays on ( not flashing ) shows valid sentance being received... , if did lose lock, freerun clock should transmitting every minute anyway..
unfortunately both times has happened, didnt leave on see if come on after minute, , hasn't happened in last day or two. waiting scope ready, watched pot never boils ....
can see in code below, if have stupid mistakes, or situations make hang ?
code: [select]
// gps clock bottom end apr 7
#include <newsoftserial.h>
#include <tinygps.h>
#include <virtualwire.h>
#define ledpin 8 // flashes led
int sensorpin = 3; // analog pin 0 reads tmp35 output
unsigned long clockmillis = 0;
unsigned long rxmillis = 0;
unsigned long interval = 60000;
unsigned long currentmillis;
unsigned long currentclockmillis;
int clockmins;
int clockhours;
#define rxpin 14 //
#define txpin 15 //
#define gpsbaud 9600 // baud rate of our up501d gps module. change gps module if different
char msg [6];
tinygps gps;
newsoftserial uart_gps(rxpin, txpin);
void getgps(tinygps &gps);
int temptens;
int tempunits;
int hourtens;
int hourunits;
int mintens;
int minunits;
int rxhours;
int rxmins;
int ledstate = low;
long previousmillis = 0;
long flashinterval = 1200;
int searching;
// ************************************* set ***********************************************
void setup() {
uart_gps.begin(gpsbaud); // setup sketch data output speed of gps module
// serial.begin(115200);
interval = 60000;
pinmode (ledpin, output );
digitalwrite(ledpin, low);
searching = high;
vw_set_tx_pin(16); // tx module connections , speed
vw_set_ptt_pin(10); // powers tx , led not used project
vw_setup(1000);
} // end of setup
// ===================================get gps==========================
void getgps(tinygps &gps) // getgps function values want.
{
int year,a,t;
byte month, day, hour, minute, second, hundredths;
gps.crack_datetime(&year,&month,&day,&hour,&minute,&second,&hundredths);
hour=hour+2; // zone gmt +2
if (hour>23) {
hour = hour - 24;
}
int nexthour = rxhours;
int nextmins = (rxmins + 1) ;
if (nextmins > 59 ){
nextmins = 0;
( nexthour + 1 );
if ( nexthour > 23 ){
nexthour = 0;
}
}
rxhours = ( int ) hour;
rxmins = (int) minute;
if ( rxmins == nextmins ){
if (rxhours == nexthour) { // if time expected prev read
searching = low;
clockmins = rxmins;
clockhours = rxhours;
clockmillis = millis () ; // reset freerun clock counter millis
showtime ();
}
}
} // end of gps
//********************************************************************************************
void loop()
{
//******************************* flashing led *************************************************
currentmillis = millis(); // flash led til gps aquires
if(currentmillis - previousmillis > flashinterval) {
previousmillis = currentmillis;
if (ledstate == low)
{
ledstate = high;
}
else
{
ledstate = low;
}
}
digitalwrite ( ledpin, ledstate );
//********************************now check gps receiver ************************************************
if(uart_gps.available()) // while there data on rx pin...
{
int c = uart_gps.read(); // load data variable...
if(gps.encode(c)) // if there new valid sentence...
{
ledstate = high; // leaves led on when valid sentance aquired resetting ledflash timer
previousmillis = currentmillis;
getgps(gps); // grab data,
}// end if gps encode
} // end if uuart avail
// ------------------------------------------------ free running backup clock ------------------
currentclockmillis = millis();
if(currentclockmillis - clockmillis > interval) { // if new minute
clockmillis = currentclockmillis;
clockmins ++;
if ( clockmins >59 ) {
clockmins = 0;
clockhours ++;
if (clockhours >23 ) {
clockhours = 0 ;
}
}
if ( searching == low ){
showtime ();
}
}// end of if new minute
} // end of loop
//*********************************gen display numbers , send display unit ****************************************
void showtime () {
checktemp ();
int t,a;
byte hour, minute;
if (clockhours<10)
{
hourtens = 0;
hourunits = clockhours; // serial.print("less ten hours == "); serial.println(hourunits);
}
if (clockhours>=10) {
t=clockhours/10;
a=int(t);
hourtens = a; // serial.print(" hourstens = "); serial.println(hourtens);
t=clockhours%10;
hourunits = t; // serial.print(" hoursunits == "); serial.println(hourunits);
}
if (clockmins<10)
{
mintens = 0;
minunits = clockmins; // serial.print("less ten mins == "); serial.println(minunits);
}
if (clockmins>=10)
{
t=clockmins/10;
a=int(t);
mintens = a; //serial.print(" mintens >10 ="); serial.println(mintens);
t=clockmins%10;
minunits = t; // serial.print(" minunits>10 == "); serial.println(minunits);
}
msg [0] = hourtens;
msg [1] = hourunits;
msg [2] = mintens;
msg [3] = minunits;
msg [4] = temptens;
msg [5] = tempunits;
vw_send((uint8_t *)msg, 6); // send character out
vw_wait_tx(); // wait until whole message gone
}
//================================================checking temperature=======================
void checktemp () {
int reading = analogread(sensorpin);
float voltage = reading * 5.0;
voltage /= 1024.0;
int temperaturec = (voltage ) * 100 ;
int x,y;
if (temperaturec<10)
{
temptens = 0;
tempunits = temperaturec;
}
if (temperaturec>=10) {
x=temperaturec/10;
y=int(x);
temptens = y; //
x=temperaturec%10;
tempunits = x; //
}
} // end checktemp
// =================================================================================
ah ! have noticed defined
long previousmillis = 0;
instead of normal unsigned long, perhaps overrunning when unit had been on long time ( gets set millis () @ 1 point ) ??
long previousmillis = 0;
instead of normal unsigned long, perhaps overrunning when unit had been on long time ( gets set millis () @ 1 point ) ??
Arduino Forum > Using Arduino > Project Guidance > GPS clock almost working right
arduino
Comments
Post a Comment