Hi,
I installed a third led light brake in my motorbike. It works along with the other brake lights.
The Arduino Uno is programmed for:
When I'm above 30mph, if I press the brake, the 3rd LED flashes 20 times 50 milliseconds(Relay open circuit), it catches the attention of other drivers. After that, delay 5 seconds to make sure I'm below 30mph and doesn't trigger the strobe again.
There's a small problem. The monitor is writing duplicated lines. So the loop is always running twice even if speed is below 30mph.
There's no way I can make the monitor writing single lines instead of duplicates. (see monitor capture attached image)
Any ideas how to do that? Cheers
(PS: I'll need to keep month and hour for another function: Daytime running leds connected with a MOSFET.)
#define Relay 8
#include <SoftwareSerial.h>
#include <TinyGPS.h>
SoftwareSerial mySerial(10, 11);
TinyGPS gps;
void gpsdump(TinyGPS &gps);
void printFloat(double f, int digits = 2);
int sensorPin = A0; // select the input pin for the brake 5v signal
int sensorValue = 0;
int strobeMs = 50; // 50ms is 20times per second
int PauseStrobe = 5000; //1000ms is 1second
int NumBlink = 30; //This is the times it blinks
void setup()
{
//Setup all the Arduino Pins
pinMode(Relay, OUTPUT);
//Turn OFF any power to the Relay channels
digitalWrite(Relay,HIGH);
// Oploen serial communications and wait for port to open:
Serial.begin(9600);
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() // run over and over
{
bool newdata = false;
unsigned long start = millis();
// Every 5 seconds we print an update
while (millis() - start < 5000)
{
if (mySerial.available())
{
char c = mySerial.read();
//Serial.print(c); // uncomment to see raw GPS data
if (gps.encode(c))
{
newdata = true;
break; // uncomment to print new data immediately!
}
}
}
if (newdata)
{
gpsdump(gps);
}
}
void gpsdump(TinyGPS &gps)
{
unsigned long age, date, time, chars;
int year;
byte month, day, hour, minute, second, hundredths;
unsigned short sentences, failed;
gps.get_datetime(&date, &time, &age);
Serial.print(" Time(hhmm): ");
Serial.print(time);
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
Serial.print(" Date: "); Serial.print(static_cast<int>(month));
Serial.print(" (kmph): ");
printFloat(gps.f_speed_kmph()); Serial.println();
}
void printFloat(double number, int digits)
{
// Handle negative numbers
if (number < 0.0)
{
Serial.print('-');
number = -number;
}
// Round correctly so that print(1.999, 2) prints as "2.00"
double rounding = 0.5;
for (uint8_t i=0; i<digits; ++i)
rounding /= 10.0;
number += rounding;
// Extract the integer part of the number and print it
unsigned long int_part = (unsigned long)number;
double remainder = number - (double)int_part;
Serial.print(int_part);
// Print the decimal point, but only if there are digits beyond
if (digits > 0)
Serial.print(".");
// Extract digits from the remainder one at a time
while (digits-- > 0)
{
remainder *= 10.0;
int toPrint = int(remainder);
Serial.print(toPrint);
remainder -= toPrint;
if ((gps.f_speed_kmph()>=0.60 ) && (sensorValue = analogRead(sensorPin)))
{
for (int j=1; j<=NumBlink; j=j+1){
digitalWrite(Relay, HIGH);
delay(strobeMs);
digitalWrite(Relay, LOW);
delay(strobeMs);
}
digitalWrite (Relay, HIGH);
delay(PauseStrobe);
}
}
}
Moderator edit: code tags. Why is this so difficult?