Hello Timo,
I am really sorry to bother you with the rudder position thing.. I do try it now for the last 6 days and get stuck every time with something else.
That is killing me soon... Back to analog instruments! :grin:
This code:
void RudderPostion() {
double dMap(double x, double in_min = 40, double in_max = 1023, double out_min = -45, double out_max = 45){
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
}
is producing this error:
RudderPosition_NMEA2K_v2:42: error: a function-definition is not allowed here before '{' token
exit status 1
a function-definition is not allowed here before '{' token
This is the hole sketch I thought to use:
// Demo: NMEA2000 library. Send main wind data to the bus.
#include
#include // This will automatically choose right CAN library and create suitable NMEA2000 object
#include
int rudderSens = 0xA0; // value read from the pot
double rudderValue = 0;
const unsigned long TransmitMessages[] PROGMEM = {127245L, 0};
void setup() {
// Set Product information
NMEA2000.SetProductInformation("00000002", // Manufacturer's Model serial code
100, // Manufacturer's product code
"Rudder Position monitor", // 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(1, // Unique number. Use e.g. Serial number.
155, // Device function=Atmospheric. See codes on http://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
40, // Device class=External Environment. See codes on http://www.nmea.org/Assets/20120726%20nmea%202000%20class%20&%20function%20codes%20v%202.00.pdf
1851 // 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();
}
#define RudderSendPeriod 100 // ms
void SendRudder() {
static unsigned long RudderSendTime = millis() + RudderSendPeriod;
tN2kMsg N2kMsg;
if ( RudderSendTime < millis() ) {
RudderSendTime += RudderSendPeriod;
double dMap(double x, double in_min = 40, double in_max = 1023, double out_min = -45, double out_max = 45) {
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
SetN2kRudder(N2kMsg, rudderValue);
NMEA2000.SendMsg(N2kMsg);
}
}
void loop() {
NMEA2000.ParseMessages();
SendRudder();
}
It took also some time until I have recognized that the usual map function will not work here. I thought your post which showed the userdefiend map function is not mandetorry for this case and I got always wrong values. The strange thing is that I used the standart map function to test the sensor in a simple sketch and it was working just fine like this one:
int rudderSens= A0; // Analog input pin that the potentiometer is attached to
int sensorValue = 0; // value read from the pot
int RudderPosition;
void setup() {
Serial.begin(115200);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(rudderSens);
// map it to the range of the analog out:
RudderPosition= map(sensorValue, 54, 1023, -45,45);
// print the results to the serial monitor:
Serial.println(sensorValue);
}
In case I got the sketch running than I always got a constant rudderposition send which had nothing to do with the sensor value.
If I get this one working fine than I think I get almost every other I thought to implement also working.
Would be gorgeous to get this running with the help of you, without I think I have to buy the commercial version of this sensor for rudder position.
Thousends of thanks in advance!