I sorted out my comm issues between KSP and my controller.
I threw together a basic sketch that 'tries' to do what you were attempting. It will blink the builtinLED when the fuel level is below 10.0.
PLEASE make sure that you have the correct COM port assigned in the settings.cfg file in the plugindata directory
D:\SteamLibrary\SteamApps\common\Kerbal Space Program\GameData\KerbalSimpit\PluginData
#include <KerbalSimpitMessageTypes.h>
#include <PayloadStructs.h>
#include <KerbalSimpit.h>
KerbalSimpit mySimpit(Serial);
float fuel = 10.0;
unsigned int lastBlink = 0;
int blinkPeriod = 500;
bool blinkBool = false;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(LED_BUILTIN,OUTPUT);
digitalWrite(LED_BUILTIN,LOW); //TURN THE LED OFF UNTIL WE CONNECT WITH KSP
delay(100);
while(!mySimpit.init()); {
delay(100);
}
digitalWrite(LED_BUILTIN,HIGH); // SHOWS WERE CONNECTED!
delay(500);
mySimpit.inboundHandler(messageHandler);
mySimpit.registerChannel(10); // 10 is the value for LF -- Outputs a resourceMessage
}
void loop() {
// put your main code here, to run repeatedly:
mySimpit.update();
checkFuelLevel();
}
void messageHandler(byte messageType, byte msg[], byte msgSize) {
if (msgSize == sizeof(resourceMessage))
{
resourceMessage myLF;
myLF = parseResource(msg);
//float percentLF = myLF.available / myLF.total;
fuel = myLF.available;
}
}
void checkFuelLevel(){
unsigned int nowCheck = millis();
if( fuel < 10.0 ){ // CHECK FOR FUEL LOW
if( nowCheck > ( lastBlink + blinkPeriod ) ){
lastBlink = nowCheck;
digitalWrite(LED_BUILTIN, blinkBool);
blinkBool = !blinkBool;
}
}else{
digitalWrite(LED_BUILTIN,HIGH);
}
}