Multiple switch cases

Hey guys,

Having a bit of trouble which Ill try and explain the best I can.

I am using a programme called Link2fs which if you are not familiar with it acts as an interface programme between arduino and FSX. Basically it works an input by sending a Serial.println to Link2fs which makes the control in the simulator. From the outputs it read data from FSX and when that data reaches a certain parameter it digitalWrites a pin to activate an LED or an alarm.

My trouble is trying to get multiple switch cases to feed into one LED. So on the aircraft I fly if I drop my manifold pressure below 14inHG whilst my gear is up I get an LED come on. Therefore for that LED to come on two conditions have to be met my Manifold pressure has to drop and my gear has to be up but I have no Idea how to code it. I can get them to work as separate switch cases but not together. Ill post what code I have go so far in the hope that someone can help.

There is lots more on there as it runs my annunciator panel but I am looking at switch case 'Q' & 'Y'

Many thanks
Marc

int AnunPinNo[5] = {2, 3, 4, 5, 6};// the selected pins with LED's
char AnunIdent2[5] = {'E','N','F','H','R'};//Selected items in "Annunciators" in Multi
long AnunButtonStop = 0;
boolean ButtonStopEnable = 0;
int AnunPinValNew[5] = {0,0,0,0,0};//Array for pin values (The new readings)
int AnunPinValOld[5] = {0,0,0,0,0};//Array for pin values (The old readings)
int count = 0; //for array reading
int CodeIn;// Gets the serial read
int bipassSwitchPins = 0; //=1 avoids repetative pin assignments
int dummy;//dummy output until LED's are restored after test
int AnunPinTot = 0;//Used for cancelling alarms if fault disappears before pushing button
int KpinNo; 
int Koutpin;
String start;
String pitot;
String warn;
int warnalarm;
int gearalarm;
String gear;
String KoldpinStateSTR, KpinStateSTR, Kstringnewstate,Kstringoldstate;

void setup(){
  Kstringoldstate = "111111111111111111111111111111111111111111111111111111111111111111111";

  for (int KoutPin = 10; KoutPin < 12; KoutPin++)// Get these pins ready as inputs  
  {
    pinMode(KoutPin, INPUT);
    digitalWrite(KoutPin, HIGH);  
  }
  
  for (count=0;count<7;count++) {// we make all the Anun declarations at once
    // Be careful with the above line.(Dont involve a pin that will have a switch on it ,, it can fry your pin or card)
    pinMode(AnunPinNo[count], OUTPUT);
   digitalWrite(AnunPinNo[count], LOW); 

   pinMode(7, OUTPUT);
   digitalWrite(7, LOW);
   pinMode(8, OUTPUT);
   digitalWrite(8, LOW);
   pinMode(9, OUTPUT);
   digitalWrite(9, LOW);

  Serial.begin(115200);
}}

void loop() {
  {KEYS();} //Check the "keys" section
  {OTHER();}
  if (Serial.available()) { 
    CodeIn = getChar();   
    if (CodeIn == '=') {EQUALS();} 
    if (CodeIn == '<') {LESSTHAN();}
    if (CodeIn == '?') {QUESTION();}
    if (CodeIn == '/') {SLASH();}
  }
}

char getChar()
{
  while(Serial.available() == 0);
  return((char)Serial.read());
}

void OTHER(){
   // Normal "run" stuff in here
}

void EQUALS(){      
       CodeIn = getChar();
  switch(CodeIn) {
    case 'A':
       //Do something
    break;
     
    case 'W':
       //Do something
    break;
     
    case 'a':
       //Do something
    break;
       //etc etc etc
     }
}

void LESSTHAN(){ 
  CodeIn = getChar(); // Get another character
  switch(CodeIn) {// Now lets find what to do with it
     case 'b':{
      pitot = "";
      pitot += getChar();
      int pitotInt = pitot.toInt(); // convert it to an integer (Thanks Phill)
      if (pitotInt == 1) {digitalWrite(8, HIGH);} else {digitalWrite(8, LOW);}
      break;
     }

      case 'k':{
      start = "";
      start += getChar();
      int startInt = start.toInt();
      if (startInt == 1) {digitalWrite(9, HIGH);} else {digitalWrite(9, LOW);}
      }
    
  }   
    }
      
void QUESTION(){    // The first identifier was "?"
 CodeIn = getChar(); // Get another character
  switch(CodeIn) {// Now lets find what to do with it

case 'Q':{//The second identifier was an "Q" = Manifold pressure
warn = "";
warn += getChar();
warn += getChar();
warn += getChar();
int warnInt = warn.toInt();
if (warnInt <6.3) {warnalarm = 1;}else{warnalarm = 0;}
break;
} 

case 'Y':{//The second identifier was an "Y" = Gear position
gear = "";
gear += getChar();
gear += getChar();
gear += getChar();
int gearInt = gear.toInt();
if (gearInt <2) {gearalarm = 1;}else{gearalarm = 0;}
break; 
}
if (warnalarm ==1 && gearalarm == 1) {digitalWrite(7, HIGH);}else {digitalWrite(7, LOW);} 
}
}



void SLASH(){    // The first identifier was "/" (Annunciators)
  CodeIn = getChar();
  for (count=0;count<7;count++) {
   if (CodeIn == AnunIdent2[count]){
      AnunPinValNew[count] = (getChar()- 48); 
     if (AnunPinValNew[count] == 1){ 
       if (AnunPinValOld[count] < 1) {digitalWrite(AnunPinNo[count], HIGH);
        
         AnunPinValOld[count] = AnunPinValNew[count];}}
     if (AnunPinValNew[count] == 0){
       if (AnunPinValOld[count] > 0) {digitalWrite(AnunPinNo[count], LOW);AnunPinValOld[count] = AnunPinValNew[count]; 
   }
}
}
}
}

   void KEYS() 
{
  Kstringnewstate = "";
  for (int KpinNo = 10; KpinNo < 12; KpinNo++){
    KpinStateSTR = String(digitalRead(KpinNo)); 
    KoldpinStateSTR = String(Kstringoldstate.charAt(KpinNo - 10));
    if (KpinStateSTR != KoldpinStateSTR)
    {
      if (KpinNo == 10){if (KpinStateSTR == "1" ){Serial.println ("E42");}else {Serial.println ("E43");}}//Start
      if (KpinNo == 11){if (KpinStateSTR == "1" ){Serial.println ("C05");}else {Serial.println ("C06");}}//Pitot
    }
    Kstringnewstate += KpinStateSTR;
  }
   Kstringoldstate = Kstringnewstate;
  }
   }}}}}

Don't you hate it when that happens?

Please use the IDE's auto-format tool before posting code.

Marc1980:
My trouble is trying to get multiple switch cases to feed into one LED. So on the aircraft I fly if I drop my manifold pressure below 14inHG whilst my gear is up I get an LED come on. Therefore for that LED to come on two conditions have to be met my Manifold pressure has to drop and my gear has to be up but I have no Idea how to code it. I can get them to work as separate switch cases but not together. Ill post what code I have go so far in the hope that someone can help.

I didn't look through your code, but from your description I'd say that the solution would be to seperate the logical state of your LED from the physical state, and you do that by using the IPO programming logic for the loop() function. Seperate these steps:

  • Input() // read all necessary input from Serial, buttons and sensors and save input states in variables
  • Processing() // do logical processing of logical output states in variables only
  • Output() // actually switch the logical state to the physical state on the output devices

So with an LED that depends on the two conditions "manifold pressure" and "gear position", the pseudo logic would go something like that:

void loop()
{
  // input
  int manifold_pressure= readPressureSensor();
  int gear_position= readGearPosition();
  // insert more input readings here

  // processing
  boolean ledState=false;
  if (manifold_pressure<14 && gear_position== GEARUP) ledState=true;
  // insert more processing logic here


  // output
  digitalWrite(LEDPIN, ledState);
  // insert more output settings of logical states to physical devices here
}

During the "processing" part of the code you then can change the state of variables several times if you like, without anything to happen at the physical outputs. And only in the last step, after all processing logic has been worked through, you finally set the outputs accordingly to their logical state after processing all conditions you want to have evaluated.

+1 to what jurs posted and another tip,

if what you should send to FSX depends on multiple conditions then you can have your input code set bits in a variable, each bit is a true/false, and when your process code sees certain combinations of bits, it will trigger. The fun part is that in a variable, the bits add up to numbers and you can use a switch-case to go right to the code for this-and-this-but-not-that-etc-for-many-conditions-combined and set a flag for the output to turn the led on or off.

I'm a bit surprised you only want one light and not a Master Warning Panel of some kind.

If your serial input gives you ( letter/label + number ) codes, you can use a state machine to evaluate the text as fast as it arrives. The label tells your code what to do with the next characters.

Here's the intro to Nick Gammon's tutorial on state machines. The process state says which code to run, he shows code for this example:

State Machine

Another way of processing incoming data, without blocking, is to set up a "state machine". Effectively this means looking at each byte in the input stream, and handling it depending on the current state.

As an example, say you had this coming into the serial port:

R4500S80G3

Where Rnnn is RPM, Snnnn is speed, and Gnnnn is the gear setting.

The state machine below switches state when it gets a letter "R", "S" or "G". Otherwise it processes incoming digits by multiplying the previous result by 10, and adding in the new one.

When switching states, if first handles the previous state. So for example, after getting R4500 when the "S" arrives, we call the ProcessRPM function, passing it 4500.

This has the advantage of handling long messages without even needing any buffer, thus saving RAM. You can also process message as soon as the state changes, rather than waiting for end-of-line.

That code always knows what switch-case or other code to use based on what came before.

With IPO you get modular code and with state machines you get fast evaluation at whatever complexity you need and can for.

That tutorial can be found at the 2nd Nick Gammon blog address in my sig space below.

BTW, a Leonardo or Teensy will let you interface the PC as stick + mouse + keys in HID mode.

Thanks for the replies.

I'm a bit surprised you only want one light and not a Master Warning Panel of some kind.

I do have a warning panel with 8 indications. Stall, Start, Gear, Gyro, Alternator, Fuel, Oil pressure & Pitot. It's the gear that Im struggling with as the rest are dependent on only one condition.

I'm quite new to the world of Arduino so will take the above advice one step at a time.

Kind regards
Marc