Created
April 21, 2017 15:44
-
-
Save andrew-schofield/af27df303ebfd7af66daace682832185 to your computer and use it in GitHub Desktop.
Example code for arduino data pump
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Kart_timing_data_pump | |
Writes out fake kart timing data over the serial port. | |
Copyright 2013 Andrew Schofield | |
*/ | |
// The minimum and maximum lap times that can be generated | |
int const MIN_LAP_TIME = 3; | |
int const MAX_LAP_TIME = 5; | |
// The number of karts to simulate | |
int const KART_COUNT = 5; | |
// Timing variables necessary to generate data for each kart without using threads | |
unsigned long previousMillis[KART_COUNT]; | |
unsigned long intervalMillis[KART_COUNT]; | |
// The time since the arduino was last reset | |
unsigned long currentMillis = 0; | |
// Configure everything | |
void setup() { | |
Serial.begin(9600); | |
for( int i = 0; i < KART_COUNT; i++) { | |
previousMillis[i] = 0; | |
intervalMillis[i] = 0; | |
} | |
} | |
void loop() { | |
currentMillis = millis(); | |
for( int i = 0; i < KART_COUNT; i++) { | |
if(currentMillis - previousMillis[i] > intervalMillis[i]) { | |
printKartData(i); | |
previousMillis[i] = currentMillis; | |
intervalMillis[i] = newLapTime(); | |
} | |
} | |
} | |
// Assemble the output string sent over the serial port | |
void printKartData(int kartID) { | |
char outputLine[20]; | |
char time[11]; | |
char kart[3]; | |
sprintf(time, "%010lu", millis()); | |
sprintf(kart, "%02x", kartID); | |
strcat(outputLine, kart); | |
strcat(outputLine, ":"); | |
strcat(outputLine, time); | |
strcat(outputLine, ":"); | |
strcat(outputLine, "25"); | |
Serial.println(outputLine); | |
} | |
// Generate a new lap time | |
unsigned long newLapTime() { | |
return random(MIN_LAP_TIME * 1000,MAX_LAP_TIME * 1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment