Step 7Understand the Starting Sketch
----------------
// Buzzer example function for the CEM-1203 buzzer (Sparkfun's part #COM-07950).
// by Rob Faludi
// http://www.faludi.com
----------------
The above section is ignored by the compiler. Any line starting with '//' is ignored, used for making comments. Commenting the code is great for describing what's going on for your future reference, and for others looking at your code.
----------------
void setup() {
pinMode(4, OUTPUT); // set a pin for buzzer output
}
----------------
Anything run in the built in setup() funtion is only run once when the Arduino is booted. pinMode(11, OUTPUT); sets pin 11 on the Arduino as an output pin. The guts of every function, setup() included, are held between the opening '{' and closing '}'.
----------------
void loop() {
buzz(4, 2500, 500); // buzz the buzzer on pin 4 at 2500Hz for 500 milliseconds
delay(1000); // wait a bit between buzzes
}
----------------
The built in loop() function is run over and over as long as the Arduino is powered. In this example, the custom buzz() function is called (see below), followed by Arduino's built in delay() function. The delay() function pauses the script for a specified number of miliseconds. In this case, 1000 miliseconds, or 1 second.
----------------
void buzz(int targetPin, long frequency, long length) {
...
}
----------------
The buzz() function is a custom function that Ron created, requiring three arguments: targetPin, frequency, and length. Each argumant is given a data type (int, long, and long). See Ron's code comments in the sketch (or image in this step) for details on how the buzz is generated. More information about data types and scripting is available here.
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|














































