Hardware Reference
In-Depth Information
Creating a simple library
There are typically two “Hello World” programs for Arduino. One blinks an LED on and off, and the other sends a
"Hello, World!" message over the serial connection. Here, we will convert the sketch into a simple library. Figure 12-2
shows a visualization of the library, implementation, and sketch file that we are creating.
Figure 12-2. SimpleHello.ino sketch and code layout
The starter sketch for this is example is Listing 12-2.
Listing 12-2. Initial sketch code
const int LED_PIN = 13;
void setup()
{
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
}
void loop()
{
Serial.println("Hello, World!");
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}
There are several small details in this code that we can clean up and place into a library. The pin can differ
between boards, so we will want to define a default pin of 13 and allow for it to be overridden.
 
Search WWH ::




Custom Search