Hi guys i am currently building a project where if a speed of 5MPH is reached a 12v siren and a 12v Light will flash. i have reduced the speed from 5mph down to 0.01mph to test the siren and light and this works well however my issue is that the GPS module will not give me the correct speed when moving, what i have found is that if i unplug the unit from the USB and plug it back in again it gives the correct speed. Below is the code
#include <TinyGPS++.h>
#include <AltSoftSerial.h>
TinyGPSPlus gps;
AltSoftSerial gpsSerial;
const int sirenPin = 13; // Pin controlling the siren relay (IN2)
const int ledPin = 7; // Pin controlling the LED relay (IN1)
float speedThreshold = 5.00; // Speed threshold to activate siren and LED
void setup() {
Serial.begin(9600);
gpsSerial.begin(9600);
pinMode(sirenPin, OUTPUT); // Pin controlling the siren relay
pinMode(ledPin, OUTPUT); // Pin controlling the LED relay
digitalWrite(sirenPin, LOW); // Initially turn off relay (siren off)
digitalWrite(ledPin, LOW); // Initially turn off relay (LED off)
Serial.println("Waiting for GPS fix...");
}
void loop() {
while (gpsSerial.available() > 0) {
gps.encode(gpsSerial.read());
if (gps.location.isValid() && gps.speed.isValid()) {
float speed = gps.speed.mph();
if (speed >= speedThreshold) {
digitalWrite(sirenPin, HIGH); // Activate the siren relay
digitalWrite(ledPin, HIGH); // Activate the LED relay
} else {
digitalWrite(sirenPin, LOW); // Deactivate the siren relay
digitalWrite(ledPin, LOW); // Deactivate the LED relay
}
Serial.print("Speed mph: ");
Serial.println(speed);
} else {
Serial.println("Waiting for valid GPS fix and speed data...");
}
}
}