Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: Vehicle Data
- - Source Code NOT compiled for: Arduino Mega
- - Source Code created on: 2025-07-11 22:33:52
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* send data from bluetooth to android */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <SoftwareSerial.h>
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- float T_interpolate(byte DS_Temp); // Added prototype for T_interpolate function
- /***** DEFINITION OF Software Serial *****/
- const uint8_t HC05_Bluetooth_TX_PIN = A0; // Bluetooth TX pin connected to Arduino A0
- const uint8_t HC05_Bluetooth_RX_PIN = A1; // Bluetooth RX pin connected to Arduino A1
- SoftwareSerial HC05(BT_TX_PIN, BT_RX_PIN); // Instantiate Bluetooth serial
- // Define other variables used in the code
- bool SilenceFound = false;
- unsigned long StartTime = 0;
- bool PreambleFound = false;
- unsigned long PreambleTimer = 0;
- byte ALDLbyte = 0;
- const int ByteCount = 64; // Total bytes in data packet
- int DataStreamIndex = 0;
- unsigned int CheckTotal = 0;
- byte DataBytes[65]; // Data buffer
- byte Preamble[3] = {0x80, 0x95, 0x01}; // Preamble sequence
- int i = 0;
- int CheckSum = 0;
- float RPM, TPS, MAF, BLM, INTEGRATOR, InjPW, O2mv, MAT, Runtime;
- const int HexDataOutputPin = 4; // Pin for hex output flag
- int linecount = 32; // For hex output formatting
- int bytecounter = 0;
- int linecount = 32; // For hex output formatting
- byte M1Cmd[4] = {0x80, 0x95, 0x01, 0x00}; // Example command, replace with actual command if needed
- void setup(void)
- {
- // Initialize Bluetooth serial
- HC05.begin(9600);
- // Initialize serial communication for debugging
- Serial.begin(115200);
- // Set pin modes
- pinMode(4, INPUT_PULLUP); // Assuming active low for hex output flag
- }
- void loop(void)
- {
- // Wait for silence period on the ALDL
- Serial.print("wait for silence ");
- SilenceFound = false; // Reset silence flag
- StartTime= micros(); // First look for an active signal or a timeout - initialize timer
- // Should exit this loop on the start of a bit
- while ((micros() - StartTime) < 15000) // Wait for a 15 ms silent period
- {
- if (digitalRead(4) == 0) // Any line activity resets the start time
- {
- StartTime= micros(); // Timing starts over
- }
- }
- SilenceFound = true; // Set the silence flag on exit
- while (SilenceFound == true) // While silence found flag is set, continuously request and transmit Mode 1 data
- {
- PreambleFound = false; // Reset preamble found flag
- while (PreambleFound == false) // Look for preamble
- {
- Serial.print(" M1 cmd ");
- i=0; // use bytecounter to send the outgoing Mode1CMD sequence
- while (i<4)
- {
- Serial1.write(M1Cmd[i]); // Send command bytes
- i++;
- }
- Serial.println(" Finding Preamble ");
- i=0; // Reset byte counter
- PreambleTimer = millis(); // Initialize timer
- while ((((millis() - PreambleTimer) < 100)) && (PreambleFound == false))
- {
- if (Serial1.available() > 0)
- {
- ALDLbyte = Serial1.read();
- if (ALDLbyte == Preamble[i])
- {
- i++;
- if (i > 2) PreambleFound = true;
- }
- else
- {
- PreambleFound = false;
- i=0;
- }
- }
- }
- }
- // Read data packet
- DataStreamIndex = 1;
- while (DataStreamIndex < 65)
- {
- if (Serial1.available() > 0)
- {
- DataBytes[DataStreamIndex] = Serial1.read();
- DataStreamIndex++;
- }
- }
- // Calculate checksum
- i=1;
- CheckTotal = 0x80+0x95+0x01; // Sum preamble bytes
- while (i < ByteCount)
- {
- CheckTotal += DataBytes[i];
- i++;
- }
- CheckSum = 0x200 - CheckTotal;
- // Verify checksum
- if (CheckSum == DataBytes[ByteCount])
- {
- // Prepare data string to send via Bluetooth
- String dataString = "";
- dataString += "RPM: ";
- dataString += String((int)(DataBytes[11] * 25));
- dataString += ", TPS: ";
- dataString += String(DataBytes[10] * 0.019608);
- dataString += ", MAF: ";
- dataString += String(((DataBytes[36] * 256) + DataBytes[37]) * 0.003906);
- dataString += ", BLM: ";
- dataString += String(DataBytes[20]);
- dataString += ", BLCELL: ";
- dataString += String(DataBytes[21]);
- dataString += ", INTEGRATOR: ";
- dataString += String(DataBytes[22]);
- dataString += ", InjPW: ";
- dataString += String(((DataBytes[45] * 256) + DataBytes[46]) * 0.015259);
- dataString += ", O2mv: ";
- dataString += String(DataBytes[17] * 4.44);
- dataString += ", MAT: ";
- dataString += String(T_interpolate(DataBytes[30]));
- dataString += ", Runtime: ";
- dataString += String(DataBytes[52] * 256 + DataBytes[53]);
- // Send data string over Bluetooth
- HC05.println(dataString);
- }
- // Optional: add delay or further processing
- delay(1000); // Send data every 1 second
- }
- }
- // T_interpolate function remains unchanged from the provided user code
- float T_interpolate(byte DS_Temp)
- {
- const float TempScale[38] = {1,5,6,9,11,15,19,25,31,38,47,57,67,79,91,104,117,130,142,154,164,175,184,192,200,206,212,217,222,226,230,233,235,238,240,242,244,256};
- const float TempValue[38] = {-40,-30,-25,-20,-15,-10,-5,0,5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100,105,110,115,120,125,130,135,140,145,200};
- float T_Range;
- float T_Diff;
- float TempRange;
- float Temperature;
- int i = 0;
- while (i<38)
- {
- if (TempScale[i]> DS_Temp) break;
- i++;
- }
- if (i>0)
- {
- T_Range = TempScale[i] - TempScale[i-1];
- T_Diff = DS_Temp - TempScale[i-1];
- TempRange = TempValue[i] - TempValue[i-1];
- Temperature = TempValue[i-1] + (T_Diff/T_Range)*TempRange;
- }
- else Temperature = TempValue[0];
- return Temperature;
- }
Advertisement
Add Comment
Please, Sign In to add comment