0% found this document useful (0 votes)
182 views4 pages

Programa Arduino

This Arduino sketch sends simulated engine parameter data over CAN to RealDash software. It reads analog and digital pin values, generates example engine data, then builds and sends 4 CAN frames with the data to a serial port. The frames include RPM, MAP, CLT, TPS and pin values. A text frame is also sent periodically with example messages.

Uploaded by

Thiago Gonzaga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views4 pages

Programa Arduino

This Arduino sketch sends simulated engine parameter data over CAN to RealDash software. It reads analog and digital pin values, generates example engine data, then builds and sends 4 CAN frames with the data to a serial port. The frames include RPM, MAP, CLT, TPS and pin values. A text frame is also sent periodically with example messages.

Uploaded by

Thiago Gonzaga
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

/**

* ============================================================================
* Name : RealDash_CAN.ino
* Part of : RealDash
* Author : Jani Immonen
* Created : 15.10.2017
*
* Arduino example sketch of how to use RealDash CAN protocol.
*
* This example code is free for any use.
*
* www.realdash.net
* ============================================================================
**/

// Arduino digital and analog pins


unsigned int digitalPins = 0;
int analogPins[7] = {0};

unsigned int rpm = 0;


unsigned int kpa = 992; // 99.2
unsigned int tps = 965; // 96.5
unsigned int clt = 80; // 80 - 100
unsigned int textCounter = 0;

void setup()
{
// init serial
Serial.begin(115200);
delay(100);
}

void loop()
{
ReadDigitalStatuses();
ReadAnalogStatuses();
SendCANFramesToSerial();

// just some dummy values for simulated engine parameters


if (rpm++ > 10000)
{
rpm = 500;
}
if (kpa++ > 2500)
{
kpa = 10;
}
if (tps++ > 1000)
{
tps = 0;
}
if (clt++ > 230)
{
// all values in frame are handled as unsigned values. To have negative values,
// offset actual value and write corresponding conversion to xml file imported
to RealDash
clt = 0;
}
if (textCounter++ > 4000)
{
textCounter = 0;
}

delay(5);
}

void ReadDigitalStatuses()
{
// read status of digital pins (1-13)
digitalPins = 0;

int bitposition = 0;
for (int i=1; i<14; i++)
{
if (digitalRead(i) == HIGH) digitalPins |= (1 << bitposition);
bitposition++;
}
}

void ReadAnalogStatuses()
{
// read analog pins (0-7)
for (int i=0; i<7; i++)
{
analogPins[i] = analogRead(i);
}
}

void SendCANFramesToSerial()
{
byte buf[8];

// build & send CAN frames to RealDash.


// a CAN frame payload is always 8 bytes containing data in a manner
// described by the RealDash custom channel description XML file
// all multibyte values are handled as little endian by default.
// endianess of the values can be specified in XML file if it is required to use
big endian values

// build 1st CAN frame, RPM, MAP, CLT, TPS (just example data)
memcpy(buf, &rpm, 2);
memcpy(buf + 2, &kpa, 2);
memcpy(buf + 4, &clt, 2);
memcpy(buf + 6, &tps, 2);

// write first CAN frame to serial


SendCANFrameToSerial(3200, buf);

// build 2nd CAN frame, Arduino digital pins and 2 analog values
memcpy(buf, &digitalPins, 2);
memcpy(buf + 2, &analogPins[0], 2);
memcpy(buf + 4, &analogPins[1], 2);
memcpy(buf + 6, &analogPins[2], 2);

// write 2nd CAN frame to serial


SendCANFrameToSerial(3201, buf);

// build 3rd CAN frame, rest of Arduino analog values


memcpy(buf, &analogPins[3], 2);
memcpy(buf + 2, &analogPins[4], 2);
memcpy(buf + 4, &analogPins[5], 2);
memcpy(buf + 6, &analogPins[6], 2);

// write 3rd CAN frame to serial


SendCANFrameToSerial(3202, buf);

// build 4th frame, this is a text extension frame


// only send once at 1000 loops
if (textCounter == 0)
{
SendTextExtensionFrameToSerial(3203, "Hello RealDash, this is Arduino sending
some text data");
}
else if (textCounter == 1000)
{
SendTextExtensionFrameToSerial(3203, "Tomorrow's forecast: Lots of sun and 30
degrees centigate");
}
else if (textCounter == 2000)
{
SendTextExtensionFrameToSerial(3203, "Now Playing: Insert your favorite song
info here");
}
else if (textCounter == 3000)
{
SendTextExtensionFrameToSerial(3203, "Message from Arduino: All systems running
at nominal efficiency");
}
}

void SendCANFrameToSerial(unsigned long canFrameId, const byte* frameData)


{
// the 4 byte identifier at the beginning of each CAN frame
// this is required for RealDash to 'catch-up' on ongoing stream of CAN frames
const byte serialBlockTag[4] = { 0x44, 0x33, 0x22, 0x11 };
Serial.write(serialBlockTag, 4);

// the CAN frame id number (as 32bit little endian value)


Serial.write((const byte*)&canFrameId, 4);

// CAN frame payload


Serial.write(frameData, 8);
}

void SendTextExtensionFrameToSerial(unsigned long canFrameId, const char* text)


{
if (text)
{
// the 4 byte identifier at the beginning of each CAN frame
// this is required for RealDash to 'catch-up' on ongoing stream of CAN frames
const byte textExtensionBlockTag[4] = { 0x55, 0x33, 0x22, 0x11 };
Serial.write(textExtensionBlockTag, 4);

// the CAN frame id number (as 32bit little endian value)


Serial.write((const byte*)&canFrameId, 4);

// text payload
Serial.write(text, strlen(text) + 1);
}
}

You might also like