5.0 Putting It All Together: Your First Functional Sketch
You now have the basic tools to read and understand a real Arduino sketch. The famous “Blink” sketch is a perfect example that combines everything we’ve discussed. The comments below highlight how each building block is used.
// 1. VARIABLE: A global variable to hold the pin number for our LED.
// It’s an ‘int’ (integer) because pin numbers are whole numbers.
int ledPin = 13;
// 2. STRUCTURE: The setup() function runs once.
void setup() {
// We set the ledPin to be an output.
pinMode(ledPin, OUTPUT);
}
// 3. STRUCTURE: The loop() function runs forever.
void loop() {
// Turn the LED on
digitalWrite(ledPin, HIGH);
// Wait for 1 second (1000 milliseconds)
delay(1000);
// Turn the LED off
digitalWrite(ledPin, LOW);
// Wait for 1 second
delay(1000);
}
// NOTE: This sketch uses an ‘int’ variable for the pin number and the basic
// setup()/loop() structure. Inside loop(), we call the digitalWrite() function,
// passing it the pin number and a state (HIGH or LOW) to control the LED.
With these fundamental building blocks—setup() and loop(), variables and their data types, and operators—you have a solid foundation for your journey into the world of Arduino. You are now ready to start exploring, experimenting, and writing your own amazing sketches.