help with functions

Hello, I am new to programming and I am currently writing a code for a midi controller which I would like to make as reusable as possible.
I have started by writing a code for my mode selectors. Basically pushbuttons which can be configured to trigger 2 or 3 modes each, each connected to its own led which is off, on or blinking.
I wrote the code using arrays and the code appears to be working ok. Now I would like to make a function of this code so that I can reuse this in future and make the code a bit cleaner. However I am getting all sort of errors. I presume it should be a return function as it needs to return the modes type and values (i;e mode A1, A2 or A3; B1 B2 or B3, etc. but after viewing many tutorials I am still not getting there. I am clearly missing something. I have attached my working code.
Any help would be really appreciated please.
Thanks.

code.txt (4.04 KB)

I don't know anything about midi or music (apart from how to play a CD).

The use of functions is illustrated in Planning and Implementing a Program

...R

Thank you both.
I am going to have a look at this thread and hopefully I will clarify the matter. In the meantime I have done what Delta G suggested. formatted the code better. I am going to post it now Hopefully in the right way.

I have been able to compile the function properly if I make it a void function but when I test it I gets weird result this is what happen when I run the 3 tests I have made (which you can see commented in the code):

if I run test 2 (which is inside the function itself) the code runs perfectly - but when I RUN test 2 (in void loop) I only get MODE 2 - TYPE 0; no matter what button I press.

Even more strangely when I uncomment test 3 i get an error message ('modeselection' was not declared in this scope) and I cannot compile. I find this quite weird.

const int modeType = 2;                                       // * how many modes types (buttons and pins)
const int modesNumber[modeType] = {3, 2};                     // * how many modes for each type
const int modeLedPin[modeType] = {12, 13};                    // * which led pins are used
const int modeButton[modeType] = {2, 4};                      // * which button pins are used

int modeLedState[modeType] = {0};                             // ledState used to set the LED - only for mode selection with led blink indicator
int modeButtonState[modeType];                                // current state of the Mode Button
int modeButtonPState[modeType] = {1};                         // previous state of the Mode Button,initialise it to HIGH
int mode[modeType] = {1, 1};                                  // define mode variable and initialise it to 1
int i;

// blinking timing
unsigned long previousMillis [modeType]  = {0};               // will store last time LED was updated - only for mode selection with led blink indicator
const long interval = 500;                                    // interval at which to blink (milliseconds) - only for mode selection with led blink indicator

// DEBOUNCE VARIABLE
unsigned long lastDebounceTimeMB[modeType] = {0};             // the last time the mode button button was pressed
unsigned long debounceDelay = 20;                             // the debounce time; increase if the output flickers


void setup() {
  for (i = 0; i < modeType; i++) {
    pinMode(modeLedPin[i], OUTPUT);                           // set the digital pin for led as output
    pinMode(modeButton[i], INPUT_PULLUP);                     // set the digital pin for ModeButtoSerial.begin (9600);begin (9600);
    Serial.begin(9600);
  }
}


void loop() {
  modeSelection();

  // TEST 2
  //          Serial.print("MODE ");                              // Print MODE TYPE AND MODE NUMBER
  //          Serial.print(i);                                    // Print MODE TYPE AND MODE NUMBER
  //          Serial.print(" TYPE ");                             // Print MODE TYPE AND MODE NUMBER
  //          Serial.println(mode[i]);                            // Print MODE TYPE AND MODE NUMBER

  // TEST 3

  //    if ((i == 0)&&(mode[i]) == 1){Serial.print("MODE A1");
  //    if ((i == 0)&&(mode[i]) == 2){Serial.print("MODE A2");
  //    if ((i == 0)&&(mode[i]) == 3){Serial.print("MODE A3");
  //    if ((i == 1)&&(mode[i]) == 1){Serial.print("MODE B1");
  //    if ((i == 1)&&(mode[i]) == 2){Serial.print("MODE B2");
  //    if ((i == 1)&&(mode[i]) == 3){Serial.print("MODE B3");
}


void modeSelection() {
  for (i = 0; i < modeType; i++) {
    modeButtonState[i] = digitalRead(modeButton[i]);          // check state of mode button and what happen when mode button is pressed
    if ((millis() - lastDebounceTimeMB[i]) > debounceDelay)   // DEBOUNCE FUNCTION FOR MODE BUTTON
    { if (modeButtonState[i] != modeButtonPState[i])          // check that button state is different from previous button state
      { lastDebounceTimeMB[i] = millis();                     // update debounce time
        if (modeButtonState[i] == LOW)                        // if button is pressed increase mode
        { mode[i] ++;
          if (mode[i] > modesNumber[i])                       // if mode bigger than number of modes available
          {
            mode[i] = 1;                                      // bring back mode to initial value
          }
          //TEST 1
          //          Serial.print("MODE ");                              // Print MODE TYPE AND MODE NUMBER
          //          Serial.print(i);                                    // Print MODE TYPE AND MODE NUMBER
          //          Serial.print(" TYPE ");                             // Print MODE TYPE AND MODE NUMBER
          //          Serial.println(mode[i]);                            // Print MODE TYPE AND MODE NUMBER
        }
        modeButtonPState[i] = modeButtonState[i];             // update mode button state
      }
    }
    switch (mode[i]) {
      case 1:                                                 // SWITCH LED OFF
        digitalWrite(modeLedPin[i], LOW);
        break;
      case 2:                                                 // SWITCH LED ON
        digitalWrite(modeLedPin[i], HIGH);
        break;
      case 3:                                                 // LED BLINKING
        { unsigned long currentMillis = millis();
          if (currentMillis - previousMillis[i] >= interval)  // check that intervall has passed
          { previousMillis[i] = currentMillis;                // save the last time you blinked the LED
            if (modeLedState[i] == LOW) {
              modeLedState[i] = HIGH;
            }
            else {
              modeLedState[i] = LOW;
            }
            digitalWrite(modeLedPin[i], modeLedState[i]);    // set the LED with the ledState of the variable:
          }
        }
        break;
    }
  }
}

fluxia:
Even more strangely when I uncomment test 3 i get an error message ('modeselection' was not declared in this scope) and I cannot compile. I find this quite weird.

There is nothing strange about that. You don't have matched braces {}

If you write your code with one statement on each line that sort of problem is more obvious. Rather than

if ((i == 0)&&(mode[i]) == 1){Serial.print("MODE A1");

do it like this

if ((i == 0)&&(mode[i]) == 1) { 
     Serial.print("MODE A1");
}  // in the one line version this brace is missing

...R

Thank you Robin, I have read your Planning and Implementing an Arduino Program and I like the way you organise your coding and the style of your command.
I have corrected the syntax silly mistake but I am still having problems in outputting the modes selected - it's probably taking me longer as I wanted to use arrays, and I am still familiarising with things tomorrow I will give it another shot and try to find where I am going wrong.

Thanks !

I managed!! :slight_smile:

There were quite a few problems in the code. I reviewed all the variables and created two functions, one for mode selection and one for printing (to test it out).
It seems to be running smoothly.
I will attach the final file for who people who may find it useful.
It's basically to create buttons to select mode functions (I will use it for a midi controller).
It can have as many buttons as you like and 2 or 3 modes to be selected for each button (the correspondent led will indicate the mode: off for mode 1, on for mode 2 and blinking for mode 3).
Thanks for the advice ! :wink:

Cheers

A

const int numberOfTypes = 2;                                 // * how many modes types (buttons and pins)
const int numberOfModes[numberOfTypes] = {3, 2};             // * how many modes for each type
char typeLetter[] = "AB";                                    // * letters assigned to mode types
const int modeLedPin[numberOfTypes] = {11, 12};              // * which led pins are used
const int modeButton[numberOfTypes] = {2, 4};                // * which button pins are used

int modeLedState[numberOfTypes] = {0};                       // ledState used to set the LED - only for mode selection with led blink indicator
int modeButtonState[numberOfTypes];                          // current state of the Mode Button
int modeButtonPState[numberOfTypes] = {1};                   // previous state of the Mode Button,initialise it to HIGH

int modeNumb[numberOfTypes] = {1, 1};                        // define mode-numbers variable and initialise it to mode number "1" for all types

// BLINKING TIMING================================================

unsigned long previousMillis [numberOfTypes]  = {0};         // will store last time LED was updated - only for mode selection with led blink indicator
const long interval = 500;                                   // interval at which to blink (milliseconds) - only for mode selection with led blink indicator
unsigned long lastPrint = 0;
// DEBOUNCE VARIABLE==============================================

unsigned long lastDebounceTimeMB[numberOfTypes] = {0};       // the last time the mode button button was pressed
unsigned long debounceDelay = 20;                            // the debounce time; increase if the output flickers

// SETUP =========================================================

void setup() {
  for (int i = 0; i < numberOfTypes; i++)  {
    pinMode(modeLedPin[i], OUTPUT);                          // set the digital pin for led as output
    pinMode(modeButton[i], INPUT_PULLUP);                    // set the digital pin for ModeButtoSerial.begin (9600);begin (9600);
    Serial.begin(9600);
    Serial.print("MODE TYPE: ");
    Serial.print(typeLetter[i]);
    Serial.print(" MODE NUMBER: ");
    Serial.println(modeNumb[i]);
  }
}

void loop() {
  modeSelection();
  printFunction();
}

void modeSelection() {
  int i;
  unsigned long currentMillis = millis();

  for (i = 0; i < numberOfTypes; i++) {
    modeButtonState[i] = digitalRead(modeButton[i]);                  // read the state of mode button
    if ((currentMillis - lastDebounceTimeMB[i]) > debounceDelay) {    // DEBOUNCE FUNCTION FOR MODE BUTTON
      if (modeButtonState[i] != modeButtonPState[i]) {                 // check that button state is different from its previous state
        lastDebounceTimeMB[i] = currentMillis;                        // update debounce time
        if (modeButtonState[i] == LOW) {                               // if button is pressed
          modeNumb[i] ++;                                              //increase mode number for that mode type
          if (modeNumb[i] > numberOfModes[i]) {                        // if number bigger than number of modes available
            modeNumb[i] = 1;                                          // bring back mode number to initial value
          }
        }
        modeButtonPState[i] = modeButtonState[i];                     // update mode button state
      }
    }
    switch (modeNumb[i]) {
      case 1:                                                         // SWITCH LED OFF
        digitalWrite(modeLedPin[i], LOW);
        break;
      case 2:                                                         // SWITCH LED ON
        digitalWrite(modeLedPin[i], HIGH);
        break;
      case 3: {                                                        // LED BLINKING
          if (currentMillis - previousMillis[i] >= interval) {         // check that intervall has passed
            previousMillis[i] = currentMillis;                        // save the last time you blinked the LED
            if (modeLedState[i] == LOW) {
              modeLedState[i] = HIGH;
            }
            else {
              modeLedState[i] = LOW;
            }
            digitalWrite(modeLedPin[i], modeLedState[i]);             // set the LED with the ledState of the variable:
          }
        }
        break;
    }
  }
}
void printFunction() {
  unsigned long currentTime = millis();
  if ((currentTime - lastPrint) > 2000) {
    lastPrint = currentTime;
    for (int i = 0; i < numberOfTypes; i++) {
      Serial.print("MODE TYPE: ");
      Serial.print(typeLetter[i]);
      Serial.print(" MODE NUMBER: ");
      Serial.println(modeNumb[i]);
    }
  }
}