Hi,
I need to know how long an input signal, captured through an opto coupler on pin A8 was active. This will be part of a bigger telemetry project, for which a GPS will be used as well. Since I already have the GPS, I thought I’d use it as a real time clock as well. As such, I’m capturing the exact time when the input signal on pin A8 is detected, and when it’s lost, but can’t figure out how to get the time difference between the two values.
Here’s the sketch, modified from the original TinyGPS library:
#include <TinyGPS.h>
#include <LiquidCrystal.h>
/* This sample code demonstrates the normal use of a TinyGPS object.
It requires the use of SoftwareSerial, and assumes that you have a
4800-baud serial GPS device hooked up on (rx1, or 2, or 3) and (tx1, or 2, or 3).
*/
TinyGPS gps;
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
void printFloat(double f, int digits = 2);
String SD_date_time = "invalid";
//int run_time = 0;
int start_counter = 0;
int optoPin = A8;
static void gpsdump(TinyGPS &gps);
static bool feedgps();
static void print_date(TinyGPS &gps);
int current_time_set = 0;
int current_time = 0;
int secs=0;
int minutes=0;
int hours=0;
int run_second=0;
int run_minute=0;
int run_hour=0;
int time_stop=0;
int time_diff=0;
unsigned long age, date, time, chars;
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long start_second = 0;
void setup()
{
lcd.begin(16, 2);
//Print a message to the LCD.
lcd.print("GPS Data Logger");
pinMode(optoPin, INPUT);
digitalWrite(optoPin, HIGH);
Serial.begin(115200); // Be sure to set the serial monitor to the same spoeed
Serial.println("Testing Time lapse.....");
Serial3.begin(9600); //(************This is important is the baud rates don't match nothing works******************)
}
void loop()
{
bool newdata = false;
unsigned long start = millis();
// Every second we print an update
while (millis() - start < 1000)
{
if (feedgps())
newdata = true;
}
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
second++;
if (second==60){
second=0;
minute++;
};
if (minute==60){
minute=0;
hour++;
}
if(digitalRead(optoPin) == LOW)
{
if(current_time_set == 0)
{
current_time_set = 1;
current_time = current_time + second;
}
if (current_time_set == 1)
{
char time_start[32];
if(start_counter == 0)
{
sprintf(time_start, "%02d%02d%02d ", hour, minute, second);
start_counter = 1;
Serial.print("Time started: ");
Serial.println(time_start);
Serial.println("------------------");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Start: ");
lcd.print(time_start);
}
}
}
else {
char time_stop[32];
char time_start[32];
if(start_counter == 1)
{
sprintf(time_stop, "%02d%02d%02d ", hour, minute, second);
start_counter = 0;
Serial.print("Current Time: ");
Serial.print(hour);
Serial.print(":");
Serial.print(minute);
Serial.print(":");
Serial.println(second);
time_diff = time_start - time_stop;
Serial.println(time_diff);
lcd.setCursor(0,1);
lcd.print("Stop: ");
lcd.print(time_stop );
Serial.print("Time stopped: ");
Serial.println(time_stop);
Serial.println("------------------");
}
}
}
static void gpsdump(TinyGPS &gps)
{
print_date(gps);
}
static void print_date(TinyGPS &gps)
{
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long age;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
if (age == TinyGPS::GPS_INVALID_AGE)
Serial.print("******* ******* ");
else
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
month, day, year, hour, minute, second);
Serial.print(sz);
}
// print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
feedgps();
}
static bool feedgps()
{
while (Serial3.available())
{
if (gps.encode(Serial3 .read()))
return true;
}
return false;
}
static void print_time(TinyGPS &gps)
{
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long age;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
if (age == TinyGPS::GPS_INVALID_AGE)
{
//Serial.print("******* ******* ");
SD_date_time = "invalid";
}
else
{
char sz[32];
sprintf(sz, "%02d%02d%02d ",
hour, minute, second);
Serial.print(sz);
SD_date_time = sz;
}
//print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
feedgps();
}
static void print_str(const char *str, int len)
{
int slen = strlen(str);
for (int i=0; i<len; ++i)
//Serial.print(i<slen ? str[i] : ' ');
feedgps();
}
The problem is, that the time difference between “on” and “off” is always “32”, no matter how long the signal was active on the opto coupler
Time started: 214345
Current Time: 21:43:48
32
Time stopped: 214348Time started: 214353
Current Time: 21:44:5
32
Time stopped: 214405
The hardward includes the following:
Arduino MEGA 2560
a bunch of 4N25 opto couplers
a GTOP GTPA013 / P6H GPS module (connected to Serial2)
and an iTeadstudio Sim900 GSM Shield (for transmitting data to a remote database server
There’s a 16x4 LCD attached to it as well, which I purely use to see if everything works as expected, while in the car without my laptop but it will be removed later on.
Any help would be appreciated.