Ì am building speedometer with NEO-6M and have some problems with my code. The scenario is that I send some data from our Arduino to another device via a TX pin with a delay of 50 ms. This delay of 50 ms needs to be maintained and should not exceed. The issue with GPS data, which arrives from the device, is that it is received every 1 second. This issue will cause the 50ms delay to be longer and exceed to 1050 ms. In my last code change delay is not maintained on 50 ms, it is all 1000 ms. Not sure what I did wrong. My code is
original
#include <AD9850.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
static const int RXPin = 0, TXPin = 1;
static const uint32_t GPSBaud = 9600;
const int W_CLK_PIN = 8;
const int FQ_UD_PIN = 9;
const int DATA_PIN = 10;
const int RESET_PIN = 11;
double freq = 1;
double trimFreq = 124999500;
int phase = 0;
unsigned int speed = 0;
TinyGPSPlus gps;
SoftwareSerial ss(RXPin, TXPin);
void setup()
{
Serial.begin(115200);
ss.begin(GPSBaud);
DDS.begin(W_CLK_PIN, FQ_UD_PIN, DATA_PIN, RESET_PIN);
DDS.calibrate(trimFreq);
}
void loop()
{
// Dispatch incoming characters
while (ss.available() > 0)
gps.encode(ss.read());
{
digitalWrite(outPin, HIGH); // sets the pin on
delayMicroseconds(50); // pauses for 50 microseconds
digitalWrite(outPin, LOW); // sets the pin off
delayMicroseconds(50); // pauses for 50 microseconds
}
if (gps.speed.isUpdated())
{
Serial.print(F("SPEED Fix Age="));
Serial.print(gps.speed.age());
Serial.print(F("ms Raw="));
Serial.print(gps.speed.value());
Serial.print(F(" km/h="));
Serial.println(gps.speed.kmph());
speed = gps.speed.kmph();
//calculate the frequency with map()
freq = map(speed, 0, 100, 0, 160);
DDS.setfreq(freq, phase);
}
}
revised
(replying to https://arduino.stackexchange.com/a/45657) Thank you for help. Is it possible to put GPS speed in this code? https://pastebin.com/LdT7UHBQ I used your code and it works but my boss demands that we modify his code and make it maintain delay with it.
long prevTxTime = 0;
const int delayTime = 51;
void setup()
{
Serial1.begin(115200);
Serial2.begin(115200);
}
void loop()
{
int b, chunk = 0;
if (Serial2.available())
{
b = Serial2.read();
chunk = b >> 6;
if (chunk == syncc)
{
bytes[syncc] = b & 63;
syncc++;
if (syncc == 4)
{
syncc = 0;
tach = bytes[0] + (bytes[1] << 6);
velocityfreq = bytes[2] + (bytes[3] < delayTime)
{
//speed from gps would most likely need to be set here as "speedfromgps"
SendData();
prevTxTime = millis();
}
}
void SendData()
{
sprintf(charToSend, "%d|%d|%d|%d|%d\n\r",
data1, data2, data3, data4, speedfromgps);
Serial1.print(charToSend);
}