Problem with function

Hello i’m doing a school project that is supposed to print data to the system monitor and based on that data do different things. The device has 17 buttons attached to it. I want to be able to press 9 buttons in a row in any order and after every press i would like to have the numbers printed out to the serial monitor. The problem is that it doesn’t want to execute the code in the function. It only gets to the point where it prints out “in the function”.

Here is the code

#include <VirtualWire.h>

const int output1 = 38;
const int output2 = 40;
const int output3 = 42;
const int output4 = 44;
const int output5 = 46;
const int output6 = 48;
const int output7 = 50;
const int output8 = 52;
const int output9 = 23;
const int output10 = 25;
const int output11 = 27;
const int output12 = 29;
const int output13 = 31;
const int output14 = 33;
const int output15 = 35;
const int output16 = 53;
const int output17 = 39;

int check_output1 = 0;
int check_output2 = 0;
int check_output3= 0;
int check_output4 = 0;
int check_output5 = 0;
int check_output6 = 0;
int check_output7 = 0;
int check_output8 = 0;
int check_output9 = 0;
int check_output10 = 0;
int check_output11 = 0;
int check_output12 = 0;
int check_output13 = 0;
int check_output14 = 0;
int check_output15 = 0;
int check_output16 = 0;
int check_output17 = 0;

void setup()
{
Serial.begin(9600);
pinMode(output1, INPUT);
pinMode(output2, INPUT);
pinMode(output3, INPUT);
pinMode(output4, INPUT);
pinMode(output5, INPUT);
pinMode(output6, INPUT);
pinMode(output7, INPUT);
pinMode(output8, INPUT);
pinMode(output9, INPUT);
pinMode(output10, INPUT);
pinMode(output11, INPUT);
pinMode(output12, INPUT);
pinMode(output13, INPUT);
pinMode(output14, INPUT);
pinMode(output15, INPUT);
pinMode(output16, INPUT); 
pinMode(output17, INPUT);   
}


void loop()
{
  check_output17=digitalRead(output17);
  if(check_output17 == HIGH){
       Serial.println("going into function");
   number();
       }
    }
 
  void number(void)  
  { 
    check_output1=digitalRead(output1);
  check_output2=digitalRead(output2);
  check_output3=digitalRead(output3);
  check_output4=digitalRead(output4);
  check_output5=digitalRead(output5);
  check_output6=digitalRead(output6);
  check_output7=digitalRead(output7);
  check_output8=digitalRead(output8);
  check_output9=digitalRead(output9);
  check_output10=digitalRead(output10);
  check_output11=digitalRead(output11);
  check_output12=digitalRead(output12);
  check_output13=digitalRead(output13);
  check_output14=digitalRead(output14);
  check_output15=digitalRead(output15);
  check_output16=digitalRead(output16);
  
    Serial.println("in the function");
    for(int i = 0; i < 9; i++){
  
      back:
      
    if (check_output1 == HIGH){
      Serial.println("1 ");
      }

    else if (check_output2 == HIGH){
      Serial.println("2 ");
       }

    else if (check_output3 == HIGH){
      Serial.println("3 ");
       }

    else if (check_output4 == HIGH){
      Serial.println("A ");
       }

    else if (check_output5 == HIGH){
      Serial.println("4 ");
       }

    else if (check_output6 == HIGH){
      Serial.println("5 ");
       }

    else if (check_output7 == HIGH){
      Serial.println("6 ");
       }

    else if (check_output8 == HIGH){
      Serial.println("B ");
       }

    else if (check_output9 == HIGH){
      Serial.println("7 ");
       }

    else if (check_output10 == HIGH){
      Serial.println("8 ");
       }

    else if (check_output11 == HIGH){
      Serial.println("9 ");
       }

    else if (check_output12 == HIGH){
      Serial.println("C ");
       }

    else if (check_output13 == HIGH){
      Serial.println("* ");
       }

    else if (check_output14 == HIGH){
      Serial.println("0 ");
       }

    else if (check_output15 == HIGH){
      Serial.println("# ");
       }

    else if (check_output16 == HIGH){
      Serial.println("D ");
       }
    else{
      goto back; }
    
    }

 }

however when i delete the function and put the code from the deleted function into void loop it somehow works

void loop()
{

check_output1=digitalRead(output1);
  check_output2=digitalRead(output2);
  check_output3=digitalRead(output3);
  check_output4=digitalRead(output4);
  check_output5=digitalRead(output5);
  check_output6=digitalRead(output6);
  check_output7=digitalRead(output7);
  check_output8=digitalRead(output8);
  check_output9=digitalRead(output9);
  check_output10=digitalRead(output10);
  check_output11=digitalRead(output11);
  check_output12=digitalRead(output12);
  check_output13=digitalRead(output13);
  check_output14=digitalRead(output14);
  check_output15=digitalRead(output15);
  check_output16=digitalRead(output16);


  check_output17=digitalRead(output17);
  if(check_output17 == HIGH){
       Serial.println("in function");
       delay(1000);
       }

   if (check_output1 == HIGH){
      Serial.println("1 ");      
      delay(500);}

    else if (check_output2 == HIGH){
      Serial.println("2 ");
      delay(500); }

    else if (check_output3 == HIGH){
      Serial.println("3 ");

      delay(500); }

    else if (check_output4 == HIGH){
      Serial.println("A ");
      delay(500); }

    else if (check_output5 == HIGH){
      Serial.println("4 ");
      delay(500); }

    else if (check_output6 == HIGH){
      Serial.println("5 ");
      delay(500); }

    else if (check_output7 == HIGH){
      Serial.println("6 ");
      delay(500); }

    else if (check_output8 == HIGH){
      Serial.println("B ");
      delay(500); }

    else if (check_output9 == HIGH){
      Serial.println("7 ");
      delay(500); }

    else if (check_output10 == HIGH){
      Serial.println("8 ");
      delay(500); }

    else if (check_output11 == HIGH){
      Serial.println("9 ");
      delay(500);}

    else if (check_output12 == HIGH){
      Serial.println("C ");
      delay(500); }

    else if (check_output13 == HIGH){
      Serial.println("* ");
      delay(500); }

    else if (check_output14 == HIGH){
      Serial.println("0 ");
      delay(500); }

    else if (check_output15 == HIGH){
      Serial.println("# ");
      delay(500); }

    else if (check_output16 == HIGH){
      Serial.println("D ");
      delay(500); }
}

Can anyone explain why the first way doesn’t work and tell me how to fix it?
The reason i want to have a separate function for this is because i plan on expanding the code to do different things. Keep in mind i am a beginner so if manageable please explain in the easiest way possible.

Thank You for your time :slight_smile:

Using "goto" is going to get comments from contributors, hurry, delete it :wink:
.

The key is don't try to capture all 9 of your button presses in one pass of a function. Call your function and if there is a button press record that in a variable or array and exit and call your function again the next time loop runs. If there is no button press then just exit and wait until the next time loop runs. That's the purpose of the loop function repeating over and over, so you don't have to do things all at once.

Also learn to use for loops, it will greatly reduce your code size :slight_smile:

Not to make you feel bad but using generous amounts of white space, arrays, loops, zero-terminated C string and one subroutine I've got this down to 42 lines of code. Of course thats without debouncing the buttons, which I suppose in this case could be the addition of a simple delay.

I'll give you the subroutine ...

// returns 0 for no button pressed, 1 ... 17 otherwise ...
uint8_t checkButtons()
{
    for ( size_t i = NUM_ENTRIES(pinsButtons); i--; )
    {
        if ( BUTTON_DOWN == digitalRead(pinsButtons[i]) )
        {
            return  ++i;
        }
    }

    return 0;
}
pinMode(output1, INPUT);

It’s pretty silly to name an input pin using output in the name.

How ARE the switches wired?

I am aware of that but i did it because those pins output data to the serial monitor.
Looking back it is a bit silly :slight_smile: . Thanks for the tip. However the buttons are wired correctly because they work in the loop function.

Thank you all for your help i got it working the way i wanted it :slight_smile:

Suppose that you only had two switches. Your function, properly indented, with { in the proper places, would contain this:

for(int i = 0; i < 9; i++)
{
back:

  if (check_output1 == HIGH)
  {
    Serial.println("1 ");
  }

  else if (check_output2 == HIGH)
  {
    Serial.println("2 ");
  }

  else
  {
    goto back; 
  }
}

Now, suppose that neither switch is pressed when the function is called. Think about what this is doing. The if statement is not true, so the else if is checked. It is not true, either, so the else is executed, and control goes back to the label. Control never reaches the end of the for loop, so i is never incremented, so the for loop never ends. Since the for loop never ends, the function never ends, so it can’t be called again, so pressing a switch won’t do any good.

Now, suppose you get rid of the label and the goto statement (and you WILL!).

Explain WHY the for loop even exists. If no switch is pressed, looping 9 times looking at the same data doesn’t make sense. If a switch IS pressed, printing the switch number 9 times doesn’t make sense.