Hi,
I just got the sketch for fuel rate and fuel tank finished but can not test it.
Do anybody have a couple of minutes to look through if this would work? I don´t mean to fix everything or show every the best way. Since I am not that genius in arduino programming I would already be happy if that will work before I start to optimize everything.
EDIT
" I just found some things,I would need smoothing for the tank volume as thoose are moving a lot when the boat is rolling and so on!"
Thanks a lot!
// Get tankvolume (pin 20 Teensy3.2) and fuelrate (pin 19), calculate and send through NMEA2K CAN
#include <Arduino.h>
#include <NMEA2000.h>
#include <N2kMessages.h>
#include <NMEA2000_teensy.h>
tNMEA2000_teensy NMEA2000;
// *********************************************
// NMEA2K related setup
// *********************************************
// List here messages your device will transmit.
const unsigned long TransmitMessages[] PROGMEM = {130306L, 127505L, 0};
// *********************************************
// Variables to setup function reading fuelrate
// *********************************************
volatile int pulsCountValue; // Storage for counted pulses through interupt function
unsigned int FuelRate; // Calculated litres/hour
unsigned char flowsensor = 19; // Sensor Input
unsigned long currentTime;
unsigned long startTime;
#define FuelRateUpdatePeriod 1000
// *********************************************
// Variables to setup function reading fuel tank level
// *********************************************
unsigned char tankVolumeSens = A20;
#define FuelLevelUpdatePeriod 1000
// *********************************************
// Variables to setup sending of NMEA2K message
// *********************************************
double Instance = 0;
double Level;
int Capacity;
tN2kFluidType FluidType = N2kft_Fuel;
void setup() {
// Set Product information
NMEA2000.SetProductInformation("00000010", // Manufacturer's Model serial code
100, // Manufacturer's product code
"Fuel Information System", // Manufacturer's Model ID
"1.1.0.22 (2016-12-31)", // Manufacturer's Software version code
"1.1.0.0 (2016-12-31)" // Manufacturer's Model version
);
// Set device information
NMEA2000.SetDeviceInformation(10, // Unique number. Use e.g. Serial number.
150, // Device function=Atmospheric. See codes on http://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
75, // Device class=External Environment. See codes on http://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
2046 // Just choosen free from code list on http://www.nmea.org/Assets/20121020%20nmea%202000%20registration%20list.pdf
);
// Uncomment 2 rows below to see, what device will send to bus. Use e.g. OpenSkipper or Actisense NMEA Reader
//Serial.begin(115200);
//NMEA2000.SetForwardStream(&Serial);
// If you want to use simple ascii monitor like Arduino Serial Monitor, uncomment next line
//NMEA2000.SetForwardType(tNMEA2000::fwdt_Text); // Show in clear text. Leave uncommented for default Actisense format.
// If you also want to see all traffic on the bus use N2km_ListenAndNode instead of N2km_NodeOnly below
NMEA2000.SetMode(tNMEA2000::N2km_NodeOnly, 23);
// NMEA2000.SetDebugMode(tNMEA2000::dm_Actisense); // Uncomment this, so you can test code without CAN bus chips on Arduino Mega
NMEA2000.EnableForward(false);
NMEA2000.ExtendTransmitMessages(TransmitMessages);
NMEA2000.Open();
// Void Setup for Fuel Rate
pinMode(flowsensor, INPUT_PULLDOWN);
digitalWrite(flowsensor, LOW); // Optional Internal Pull-Up
attachInterrupt(0, pulsCounter, RISING); // Setup Interrupt
sei(); // Enable interrupts
currentTime = millis();
startTime = currentTime;
Serial.begin(9600);
// Setup for Fuel Rate END
}
void loop() {
// *****************************************************************************
// Call the looping functions
// *****************************************************************************
SendN2kFuelRate();
SendN2kFuelLevel();
NMEA2000.ParseMessages();
}
// *****************************************************************************
// Double replacement for standard map function
// *****************************************************************************
double dMap(double x, double in_min, double in_max, double out_min, double out_max) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
// *****************************************************************************
// Interupt function. Count pulses from interupt.
// *****************************************************************************
void pulsCounter() {
pulsCountValue++;
}
// *****************************************************************************
// Read and calculate fuelrate based on pulses from pin 19
// *****************************************************************************
double ReadFuelRate() {
currentTime = millis();
// Every 3 second, calculate and print litres/hour
if (currentTime >= (startTime + 3000))
{
startTime = currentTime; // Updates startTime
FuelRate = (pulsCountValue * 3.6); // (Pulse frequency x "3600/1000" 3.6 = flowrate in L/hour
// Serial.print(FuelRate, DEC); // Print litres/hour
// Serial.println(" L/hour");
}
return FuelRate;
}
// *****************************************************************************
// Read and map current fuel tank volume based on pin 20
// *****************************************************************************
double ReadTankVolume() {
double Level= dMap(analogRead(tankVolumeSens), 60, 900, 0, 100);
return Level;
}
// *****************************************************************************
// Send NMEA2K message for Fuel rate
// *****************************************************************************
void SendN2kFuelRate() {
static unsigned long FuelRateUpdated = millis();
tN2kMsg N2kMsg;
if ( FuelRateUpdated + FuelRateUpdatePeriod < millis() ) {
SetN2kEngineDynamicParam(N2kMsg, Instance, N2kDoubleNA, N2kDoubleNA, N2kDoubleNA, N2kDoubleNA, FuelRate, N2kDoubleNA);
FuelRateUpdated = millis();
NMEA2000.SendMsg(N2kMsg);
pulsCountValue = 0;
}
}
// *****************************************************************************
// Send NMEA2K message for fuel tank level
// *****************************************************************************
void SendN2kFuelLevel() {
static unsigned long FuelLevelUpdated = millis();
tN2kMsg N2kMsg;
if ( FuelLevelUpdated + FuelLevelUpdatePeriod < millis() ) {
SetN2kFluidLevel(N2kMsg, Instance, FluidType, Level, Capacity);
FuelLevelUpdated = millis();
NMEA2000.SendMsg(N2kMsg);
}
}