button

My program summary is I want to set some "ifs" in my program and I have 9 inputs and 3 outputs(the count of I/Os can change) but I have trouble in my program and there is the code:

char cntlstate2; //I set character variables there
char cntlstate3;
char mc11;
char mc12;
char mc31;
char mc32;
char mc21;
char mc22;
void setup() {
  // put your setup code here, to run once:
pinMode(2, INPUT); //I setup pin I want to take action there
pinMode(3, INPUT);
pinMode(4,INPUT);
pinMode(5, INPUT);
pinMode(6, INPUT);
pinMode(7, INPUT);
pinMode(8, INPUT);
pinMode(9, INPUT);
pinMode(10, INPUT);
pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop() { 
  // put your main code here, to run repeatedly:
  /*they are namespace of microswiches mcs are microswiches
   * and cntlstates are for tasks from user
   */
cntlstate1 = digitalRead(8); //I set data for variables there
cntlstate2 = digitalRead(9);
cntlstate3 = digitalRead(10);
mc11 = digitalRead(2);
mc12 = digitalRead(3);
mc21 = digitalRead(4);
mc22 = digitalRead(5);
mc31 = digitalRead(6);
mc32 = digitalRead(7);
//this statement is for M1

   if (mc22 == HIGH && mc32 == HIGH && cntlstate1 == HIGH) { //I put the maincode there
    digitalWrite(11, HIGH);
  } else {
    digitalWrite(11, LOW);  
  }
  //this statment is for M2
    if (mc11 == HIGH && mc32 == HIGH && cntlstate2 == HIGH) {
      digitalWrite(0, HIGH);
    } else {
      digitalWrite(0, LOW);
  }
  //this statment is for M3
    if (mc12 == HIGH && cntlstate3 == HIGH) {
      digitalWrite(1, HIGH);
    }      else {
       digitalWrite(1,LOW);
      }
    
  }

and the trouble is the function in loops do not doing anything

thank for replying:D

I have trouble in my program

What trouble ?

Look at your code as posted. It has a smiley in it. Please read the sticky at the top of this forum that explains how to post a programming question.

You might also want to check your 3 while statements and ask yourself if you enter in one of those (if the condition in the while (condition) is true) how are you going to exit that while. (Hint - at the moment seems you'll be stuck in that loop forever)

Only you know the trouble, so please explain it. As Bob suggested use code tag.

while (cntlstate1 == HIGH) {
   if (mc22 == HIGH && mc32 == HIGH) {
      digitalWrite(11, HIGH);
   } else {
      digitalWrite(11, LOW);
    } 
  }

I guess, when ctlstate1 is HIGH, the code execution will hang in the while loop and might not execute the second while loop.

loop() do the looping, so why another while loop?

If you replace the while statement with a if statement, that might work.

you know guys I do it I mean I write my loops like this:

if (mc32 == HIGH && mc 22 == HIGH && cntlstate1 == HIGH) {
digitalWrite(11, HIGH);
}

but I have still problem
but I do not test this:

while (mc32 == HIGH && mc 22 == HIGH && cntlstate1 == HIGH) {
digitalWrite(11, HIGH);
}

elvenstof you mean I write code like this:

if (cntlstate1 == HIGH) {
if (mc22 == HIGH && mc32 == HIGH) {
digitalWrite(11, HIGH);
}

?
thank for repling:D

I test anythings you said but my problem do not solved.
thank for replying:D

See answer #1 please take action

I know but I don,t know why this get like that anyway the smiley face is 8 in program.
thank for replying:D

if(cntlstate1 == HIGH)
  digitalWrite(11, mc22 == HIGH && mc32 == HIGH ? HIGH : LOW);
if(cntlstate2 == HIGH)
  digitalWrite(0, mc11 == HIGH && mc32 == HIGH? HIGH : LOW);
if(cntlstate3 == HIGH)
  digitalWrite(1, mc12 == HIGH? HIGH : LOW);

arygholo:
I know but I don,t know why this get like that anyway the smiley face is 8 in program.

arygholo:
elvenstof you mean I write code like this:

if (cntlstate1 == HIGH) {

if (mc22 == HIGH && mc32 == HIGH) {
digitalWrite(11, HIGH);
}


?
thank for repling:D

In :

While (cntlstate1 == HIGH) {

if (mc22 == HIGH && mc32 == HIGH) {
digitalWrite(11, HIGH);
}




Te program will check if cntstate1 == high, if that is true it will check if mc22 and mc23 are true. If that is true it will set pin 11 to high.
But after that the program will check if cntlstate1 == high again and if it is it will check mc22 and mc23 again and set pin 11 to high again.

So unless pin 11 going high makes cntlstate1 (pin 8 I guess) become low. The code will stay in an infinite loop.

What do you want your code to do?

Please correct your first post above and add code tags around your code. [code] // your code here [/code]. it should look like this:// your code here

then the smiley will disappear

and I programming console apps too and I,m noob in programming. and I have this problem in console apps too

Thanks for replying:D

Hi,
Can you post a table of how you want you 9 inputs to control your 3 outputs?

Thanks.. Tom.. :slight_smile: