[SOLVED] if else if works sort of

ChrisTenone:

 case 0:

// code to implement winning a new car
    break;

Please post the full program :slight_smile:

...R

TomGeorge:
Hi,
Can you post a table showing the different combinations on inputs you can have and the respective outputs for each combination?

If your system will allow, each combination of inputs will be given a specific numeric value.
The "switch" then selects the appropriate "case" by using those values, the case has the code to select the appropriate outputs.

I am familiar with home and distant signals, a diagram of the placement of the points (Sorry GodsWonderfullRailway fan) and signals would help.

Good to see you have your code running.

Tom... :slight_smile:

Can I PM you?

Daryl.

Ab811:
Can I PM you?

Daryl.

Sure...
Tom.. :slight_smile:

I would like to thank Tom, George and Chris for their assistance in helping me out with my model railway signal project.

I have since added to the sketch to allow for two more signal aspects to the home signal. I am very pleased with the results so far and will be expanding the system as time allows. (a thing called WORK gets in the road every now and then).

I have started to try and get my head around switch statements. We’ll see how we go.

Kind regards,

Daryl.

/*Model Railway Signal Sketch.

  Final sketch for 2 aspect distant and 4 aspect home as at 01-August-2018
  Starter signals will be wired in at a later date.

  Button sketch originaly created 2005
  by DojoDave <http://www.0j0.org>
  modified 30 Aug 2011
  by Tom Igoe The Button example code is in the public domain at;
  http://www.arduino.cc/en/Tutorial/Button

  This sketch:
  Modified Button sketch
  2018
  by Daryl Roe

  Arduino UNO
  LED's attached to pins 7, 8, 9, 10, 11 and 12 and to ground via appropriate resistors.
  Distant signal has two aspects, Green (clear) over Yellow (caution) and
  Home signal has four aspects, Green (clear) over Red (STOP) for entry to home line and
  Green over Red for entry to loop line.

  3 current sensors detecting block occupancy attached to pins 2, 3 and 4.
  1 micro switch (on/off) from turnout to pin 5.
  The current sensors give HIGH as unoccupied and LOW as occupied.
  HIGH and LOW read as > 4.95v and < 1v respectively at pins.
  The turnout switch gives HIGH as straight and LOW as diverging.
*/
const int mainPin = 2;     //  number of the main sensor pin
const int turnoutPin = 5;  //  number of the turnout switch pin
const int homePin = 3;     //  number of the home sensor pin
const int loopPin = 4;     //  number of the loop sensor pin
const int DGPin =  9;      //  number of the distant green LED pin
const int DYPin = 10;      //  number of the distant yellow LED pin
const int HRPin = 12;      //  number of the home red LED pin
const int HGPin = 11;      //  number of the home green LED pin
const int HGLPin = 7;      //  number of the home green lower to loop pin
const int HRLPin = 8;      //  number of the home red lower to loop pin

// variables that will change:
bool mainState = 0;        // variables for reading the main sensor status
bool turnoutState = 0;     //etc
bool homeState = 0;        //etc
bool loopState = 0;        //etc

void setup()
{
  Serial.begin(9600);
  // initialize the LED pins as outputs:
  pinMode(DGPin, OUTPUT);  //Distant green = clear
  pinMode(DYPin, OUTPUT);  //Distant yellow = caution
  pinMode(HRPin, OUTPUT);  //Home red = STOP
  pinMode(HGPin, OUTPUT);  //home green = clear
  pinMode(HGLPin, OUTPUT);  //home green lower = clear for loop
  pinMode(HRLPin, OUTPUT);  //home red lower = STOP for loop

  // initialize the sensor pins as inputs:
  pinMode(mainPin, INPUT);
  pinMode(turnoutPin, INPUT);
  pinMode(homePin, INPUT);
  pinMode(loopPin, INPUT);
}
void loop()
{
  // serial print on to assist with debugging
  Serial.print(mainPin);
  // read the state of the sensor values:
  mainState = digitalRead(mainPin);
  Serial.print(mainState);
  delay(10);
  turnoutState = digitalRead(turnoutPin);
  Serial.println(turnoutState);
  delay(10);
  homeState = digitalRead(homePin);
  Serial.println(homeState);
  delay(10);
  loopState = digitalRead(loopPin);
  Serial.println(loopState);
  delay(10);

  // check the various sensor states
  // I will call this condition 1)
  if ((mainState == HIGH) &&      //no train. This could be demeed as a default
      (turnoutState == HIGH) &&   //set straight to home
      (homeState == HIGH))        //unoccupied
    //(loopState == DON'T CARE)
  {
    // If condition 1 is true, then turn appropriate LED's on or off
    digitalWrite(DGPin, LOW);     //distant green off
    digitalWrite(DYPin, HIGH);    //distant yellow on
    digitalWrite(HGPin, LOW);     //home green off
    digitalWrite(HRPin, HIGH);    //home red on
    digitalWrite(HGLPin, LOW);    //home green lower off
    digitalWrite(HRLPin, HIGH);   //home red lower high
  }
  else if
  //condition 2)
  ((mainState == LOW) &&          //train on main
      (turnoutState == HIGH) &&   //turnout set straight to home
      (homeState == HIGH))        //no train on home
    //(loopState == DON'T CARE)
  {
    // If condition 2 is true, then turn appropriate LED's on or off and train can carry on through:
    digitalWrite(DGPin, HIGH);    //on
    digitalWrite(DYPin, LOW);     //off
    digitalWrite(HGPin, HIGH);    //on
    digitalWrite(HRPin, LOW);     //off
    digitalWrite(HGLPin, LOW);    //home green lower off
    digitalWrite(HRLPin, HIGH);   //home red lower high
  }
  else if
  //condition 3)
  ((mainState == LOW) &&          //train on main
      (turnoutState == HIGH) &&   //turnout set straight to home
      (homeState == LOW))         //occupied, train on home
    //(loopState == DON'T CARE)
  {
    // If condition 3 is true, then turn appropriate LED's on or off and train must stop at home signal:
    digitalWrite(DGPin, LOW);     //of
    digitalWrite(DYPin, HIGH);    //on
    digitalWrite(HGPin, LOW);     //off
    digitalWrite(HRPin, HIGH);    //on
    digitalWrite(HGLPin, LOW);    //home green lower off
    digitalWrite(HRLPin, HIGH);   //home red lower high
  }
  else if
  //condition 4)
  ((mainState == LOW) &&          //train on main
      (turnoutState == LOW) &&    //turnout set diverging to loop
      //(homeState == DON"T CARE)
      (loopState == HIGH))        //no train on loop
  {
    // If condition 4 is true, then turn appropriate LED's on or off and train can enter loop:
    digitalWrite(DGPin, LOW);     //off
    digitalWrite(DYPin, HIGH);    //on
    digitalWrite(HGPin, LOW);     //on
    digitalWrite(HRPin, HIGH);    //off
    digitalWrite(HGLPin, HIGH);   //home green lower on
    digitalWrite(HRLPin, LOW);    //home red lower off
  }
  else
  {
    // otherwise, turn appropriate LED's on or off and train must not enter loop:
    digitalWrite(DGPin, LOW);     //off
    digitalWrite(DYPin, HIGH);    //on
    digitalWrite(HGPin, LOW);     //off
    digitalWrite(HRPin, HIGH);    //on
    digitalWrite(HGLPin, LOW);    //home green lower off
    digitalWrite(HRLPin, HIGH);   //home red lower on
  }
}//End of loop().

Hi,
Good to hear Daryl, WORK a necessary evil.... :slight_smile:
Try replacing this;

// serial print on to assist with debugging
  Serial.print(mainPin);
  // read the state of the sensor values:
  mainState = digitalRead(mainPin);
  Serial.print(mainState);
  delay(10);
  turnoutState = digitalRead(turnoutPin);
  Serial.println(turnoutState);
  delay(10);
  homeState = digitalRead(homePin);
  Serial.println(homeState);
  delay(10);
  loopState = digitalRead(loopPin);
  Serial.println(loopState);
  delay(10);

With this, it will help with your debug monitor;

  // read the state of the sensor values:
  mainState = digitalRead(mainPin);
  turnoutState = digitalRead(turnoutPin);
  homeState = digitalRead(homePin);
  loopState = digitalRead(loopPin);
  // serial print on to assist with debugging
  Serial.print("mainState = ");
  Serial.print(mainState);
  Serial.print("\tturnoutState = ");
  Serial.print(turnoutState);
  Serial.print("\t homeState = ");
  Serial.print(homeState);
  Serial.print("\tloopState = ");
  Serial.println(loopState);
  delay(10);

The \t are a tab instruction and placing all the data on one line makes observing you data easier.

Tom... :slight_smile:

TomGeorge:
Hi,
Good to hear Daryl, WORK a necessary evil.... :slight_smile:
Try replacing this;

// serial print on to assist with debugging

Serial.print(mainPin);
 // read the state of the sensor values:
 mainState = digitalRead(mainPin);
 Serial.print(mainState);
 delay(10);
 turnoutState = digitalRead(turnoutPin);
 Serial.println(turnoutState);
 delay(10);
 homeState = digitalRead(homePin);
 Serial.println(homeState);
 delay(10);
 loopState = digitalRead(loopPin);
 Serial.println(loopState);
 delay(10);



With this, it will help with your debug monitor;



// read the state of the sensor values:
 mainState = digitalRead(mainPin);
 turnoutState = digitalRead(turnoutPin);
 homeState = digitalRead(homePin);
 loopState = digitalRead(loopPin);
 // serial print on to assist with debugging
 Serial.print("mainState = ");
 Serial.print(mainState);
 Serial.print("\tturnoutState = ");
 Serial.print(turnoutState);
 Serial.print("\t homeState = ");
 Serial.print(homeState);
 Serial.print("\tloopState = ");
 Serial.println(loopState);
 delay(10);



The \t are a tab instruction and placing all the data on one line makes observing you data easier.

Tom... :)

Wow, worked perpectly Tom.

Thank you.

Daryl.