/* OilPresAndTempV5.ino Original code made by DrMotor for NiNo-racing https://www.facebook.com/ninomotorracing/ https://racechrono.com/forum/discussion/1590/diy-data-collection-with-link-to-working-code-for-arduino-and-bluetooth Modified by _valtsu_ to send oil pressure and temperature data to RaceChrono Hardware and connections used: 1. Phone or another device with RaceChrono -- only 5min log with Free version, buy "Pro" for more 2. Arduino Uno used in this case 3. HC-06 Bluetooth module with voltage divider circuit 4. Analog output from AEM X-series oil pressure gauge 0-5v connected to analog input A0 with 10k pulldown resistor 5. Bosch NTC M12-H oil temp sensor connected to Analog input A1 with 1k pullup resistor Notes: -Cannot upload sketch when HC-06 is connected with hardwareserial to Arduino -Update rate about 10hz */ uint16_t count = 0; // Message counter float Pressure = 0; // Starting pressure for filter float R1 = 1000; // Pull up resistor value for analog input 1 (oil temperature) float logR2, R2, T, Temp; // Variables needed for calculating thermistor resistance (oil temperature) float A = 0.001314079102895, B = 0.000256697608317663, C = 1.83775519952406e-07; // Variables for Steinhart–Hart equation int readPres = 0; // Variable for analog input 0 (oil pressure) #define NUMSAMPLES 5 // number of temp samples int samples[NUMSAMPLES]; // to get more accurate temperature unsigned long previousMillis = 0; // will store last time oil temp was updated const long interval = 1000; // interval at when to update oil temp (1hz update rate) int update = 100; // For 10hz update rate (100ms) unsigned long time_now = 0; // Will store last time message was sent to Racechrono (for 10hz update rate) void setup() { Serial.begin(115200); // Data rate must match settings in Bluetooth board (default 9600 can be changed with AT-commands) } String checksum(String s) // Checksum calculation { uint8_t cs = 0x00; for (unsigned int i = 1; i < s.length() - 1; i++) cs = cs ^ s.charAt(i); // XOR String hex = String(cs, HEX); hex.toUpperCase(); return String((cs < 0x10 ? "0" : "") + hex); } void loop() { //For 10hz update rate time_now = millis(); // Read the pressure readPres = analogRead(0); // Read analog input Pressure = ((1.72 * ((readPres) / 2.05)) - 86) / 100; // Caluculate real pressure from adc result // For 1hz update ratio for oil pressure unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; // save the last time oil temp was updated // Averaging thermistor measurement for better readings uint8_t i; float average; // take 5 samples in a row, with a slight delay for (i = 0; i < NUMSAMPLES; i++) { samples[i] = analogRead(1); delay(10); } // average all the samples out average = 0; for (i = 0; i < NUMSAMPLES; i++) { average += samples[i]; } average /= NUMSAMPLES; // Calculate temperature R2 = R1 / (1023.0 / (float)average - 1.0); logR2 = log(R2); T = (1.0 / (A + B * logR2 + C * logR2 * logR2 * logR2)); Temp = T - 273.15; } // Put it into RaceChrono format String str = String("$RC3,,") + String(count++) + ",,,,,,,,,"; // $RC3,[time],[count],[xacc],[yacc],[zacc],[gyrox],[gyroy],[gyroz],[rpm/d1],[d2], str += String(Pressure, 1) + ","; //[a1] str += String(Temp, 1) + "," + ",,,,,,,,,,,,*"; //[a2],[a3],[a4],[a5],[a6],[a7],[a8],[a9],[a10],[a11],[a12],[a13],[a14],[a15]*checksum // Send it via serial line and Bluetooth to RaceChrono on your Phone Serial.println(str + checksum( str )); //For 10hz update rate while (millis() < time_now + update) { //wait 100ms } }