Help with understanding how my code will be read

Hi All,

I am fairly new to coding and I wanted to make sure what I have written will work as I intend it to.

const int LED1 = 51;
const int LED2 = 50;
const int LED3 = 49;
const int LED4 = 48;
const int LED5 = 47;
const int LED6 = 46;
const int LED7 = 45;
const int LED8 = 44;

unsigned char LEDNum = 1;

const int Micro1 = 3; // confirms all LED's are selected
int Micro1State;
const int Micro2 = 4; // confirms LED on
int Micro2State;

int ButtonPress1 = 0;
int ButtonPress2 = 0;

int LEDSETUP1 = 0; //to stop potential floating
int LEDSETUP2 = 0;
int LEDSETUP3 = 0;
int LEDSETUP4 = 0;
int LEDSETUP5 = 0;
int LEDSETUPFINAL; //wont ever float

void setup() {
  // put your setup code here, to run once:

  pinMode(Micro1, INPUT); //Microswitch 1
  pinMode(Micro2, INPUT); //Microswitch 2
  pinMode(LED1, OUTPUT); //LED1
  pinMode(LED2, OUTPUT); //LED2
  pinMode(LED3, OUTPUT); //LED3
  pinMode(LED4, OUTPUT); //LED4
  pinMode(LED5, OUTPUT); //LED5
  pinMode(LED6, OUTPUT); //LED6
  pinMode(LED7, OUTPUT); //LED7
  pinMode(LED8, OUTPUT); //LED8
}

void loop()
{
  while (ButtonPress1 == 0) //will send to next test area
    switch (ButtonPress2) //Different values of ButtonPress2 will move code to different cases
    {
      case 0:
        LedIfStatementLoop(); //Call LED Picker Code

        Micro2State = digitalRead(Micro2); //read the logic on pin associated with micoswitch
        if (Micro2State == LOW)  //if button has been pressed
        {
          delay(200); //break for contacts in switch to settle
          if (Micro2State == LOW) //verify button press
          { //press confirmed
            Serial.print("Module Confirmed");  //Serial message to show user unit is doing something
            Serial.println(LEDNum);  //Print which LED is on
            ++ButtonPress2; //Move to next case
            LEDSETUP1 = REG_PIOC_ODSR; //write port to variable
          }
        }
        Micro1StateCheck(); //Call up function to check user hasnt confirmed picking LED's
        break;

      case 1:
        LedIfStatementLoop();

        Micro2State = digitalRead(Micro2);
        if (Micro2State == LOW)
        {
          delay(200);
          if (Micro2State == LOW)
          {
            Serial.print("Module Confirmed");
            Serial.println(LEDNum);
            ++ButtonPress2;
            LEDSETUP2 = REG_PIOC_ODSR; //write port to a diff variable
          }
        }
        Micro1StateCheck(); //Call up function to check user hasnt confirmed picking LED's
        break;

      case 2:
        LedIfStatementLoop();

        Micro2State = digitalRead(Micro2);
        if (Micro2State == LOW)
        {
          delay(200);
          if (Micro2State == LOW)
          {
            Serial.print("Module Confirmed");
            Serial.println(LEDNum);
            ++ButtonPress2;
            LEDSETUP3 = REG_PIOC_ODSR;
          }
        }
        Micro1StateCheck(); //Call up function to check user hasnt confirmed picking LED's
        break;

      case 3:
        LedIfStatementLoop();

        Micro2State = digitalRead(Micro2);
        if (Micro2State == LOW)
        {
          delay(200);
          if (Micro2State == LOW)
          { //press confirmed
            Serial.print("Module Confirmed");
            Serial.println(LEDNum);
            ++ButtonPress2;
            LEDSETUP4 = REG_PIOC_ODSR;
          }
        }
        Micro1StateCheck(); //Call up function to check user hasnt confirmed picking LED's
        break;

      case 4:
        LedIfStatementLoop();

        Micro2State = digitalRead(Micro2);
        if (Micro2State == LOW)
        {
          delay(200);
          if (Micro2State == LOW)
          { //press confirmed
            Serial.print("Module Confirmed");
            Serial.println(LEDNum);
            ++ButtonPress2;
            LEDSETUP5 = REG_PIOC_ODSR;
          }
        }
        Micro1StateCheck(); //Call up function to check user hasnt confirmed picking LED's
        break;
    }
}


void LedIfStatementLoop()
{
  if (LEDNum == 1) //execute below statements if LEDNum equals 1, which it should by default
  {
    REG_PIOC_ODSR = 0x0C; //set bit 12 of port c high
    delay(2000); //gives user time to confirm choice
  }
  else if (1 < LEDNum && LEDNum < 8) //if LEDNum is bigger than 1 but smaller than 8
  {
    REG_PIOC_ODSR << 1;  //shift the bit up (so from bit 12 to 13... to 14... to 15 etc
    delay(2000); //gives user time to confirm choice
  }
  else
  {
    LEDNum = 0; //if none of the above criteria are met, LEDNum must be bigger than 8 and so reset the count to restart the loop
    delay(2000); //gives user time to confirm choice
  }
  ++LEDNum; //every loop increment LEDNum var by 1 to make sure loop doesn't execute same step over and over
}


void Micro1StateCheck()
{

  Micro1State = digitalRead(Micro1); //read the logic on pin associated with micoswitch
  if (Micro1State == LOW)  //if button has been pressed
  {
    delay(200); //break for contacts in switch to settle
    if (Micro1State == LOW) //verify button press
    { //press confirmed
      Serial.println("Moving into OR Section of code");  //Serial message to show user unit is doing something
      ButtonPress2 = 8; //Break switch cases
      LEDSETUPFINAL = LEDSETUP1 | LEDSETUP2 | LEDSETUP3 | LEDSETUP4 | LEDSETUP5;
      REG_PIOC_ODSR = LEDSETUPFINAL;
    }
  }
}

I am concerned that my code will get stuck within the first function I call? When I initially wrote it, I thought it would loop through that function once, come out and check the switch, and unless a button had be pressed, the program would stay trapped in the first switch case (This is what I want it to do!)

However, I can also see it being stuck in that first function ( LedIfStatement() ) because the code to break its parameter (a button being pressed) isn’t within that loop.

In other words, there is no code within that function, which would make the initial parameter not true, so the conditions for that loop would always be true, and so the programmer would step through all the lines in the function and circle back to the top, see the initial statement is still true, and go back into that loop again (This is not what I want to happen).

Which way of thinking is right?

It’s kinda tough to tell, since a lot of the code is missing. Before posting all of it, place your cursor in the IDE’s source code window and press Ctrl-T to reformat the code in a standard C style. After that, post your code using code tags, which now makes it possible for us to view/test the complete code.

First, post complete code that will actually compile. This won't in any case, because you are missing a semicolon:

  case 0:
  LedIfStatementLoop(Micro2 == HIGH) //Cycle function until a button is pressed

  Micro2State = digitalRead(Micro2); //read the logic on pin associated with micoswitch

econjack: It's kinda tough to tell, since a lot of the code is missing. Before posting all of it, place your cursor in the IDE's source code window and press Ctrl-T to reformat the code in a standard C style. After that, post your code using code tags, which now makes it possible for us to view/test the complete code.

Hi, have updated the code so its all there and got it to compile on my computer. I am not sure if the port manipulation code is correct, I cannot use PORTC as I am using a Due and therefore that command is not recognised.

jremington: First, post complete code that will actually compile. This won't in any case, because you are missing a semicolon:

  case 0:
  LedIfStatementLoop(Micro2 == HIGH) //Cycle function until a button is pressed

  Micro2State = digitalRead(Micro2); //read the logic on pin associated with micoswitch

Have edited my original code so it will compile!