Actually I think I figured it out, but it is probably not pretty… and definitely not optimized!
float readVolt;
byte sendThis[8];
unsigned int waterVal, speedoVal, tachVal, egtVal, boostVal, fuelVal, oilVal;
unsigned long startTime, endTime, ndebounceTime = 0, ncurrentTime, debounceTime = 0, currentTime, previousSensorTime, currentSensorTime;
const unsigned long serialBlockTag = 0x11223344;
bool toothDetect = false;
char debounce = 0, ndebounce = 0;
#include <max6675.h> // Library for communicating with thermocouple
#include <SoftwareSerial.h>
SoftwareSerial HM10(2, 3); // RX, TX
// ThermoCouple
#define thermo_gnd_pin 45
#define thermo_vcc_pin 47
#define thermo_so_pin 53
#define thermo_cs_pin 51
#define thermo_sck_pin 49
MAX6675 thermocouple(thermo_sck_pin, thermo_cs_pin, thermo_so_pin);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //open the hardware serial port
HM10.begin(9600); //Set blutooth baud rate to 9600
// pinMode(thermo_vcc_pin, OUTPUT);
// pinMode(thermo_gnd_pin, OUTPUT);
// digitalWrite(thermo_vcc_pin, HIGH);
// digitalWrite(thermo_gnd_pin, LOW);
pinMode(A0,INPUT);
pinMode(A1,INPUT);
pinMode(A2,INPUT);
pinMode(A3,INPUT);
pinMode(A4,INPUT);
pinMode(A5,INPUT);
}
void loop() {
fastUpdate();
currentSensorTime = millis();
if((currentSensorTime - previousSensorTime) > 1000){
slowUpdate();
previousSensorTime = currentSensorTime;
}
delay(5);
}
//-------------------------------------------------------------------------------------------------------------------------
void sendFrame(unsigned long canFrameID, const byte* frameData) { // Used to serially send a CAN frame for each sensor
HM10.write((const byte*) & serialBlockTag, 4);
HM10.write((const byte*) & canFrameID, 4);
HM10.write(frameData, 8);
}
//--------------------------------------------------------------------------------------------------------------------------
void slowUpdate(){
//WaterTemp
readVolt = analogRead(A0);
readVolt = readVolt*5/1024; // Converts from bit representation of voltage to actual voltage
waterVal = -80.46*log(readVolt)+134.36; // Finding temp value from given voltage
sendThis[0] = (waterVal >> 8);
sendThis[1] = waterVal;
//EGT
readVolt = thermocouple.readCelsius();
egtVal = readVolt;
sendThis[2] = (egtVal >> 8);
sendThis[3] = egtVal;
//Fuel
readVolt = analogRead(A4);
readVolt = readVolt*5/1024;
fuelVal = (readVolt*244/5)-32;
sendThis[4] = (fuelVal >> 8);
sendThis[5] = fuelVal;
//OilPress
readVolt = analogRead(A5);
readVolt = readVolt*5/1024;
oilVal = (readVolt-.5)*25; // Converting from volts to psi
sendThis[6] = (oilVal >> 8);
sendThis[7] = oilVal;
sendFrame(3200, &sendThis[0]);
}
void fastUpdate(){
//Speedo
readVolt = analogRead(A1);
sendThis[0] = (speedoVal >> 8);
sendThis[1] = speedoVal;
//Tach
readVolt = analogRead(A2);
readVolt = readVolt*5/1024;
if ((readVolt < 4.9) && (toothDetect == false)) { // Checks for sensor reading and for double positive reading
if (debounce == 0){ // Debounce == 0 means not enough time has passed since the last positive reading
currentTime = millis();
if (debounceTime == 0){
debounceTime = currentTime;
}
if (currentTime - debounceTime >= 50){ // Once 50 ms have passed a positive reading can be sent again
debounce = 1;
}
}
else if (debounce == 1){ // If debounce time and voltage requirements are met, the rate of positive readings per minute
debounce = 0; // is output as rpm and scaled to the gauge range. Then all limiting variables are reset
debounceTime = 0;
endTime = millis();
tachVal = 60000/(endTime - startTime);
startTime = endTime;
toothDetect = true;
}
}
else if (readVolt > 4.9) { // Same as above if statement, except for negative readings
if (ndebounce == 0){
ncurrentTime = millis();
if (ndebounceTime == 0){
ndebounceTime = ncurrentTime;
}
if (ncurrentTime - ndebounceTime >= 50){
ndebounce = 1;
}
}
else if (ndebounce == 1){
ndebounce = 0;
ndebounceTime = 0;
toothDetect = false;
}
}
sendThis[2] = (tachVal >> 8);
sendThis[3] = tachVal;
//Boost
readVolt = analogRead(A3);
readVolt = readVolt*5/1024;
boostVal = (readVolt-.5)*25; // Converting from volts to psi
sendThis[4] = (boostVal >> 8);
sendThis[5] = boostVal;
sendFrame(3201, &sendThis[0]);
}
<?xml version="1.0" encoding="utf-8"?>
<!-- RealDash CAN XML description file, version 2 Requires RealDash version 1.5.8 or newer -->
<RealDashCAN version="2">
<frames>
<frame id="3200" endianess="big">
<value targetId="14" offset="0" length="2"></value> <!-- Water Temps target -->
<value targetId="27" offset="2" length="2"></value> <!-- EGT target -->
<value targetId="170" offset="4" length="2"></value> <!-- Fuel Level target -->
<value targetId="151" offset="6" length="2"></value> <!-- Oil Pressure target -->
</frame>
<frame id="3201" endianess="big">
<value targetId="64" offset="0" length="2"></value> <!-- Speedo target -->
<value targetId="37" offset="2" length="2"></value> <!-- Tach/RPM target -->
<value targetId="270" offset="4" length="2"></value> <!-- Boost target -->
</frame>
</frames>
</RealDashCAN>
Would appreciate any feedback on this though!