Thanks! The car is awesome! I’m still editing a video or two, should have them ready soon.
Pretty much the whole build is on my Instagram @stowellandrew
Anyway, I got it to record a few PIDs and save them to an SD card, but I’m having formatting issues. Here is my code and I’m trying to get CSV file I can import into Excel.
// Including Arduino_Due_SD_HSCMI library also creates SD object (MassStorage class)
#include <Arduino_Due_SD_HSMCI.h> // This creates the object SD#include <Arduino.h>
#include <pins_arduino.h>
#include <stdint.h>
#include "OBD9141.h"
// The RX pin of the Serial1 connected to 9141 K RX of tranciever.
#define RX_PIN LIN_KRX
// The Tx pin of the Serial1 connected to 9141 K TX of tranciever.
#define TX_PIN LIN_KTX
// The Enable pin connected to 9141 SLP pin of tranciever.
#define EN_PIN LIN_KSLP
#define OBD9141_DEBUG
OBD9141 obd;
// We need to create FileStore object, we will be using it to open/create file and to close file.
FileStore FS;
// Variables
unsigned int i=0;
//int runtime = 0;
int VSS = 0;
int RPM = 0;
float ECT = 0;
float IAT = 0;
//int airtemp = 0;
//int oiltemp = 0;
void setup() {
// initialize digital pin DS2 as an output. RED LED
pinMode(DS2, OUTPUT);
// initialize digital pin DS3 as an output. YELLOW LED
pinMode(DS3, OUTPUT);
// initialize digital pin DS4 as an output. YELLOW LED
pinMode(DS4, OUTPUT);
// initialize digital pin DS5 as an output. YELLOW LED
pinMode(DS5, OUTPUT);
// initialize digital pin DS6 as an output. GREEN LED
pinMode(DS6, OUTPUT);
SerialUSB.begin(115200);
delay(2000);
pinMode(EN_PIN, OUTPUT);
digitalWrite(EN_PIN, HIGH);
pinMode(PS_J1850_9141, OUTPUT);
digitalWrite(PS_J1850_9141, HIGH);
obd.begin(Serial1, RX_PIN, TX_PIN);
// Check if there is card inserted
SD.Init(); // Initialization of HSCMI protocol and SD socket switch GPIO (to adjust pin number go to library source file - check Getting Started Guide)
FS.Init(); // Initialization of FileStore object for file manipulation
/* Following code creates file "data.csv" in dir "0:" for logging raw data. Every time pushbutton is pushed, counter increment for 1 and number is logged into "data.csv".
Check Getting Started Guide for Debug options (Debug is enabled by default).
CAUTION!
Code only works if there is no directory or file with the same name. Code first checks if name is alredy used when is needed. */
char message[] = "Line,RPM,MPH,ECT,IAT\n"; // Message at the beginnign of the file
// Create/Open file "data.csv"
FS.CreateNew("0:","data.csv"); // Create new file, if alredy exists it will be overwritten
//FS.GoToEnd(); // Do not need when creating file because new file is opened and position 0
FS.Write(message); // writing message
FS.Close(); // we need to close file to store all the data that was written to it
}
void loop() {
SerialUSB.println("Looping");
// obd.set_port(false);
bool init_success = obd.init();
SerialUSB.print("init_success:");
SerialUSB.println(init_success);
//init_success = true;
// Uncomment this line if you use the simulator to force the init to be
// interpreted as successful. With an actual ECU; be sure that the init is
// succesful before trying to request PID's.
if (init_success) {
bool res = true;
while (res) {
//res = obd.getCurrentPID(0x1F, 2);
//if (res){
//SerialUSB.print("Result 0x0C (Engine Run Time): ");
//SerialUSB.println(obd.readUint16()/4);
//runtime = (obd.readUint16()/4);
//}
res = obd.getCurrentPID(0x0C, 2);
if (res){
SerialUSB.print("Result 0x0C (RPM): ");
SerialUSB.println(obd.readUint16()/4);
RPM = (obd.readUint16()/4);
}
res = obd.getCurrentPID(0x0D, 1);
if (res) {
SerialUSB.print("Result 0x0D (Speed MPH): ");
SerialUSB.println(obd.readUint8());
VSS = (obd.readUint8());
}
res = obd.getCurrentPID(0x05, 1);
if (res){
SerialUSB.print("Result 0x05 (ECT C): ");
ECT = (obd.readUint8()) - 40 ;
SerialUSB.println(ECT);
}
res = obd.getCurrentPID(0x0F, 1);
if (res){
SerialUSB.print("Result 0x05 (IAT C): ");
IAT = (obd.readUint8()) - 40 ;
SerialUSB.println(IAT);
}
//res = obd.getCurrentPID(0x46, 1);
//if (res){
//SerialUSB.print("Result 0x46 (Ambient): ");
//SerialUSB.println(obd.readUint8());
//airtemp = (obd.readUint8());
//}
//res = obd.getCurrentPID(0x5C, 1);
//if (res){
//SerialUSB.print("Result 0x5C (Oil Temp): ");
//SerialUSB.println(obd.readUint8());
//oiltemp = (obd.readUint8());
//}
if (RPM > 4000) { // GREEN and all LED on
digitalWrite(DS2, LOW); // turn the LED on by making the voltage LOW
digitalWrite(DS3, LOW); // turn the LED on by making the voltage LOW
digitalWrite(DS4, LOW); // turn the LED on by making the voltage LOW
digitalWrite(DS5, LOW); // turn the LED on by making the voltage LOW
digitalWrite(DS6, LOW); // turn the LED on by making the voltage LOW
}
else if (RPM > 3000 && RPM <=4000) { // THREE YELLOW and GREEN LED on
digitalWrite(DS2, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(DS3, LOW); // turn the LED on by making the voltage LOW
digitalWrite(DS4, LOW); // turn the LED on by making the voltage LOW
digitalWrite(DS5, LOW); // turn the LED on by making the voltage LOW
digitalWrite(DS6, LOW); // turn the LED on by making the voltage LOW
}
else if (RPM > 2000 && RPM <=3000) { // TWO YELLOW and GREEN LED on
digitalWrite(DS2, HIGH); // turn the LED on by making the voltage LOW
digitalWrite(DS3, HIGH); // turn the LED on by making the voltage LOW
digitalWrite(DS4, LOW); // turn the LED on by making the voltage LOW
digitalWrite(DS5, LOW); // turn the LED off by making the voltage HIGH
digitalWrite(DS6, LOW); // turn the LED off by making the voltage HIGH
}
else if (RPM > 1000 && RPM <=2000) { // YELLOW and RED LED on
digitalWrite(DS2, HIGH); // turn the LED on by making the voltage LOW
digitalWrite(DS3, HIGH); // turn the LED on by making the voltage LOW
digitalWrite(DS4, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(DS5, LOW); // turn the LED off by making the voltage HIGH
digitalWrite(DS6, LOW); // turn the LED off by making the voltage HIGH
}
else if (RPM > 100 && RPM <=1000) { // RED LED on
digitalWrite(DS2, HIGH); // turn the LED on by making the voltage LOW
digitalWrite(DS3, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(DS4, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(DS5, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(DS6, LOW); // turn the LED off by making the voltage HIGH
}
else {
digitalWrite(DS2, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(DS3, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(DS4, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(DS5, HIGH); // turn the LED off by making the voltage HIGH
digitalWrite(DS6, HIGH); // turn the LED off by making the voltage HIGH
}
if (RPM > 100) {
// Increment counter
i++;
// Preapre buffer, write() method is accepting only array of chars, it is easier for us to create a string and then transform it into array of chars
String str = String(i) + "," + String(RPM) + "," + String(VSS) + "," + String(ECT,1) + "," + String(IAT,1); // creates a string
char write_buffer[sizeof(str)]; // Creating array of char in length of our string
str.toCharArray(write_buffer,sizeof(str)); // transform string to array of chars of strings's size
// SerialUSB.print(write_buffer); // We can check what was created, SerialUSB.print(.) uses serial buffer so we can print string or array of chars
// Write data to file.
FS.Open("0:","data.csv",true); // openning file
FS.GoToEnd(); // Search for the end of file and write to it
FS.Write(write_buffer); // write data to file
FS.Write("\n"); // new line
FS.Close(); // to save data in file, we must close the file
}
}
delay(250);
}
VSS = 0;
RPM = 0;
ECT = 0;
IAT = 0;
}
And here is the file I get:
Line,RPM,MPH,ECT,IAT
1,952,0,39.
2,917,0,39.
3,894,0,39.
4,899,0,39.
5,906,0,39.
6,887,0,39.
7,916,0,39.
8,933,0,39.
9,938,0,39.
10,914,0,39
11,895,0,39
12,871,0,39
13,875,0,39
14,941,0,39
15,949,0,39
16,1078,0,3
17,1291,0,3
18,1482,0,3
19,1590,0,3
20,1667,0,3
21,1451,0,3
22,1181,0,3
23,1086,0,3
24,1004,0,3
25,926,0,39
26,890,0,39
27,924,0,39
28,926,0,39
29,915,0,39
30,905,0,39
31,900,0,39
32,953,0,39
33,1310,0,3
34,1582,0,3
35,1738,0,3
36,1691,0,3
37,1407,0,3
38,1247,0,3
39,1100,0,3
40,967,0,38
41,915,0,38
42,885,0,38
43,900,0,38
44,921,0,38
45,898,0,38
46,876,0,38
47,892,0,38
48,912,0,38
49,910,0,38
50,903,0,38
51,918,0,37
52,921,0,38
53,901,0,37
54,913,0,37
55,883,0,37
56,873,0,37
57,902,0,37
58,904,0,37
59,874,0,37
60,874,0,37
61,885,0,37
62,880,0,37
63,878,0,37
64,827,0,37
65,845,0,37
66,893,0,37
67,917,0,37
68,860,0,37
69,868,0,37
70,872,0,37
71,853,0,37
72,817,0,37
73,830,0,37
74,845,0,37
75,829,0,37
76,851,0,37
77,882,0,37
78,846,0,37
79,861,0,37
80,859,0,37
81,862,0,37
82,862,0,37
83,825,0,37
84,811,0,37
85,853,0,37
86,837,0,37
87,826,0,37
88,873,0,37
89,825,0,37
90,849,0,37
91,822,0,38
92,836,0,37
93,812,0,38
94,835,0,38
95,838,0,38
96,859,0,38
97,849,0,38
98,834,0,38
99,833,0,38
100,839,0,3
101,859,0,3
102,824,0,3
103,837,0,3
104,831,0,3
105,849,0,3
106,841,0,3
107,858,0,3
108,845,0,3
109,827,0,3
110,827,0,3
111,802,0,3
112,822,0,3
113,841,0,3
114,836,0,3
115,834,0,3
116,860,0,3
117,813,0,3
118,818,0,3
119,834,0,3
120,830,0,3
121,849,0,3
122,836,0,3
123,827,0,3
124,852,0,3
125,814,0,3
126,809,0,3
127,787,0,3
128,821,0,3
129,828,0,3
130,835,0,3
131,816,0,3
132,786,0,3
133,828,0,3
134,850,0,3
135,828,0,3
136,847,0,3
137,808,0,3
138,804,0,3
139,786,0,3
140,819,0,3
141,831,0,4
142,809,0,4
143,785,0,4
144,787,0,4
145,769,0,4
146,784,0,4
147,781,0,4
148,819,0,4
149,786,0,4
150,766,0,4
151,837,0,4
152,759,0,4
153,802,0,4
154,775,0,4
155,803,0,4
156,818,0,4
157,808,0,4
158,757,0,4
159,789,0,4
160,839,0,4
161,810,0,4
162,790,0,4
163,792,0,4
164,783,0,4
165,812,0,4
166,813,0,4
167,806,0,4
168,761,0,4
169,774,0,4
170,802,0,4
171,757,0,4
172,793,0,4
173,745,0,4
174,775,0,4
175,770,0,4
176,793,0,4
177,792,0,4
178,776,0,4
179,781,0,4
180,789,0,4
181,785,0,4
182,774,0,4
183,784,0,4
184,785,0,4
185,731,0,4
186,774,0,4
187,1005,0,
188,1893,0,
189,1822,0,
190,1529,0,
191,1293,0,
192,1197,0,
193,1109,0,
194,1021,0,
195,926,0,4
196,880,0,4
197,870,0,4
198,872,0,4
199,1006,0,
200,1544,0,
201,1958,0,
202,2086,0,
203,1726,0,
204,1478,0,
205,1346,0,
206,1233,0,
207,1124,0,
208,989,0,4
209,944,0,4
210,904,0,4
211,866,0,4
212,874,0,4
213,856,0,4
214,862,0,4
215,877,0,4
216,859,0,4
217,891,0,4
218,888,0,4
219,876,0,4
220,900,0,4
221,888,0,4
222,837,0,4
223,845,0,4
224,812,0,4
225,801,0,4
226,836,0,4
227,826,0,4
228,822,0,4
229,814,0,4
230,795,0,4
231,833,0,4
232,826,0,4
233,804,0,4
234,823,0,4
235,836,0,4
236,817,0,4
237,817,0,4
238,786,0,4
239,801,0,4
240,775,0,4
241,481,0,4
242,211,0,4
243,105,0,4
It looks like it is getting truncated too soon?