By creating a function, we can reduce the amount of code repeatedly. Thus, it could reduce the number of lines of code and accelerate the creation of the program. Previously we had often used the built-in function Arduino, eg delay function (), tone (), noTone (), and pinMode (), digitalWrite (), analogWrite (), and analogRead ().
When creating a function, we must specify the type of the function. Fingsi types can refer to the type of data that already exist, such as int, float, long, char, and so on. Each function should have a value that is returned to the function. mean this, if we make the function of type int (integer), means that the function has a number, the function must have an integer value. If not, then it would be an error. Similarly, when we make a variable, then the variable must exist (valuenya). If no valuenya, will ya error as well. The function is the same with the variables, except the function of type void.
The function of type void does not have a return value, means that the function only carry out orders without own value as a function of type char, int, float, and so on. To provide a return value for what one function, it can add a return command. Note that both of the following functions:
// Function of type integer int customDelay(int x1, int x2){ int myDelay = x1 * x2; return myDelay; } // Function of type void void nyalakan_LED(int pin, int DELAY){ digitalWrite(pin, HIGH); int timeDelay = customDelay(2, DELAY); delay(timeDelay); }
CustomDelay function () integer and has 2 pieces of integer as an input parameter. Value of customDelay is the product of these two parameters are taken from the variable myDelay (return myDelay). Variable to return into value function must have the same data type to the type of the function. In the above example, the function of integer and myDelay customDelay is also an integer.
LED turn on the function () is a function of type void so that the function it does not have value, this function simply executes the commands in it. This function has two parameters, namely the pin and DELAY equally integer. In these functions there is a command to turn on the LED on pin pin, then the value timeDelay taken from the multiplication result in customDelay function (). Note, the value of timeDelay taken from customDelay function (). Most importantly, the function need not return void.
Sketch 1 Doremi program with function
Sketch 1 Doremi program with function
// Free Tutorial Arduino // www.ioisalman.com // Scales C #define NOTE_C4 262 // DO #define NOTE_D4 294 // RE #define NOTE_E4 330 // MI #define NOTE_F4 349 // FA #define NOTE_G4 392 // SOL #define NOTE_A4 440 // LA #define NOTE_B4 494 // SI #define NOTE_C5 523 // DO // speaker is on pin 9 const int pinSpeaker = 9; void setup() { pinMode(pinSpeaker, OUTPUT); } void loop() { nada(NOTE_C4, 500); nada(NOTE_D4, 500); nada(NOTE_E4, 500); nada(NOTE_F4, 500); nada(NOTE_G4, 500); nada(NOTE_A4, 500); nada(NOTE_B4, 500); nada(NOTE_C5, 500); noTone(pinSpeaker); delay(1000); } void tone (frequency int, int length) { tone (pinSpeaker, frequency, duration); delay (duration); }
In Sketch 1, the program code more we simplify by creating a tone function (). So we do not need to call the function repeatedly delay. Might initially be somewhat more difficult to understand because the code is not sequential. But the model of the above programs is easier when we make an application with the amount of data NOTE even more if using Sketch Previous.
Sketch 1 has a number of variations of only 8 tones, could you imagine if music would you create has a tone number tens or hundreds, would roughly along what your program?
Therefore, Sketch 1 should be made more flexible. How? We can use the array as we learned when making LED, do you still remember? I have 2 examples of a song that could be a picture that as the tone increased, the complexity will become more challenging.
Note to Sketch Sketch 2 and 3, you have to create pitches.h file or download it first from the link that I have given in the preceding pages. Note on Sketch 2 on the second row, there is no command #include <pitches.h>, so you should have a file pitches.h to run the application.
Sketch 2 Program Twinkle-twinkle
// Free Tutorial Arduino // www.ioisalman.com// Free Tutorial Arduino // www.ioisalman.com // Reference scales #include <pitches.h> // Speaker is on pin 9 const int pinSpeaker = 9; #define TOTAL_TONE 15 const int daftar_nada [TOTAL_TONE] = { NOTE_C4, NOTE_C4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_A4, NOTE_G4, NOTE_F4, NOTE_F4, NOTE_E4, NOTE_E4, NOTE_D4, NOTE_D4, NOTE_C4, 0 }; const int long_beat = 300; const int beats [TOTAL_TONE] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 }; void setup () { pinMode (pinSpeaker, OUTPUT); } void loop () { for (int i = 0; i <TOTAL_TONE; i ++) { if (tone [i] == 0) { delay (beats [i] * long_beat); } Else { tone (tone_list [i] beats [i] * long_beat); } // Pause between tones noTone (pinSpeaker); delay (long_beat / 2); } } void tone (frequency int, int length) { tone (pinSpeaker, frequency, duration); delay (duration); }
Sketch 3 Music Program Garuda Pancasila
// Free Tutorial Arduino // www.ioisalman.com // Reference scales #include <pitches.h> // Speaker is on pin 9 const int pinSpeaker = 9; #define JUMLAH_NADA 85 const int daftar_nada[JUMLAH_NADA] = { NOTE_G4, NOTE_G4, NOTE_C5, NOTE_C5, NOTE_D5, NOTE_D5, NOTE_E5, 0, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_D5, 0, NOTE_G4, NOTE_G4, NOTE_D5, NOTE_D5, NOTE_E5, NOTE_E5, NOTE_F5, 0, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_G4, NOTE_G4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5, 0, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_A4, NOTE_C5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_G5, 0, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_A4, NOTE_C5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_G5, 0, NOTE_G5, NOTE_A5, NOTE_G5, NOTE_F5, NOTE_E5, NOTE_D5, 0, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_A4, NOTE_G4, 0, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_C5, NOTE_D5, NOTE_E5, 0, NOTE_C5, NOTE_C5, NOTE_A5, NOTE_G5, NOTE_B4, NOTE_C5, 0 }; const int lama_beat = 300; const int beats[JUMLAH_NADA] = { 1, 1, 2, 2, 2, 2, 3, 1, 1, 1, 2, 1, 1, 2, 2, 3, 1, 1, 1, 2, 2, 2, 2, 3, 1, 1, 1, 2, 1, 1, 2, 1, 1, 3, 1, 1, 1, 2, 1, 1, 2, 1, 1, 3, 1, 1, 1, 2, 1, 1, 2, 1, 1, 3, 1, 2, 3, 1, 2, 2, 3, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 2, 2, 2, 3, 1, 3, 0 }; void setup() { pinMode(pinSpeaker, OUTPUT); } void loop() { for(int i=0; i<JUMLAH_NADA; i++){ if(nada[i] == 0){ delay(beats[i] * lama_beat); }else{ nada(daftar_nada[i], beats[i] * lama_beat); } // Pause between tones noTone(pinSpeaker); delay(lama_beat / 2); } } void nada(int frek, int durasi){ tone(pinSpeaker, frek, durasi); delay(durasi); }
Note, among Sketch 2 and 3 are just different number of tones, tones and beats her. So the main program in the loop function nothing has changed. Why? Because the program has to be flexible with their functions and help the array. Just imagine if both programs are created manually. Passable complicated course.
0 Response to "Creating Functions and Project music in arduino"
Post a Comment