I use a arduino Mega to send warning light signals via bluetooth to a tablet running the Realdash app.
Editing the sample file, i include a pin for speed sensor reading to update the speedometer, it happens that the code I entered has a delay of 300ms, it is slow for the other lights and is too fast for speedometer.
If i increase the delay, the displayed speed increase, if i decrease the delay, the displayed speed decrease.
The problem is that this delay slows the sending of other signals, which causes the direction lights to fail when times coincide.
And the number of speedometer change is too fast, I wanted to update the speed value every 1 second, but leave a maximum of 100ms of delay throughout the routine, as I do to eliminate this delay and measure the speed correctly and update its value every time 1 second using interruption code. Thanks.
/**
* ============================================================================
* 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
* ============================================================================
**/
unsigned int digitalPins = 0;
int analogPins[7] = {0};
unsigned int tanque = 0;
unsigned int tanque_mapeado = 0;
long rpmcount;
long velocidade;
void setup()
{
attachInterrupt(digitalPinToInterrupt(20), rpm_fun, CHANGE);
Serial.begin(115200);
pinMode(2, INPUT_PULLUP);
pinMode(3, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
delay (200);
}
void loop()
{
rpm();
rpm_fun();
ReadDigitalStatuses();
ReadAnalogStatuses();
SendCANFramesToSerial();
}
void rpm()
{
long count;
cli();
count = rpmcount;
sei();
delay (300);
velocidade = int(rpmcount);
rpmcount = 0;
}
void rpm_fun()
{
rpmcount++;
}
void ReadDigitalStatuses()
{
// read status of digital pins (1-13)
digitalPins = 0;
int bitposition = 0;
for (int i=1; i<14; i++)
{
if (digitalRead(i) == LOW) digitalPins |= (1 << bitposition);
bitposition++;
}
}
void ReadAnalogStatuses()
{
// read analog pins (0-7)
tanque = analogRead (0);
tanque_mapeado = map(tanque,14,96,1023,0);
for (int i=0; i<7; i++)
{
analogPins[i] = analogRead(i);
}
}
void SendCANFramesToSerial()
{
byte buf[8];
// build 2nd CAN frame, Arduino digital pins and 2 analog values
memcpy(buf, &digitalPins, 2);
memcpy(buf + 2, &tanque_mapeado, 2);
memcpy(buf + 4, &velocidade, 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);
}
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 unsigned long serialBlockTag = 0x11223344;
Serial.write((const byte*)&serialBlockTag, 4);
// the CAN frame id number
Serial.write((const byte*)&canFrameId, 4);
// CAN frame payload
Serial.write(frameData, 8);
}