Please help me turn this function to library

int btn_selector(){
  btn = 0;                              
  if(digitalRead(sw1_pin) == LOW)
    btn = 1;                              
  else 
    if(digitalRead(sw2_pin) == LOW)
    btn = 2;                              
  else
    if(digitalRead(sw3_pin) == LOW)
      btn = 3;                              
    else
      if(digitalRead(sw4_pin) == LOW)
        btn = 4;

  if (millis()- last_btn_debounce >btn_debounce) {
    last_btn_debounce = millis();
    return btn;                                                                             
  }
  else
    return 0;                                                                              
}

Why do you want to put that in a library? What problems are you having do so?

Or, should your post title have been "Do this for me"?

Hi! Paul

I found difficult to turn all my function into library. I browse the web and learn how to do it. but i found it is so complicated. I would like someone can show me what is in the library or though the function the post above and turn it to library to let me learn how to do it.

Thank you!

i am a very polite person. but English is not my language. sorry for being rude here all the time. I never mean it.

An Arduino library consists of two files - a header file, where function prototypes or classes are defined, and a source file, where functions or class methods are implemented.

You have a function. Cut the function, and paste it into a file with a .cpp extension. Create a header file with the name base name as the cpp file, with the .h extension. Put the function prototype in the header file.

In the sketch, include the header file. Call the function just like it lived in the sketch.

When that works, you can create a folder in the libraries folder of the sketch directory, with the same name as the header and source file (without the extensions), and move the header and source file into that directory.

Then, all you need to do is change the include statement. Change " to < and >, or < and > to ", whichever is needed to include the sketch from the sketch directory and the library directory.

:stuck_out_tongue: sorry for asking and asking all the time!

Mr Paul. i quite understand what you was mention above.

the most complicated thing for me is how can i assign more than one constant pin in .h file and what to do if i only have one function in the ccp file?

Would you pls covert the function to library i post above ? and let me easier to understand how to do it.

Master Paul , please help me!

Create a file, say library_name.cpp, with this

int btn_selector(){
  btn = 0;                              
  if(digitalRead(sw1_pin) == LOW)
    btn = 1;                              
  else 
    if(digitalRead(sw2_pin) == LOW)
    btn = 2;                              
  else
    if(digitalRead(sw3_pin) == LOW)
      btn = 3;                              
    else
      if(digitalRead(sw4_pin) == LOW)
        btn = 4;

  if (millis()- last_btn_debounce >btn_debounce) {
    last_btn_debounce = millis();
    return btn;                                                                             
  }
  else
    return 0;                                                                              
}

and a file, say library_name.h, with this

int btn_selector();

Place both files in the library/library_name path of your sketch sub-directory.

in the arduino offical tutorial

the .h file .cpp file is more complicate than that.

is it simple like this?

in the arduino offical tutorial

the .h file .cpp file is more complicate than that.

Because the example is creating a class (and uses #ifdef/#define/#endif to prevent multiple inclusions of the file).

You will need more in the header file than just the function prototype, since the function uses global variables. You really should revise the function to not do that. Make the pin numbers arguments, not global variables.

how to Make the pin numbers arguments, not global variables?

please give me an example. i have no idea what is global variables and pin number arguments

i have no idea what is global variables and pin number arguments

Perhaps it's time to read some tutorials.

how to Make the pin numbers arguments, not global variables?

This function takes the 4 pin numbers as argument, along with the last debounce time and the debounce time. It returns a local variable for the button number, avoiding the use of the 7 global variables that your function uses.

int btn_selector(int pin1, int pin2, int pin3, int pin4, unsigned long &lastBounceTime, unsigned long bounceTime)
{
  int btn = 0;                              
  if(digitalRead(pin1) == LOW)
    btn = 1;                              
  else 
    if(digitalRead(pin2) == LOW)
    btn = 2;                              
  else
    if(digitalRead(pin3) == LOW)
      btn = 3;                              
    else
      if(digitalRead(pin4) == LOW)
        btn = 4;

  if (millis() - lastBounceTime >bounceTime) {
    lastBounceTime = millis();
    return btn;                                                                             
  }
  else
    return 0;                                                                              
}

Global variables are those defined outside of any function.

You'd call this function like so:

btn = btn_selector(sw1_pin, sw2_pin, sw3_pin, sw4_pin, last_btn_debounce, btn_debounce);

passing it the global variables, and populating the global variable with the return value.

thank you i give it a try.