Complete Guide How to Working with Functions using Arduino

In this project, I will show you a Complete Guide on How to Working with Functions using Arduino step by step complete process.

You’ll see new ways to make your Arduino sketches easier to read and simpler to build by building your own functions. You can also build a modular, reusable code that will save you time again and again. We’ll present a method to create decisions that control blocks of code, and you’ll see about a type of integer variable called the long. Then you will apply your own functions to create a new type of thermometer.

A function consists of a set of instructions that we can utilize everywhere in our sketches. Although many functions are possiblein the Arduino language, seldom you won’t find one to suit your particular requirements—or you may want to run part of a sketch repeatedly to make it work, which is a waste of memory. In both of these conditions, you might wish you had a better function to do what you want to do. The good news is that there is such a function—the one you build yourself.


Example 1: Creating a Function to Repeat an Action


You can write easy functions to repeat actions on demand. For example, the following function will turn the built-in LED on (at 1 and 3) and off (at 2 and 4) twice.

  void blinkLED()
  {
1     digitalWrite(13, HIGH);
      delay(1000);
2     digitalWrite(13, LOW);
      delay(1000);
3     digitalWrite(13, HIGH);
      delay(1000);
4     digitalWrite(13, LOW);
      delay(1000);
  }

Here is the function being utilized within a complete sketch, which you can upload to the Arduino:

 #define LED 13
  #define del 200

  void setup()
  {
    pinMode(LED, OUTPUT);
  }

  void blinkLED()
  {
    digitalWrite(LED, HIGH);
    delay(del);
    digitalWrite(LED, LOW);
    delay(del);
    digitalWrite(LED, HIGH);
    delay(del);
    digitalWrite(LED, LOW);
    delay(del);
  }

  void loop()
  {
1   blinkLED();
    delay(1000);
  }

At the blinking LED() function is named in void loop() at 1, the Arduino will run the commands within the void blinkLED() section. In other words, you have built your own function and utilized it if necessary.


Example 2: Creating a Function to Set the Number of Blinks


The function we just built is pretty limited. What if we need to set the number of blinks and the delay? No problem; we can build a function that lets us change values, like this:

void blinkLED(int cycles, int del)
{
  for ( int z = 0 ; z < cycles ; z++ )
  {
    digitalWrite(LED, HIGH);
    delay(del);
    digitalWrite(LED, LOW);
    delay(del);
  }
}

In void blinkLED() function takes two integer values cycles and del. So if we required to blink the LED 12 times with a 100-millisecond delay, when we would utilize blinkLED(12, 100). Join the following sketch into the IDE to test with this function:

 #define LED 13

  void setup()
  {
    pinMode(LED, OUTPUT);
  }

  void blinkLED(int cycles, int del)
  {
    for ( int z = 0 ; z < cycles ; z++ )
    {
      digitalWrite(LED, HIGH);
      delay(del);
      digitalWrite(LED, LOW);
      delay(del);
    }
  }

  void loop()
  {
1   blinkLED(12, 100);
    delay(1000);
  }

You can view at 1 that the values of 12 and 100 are passed into our use function blinkLED(), where cycles will have a value of 12 and del will have a value of 100.  the LED will blink 12 times with a delay of 100 milliseconds within blinks.


How to Creating a Function to Return a Value in Working Functions using Arduino


 In To building functions that take values entered as parameters (as void blinkLED() ), you can also make functions that return a value, in the same approach, that analogRead() returns a value within 0 and 1,023 if measuring an analog input, . The void that looks at the start of functions up to this point means that the function doesn’t return anything—that is, the function’s return value is void. Let’s make some utilize functions that return actual values.

Study this function that changes degrees Celsius to Fahrenheit:

float convertTemp(float celsius)
{
  float fahrenheit = 0;
  fahrenheit = (1.8 * celsius) + 32;
  return fahrenheit;
}

In the opening line, we set the function name (convertTemp), its return variable type (float), and any variables that we might want to pass into the function (float celsius). To utilize this function, we send it an existing variable. For example, if we needed to convert 40 degrees Celsius to Fahrenheit and store the result in a float variable named tempf, when we would call convertTemp like so:

tempf = convertTemp(40);

This would set 40 into the convertTemp variable celsius and utilize it in the calculation fahrenheit = (1.8 * celsius) + 32 in the convertTemp function. The result is then returned into the variable tempf with the convertTemp line return fahrenheit.

All Arduino tutorial available Click here

 ARDUINO TUTORIAL 

 25 total views,  1 views today