I wrote this application to log vehicle data to a SQL database so that I can analyze my driving habits. The arduino makes a few measurements, and sends them across the USB serial connection in a comma separated, semi-human readable format. I have a VB app that sits on the computer and either logs it to a sql database or caches it in a text file when the sql server is unavailable.
Everything is working great on my development computer - I have another micro generating a sample waveform and it measures it perfectly. But, when I plug it into the laptop that I had intended to use on the road, a FEW of the values are corrupt, but the formatting and all of the delimiters are fine. I know it's not the VB end of things - I can open it in hyperterminal, and it's corrupt there as well. I've tried changing the baud rate, etc, but the results are the same - fine on the desktop, first two values are corrupt on the laptop. And even the WAY that they are corrupt is consistent - should be sending a value of 300, and it will alternate between a few different values - 0.00, 2500000 and 1300000 for example. Not just any random values, particular ones. It's as if the buffer is getting full on the laptop and affecting the calculations on the arduino, but why on one, and why does the baud rate have no effect? Any ideas???
Good Output:
S39928,300.61,0.00,436,5,322,7,413,2,
S59900,300.61,0.00,439,5,355,7,422,3,
Bad Output:
S86688,0.00,0.00,436,5,322,7,413,2,
S91820,1875000.00,0.00,439,5,355,7,422,3,
Code is a little messy, but here it is:
unsigned long InjOpenTime;
unsigned long InjOpenLast;
unsigned long InjCloseTime;
unsigned long InjOpenTotal;
//unsigned long InjCloseTotal;
unsigned long SampleStartTime;
unsigned long SampleStartLast;
unsigned long SampleIntervalLength;
float SampleMillis;
float SampleMinutes;
float SampleHours;
float SampleDistance;
float SampleDistanceOld;
float DistanceChange;
long InjLoopTime;
long InjSampleTime;
long InjSampleLast;
long InjDuty;
long InjCycleOpenTotal;
float RPMs;
long VssPulseCount;
long VssMicro;
long VssLastMicro;
long VssTime;
float MPH;
int distance;
int distanceOld;
int InjCloseCount;
int InjCloseCountOld;
int DeltaInjCloseCount;
boolean VssState;
boolean VssLastState;
boolean Test;
void setup() {
Serial.begin(57600);
pinMode(2, INPUT);
pinMode(3, INPUT);
pinMode(15, INPUT);
attachInterrupt(0, InjOpen, RISING); // Pin 2 interrupt
attachInterrupt(1, InjClose, FALLING); // Pin 3 interrupt
}
void loop() {
SampleStartTime = micros();
SampleDistance = distance;
InjSampleTime = InjOpenTotal;
InjDuty = InjSampleTime - InjSampleLast;
InjSampleLast = InjSampleTime;
SampleIntervalLength = SampleStartTime - SampleStartLast;
SampleStartLast = SampleStartTime;
DistanceChange = SampleDistance-SampleDistanceOld;
SampleDistanceOld = SampleDistance;
SampleMillis = SampleIntervalLength / 1000;
SampleMinutes = SampleIntervalLength / 60000;
SampleHours = SampleMinutes/60;
MPH = DistanceChange*.66 * 5280/SampleIntervalLength;
DeltaInjCloseCount = InjCloseCount - InjCloseCountOld;
if (DeltaInjCloseCount < 2)
{
// Not Running..
InjLoopTime = 400002;
}
InjCloseCountOld = InjCloseCount;
if (InjLoopTime > 400000)
{
RPMs = 0;
InjLoopTime = 400000;
}
else
{
RPMs = InjLoopTime;
RPMs = 1/RPMs * 60000000;
}
Serial.print("S");
Serial.print(InjDuty); // Inj pW
Serial.print(",");
Serial.print(RPMs); // RPM
Serial.print(",");
Serial.print(MPH); // MPH
Serial.print(",");
Serial.print(analogRead(5)); // AccelX
Serial.print(",");
Serial.print("5"); // AccelY
Serial.print(",");
Serial.print(analogRead(4)); // AccelZ
Serial.print(",");
Serial.print("7"); // CoolantTemp
Serial.print(",");
Serial.print(analogRead(5)); // AirTemp
Serial.print(",");
Serial.print(DeltaInjCloseCount); // InjEvents
Serial.println(",");
delay(1000); // Sampling period
}
void InjOpen()
{
InjOpenTime= micros();
}
void InjClose()
{
InjCloseTime = micros();
InjCycleOpenTotal = InjCloseTime - InjOpenTime;
InjOpenTotal += InjCycleOpenTotal;
InjLoopTime = InjOpenTime - InjOpenLast;
InjOpenLast = InjOpenTime;
//VssState = digitalRead(15);
//if(VssState != VssLastState)
//{
// distance++; // We just went .66 feet
// VssLastState = VssState;
//}
InjCloseCount +=1;
}
Moderator edit:
</mark> <mark>[code]</mark> <mark>
</mark> <mark>[/code]</mark> <mark>
tags added.