Every Uno sketch needs a clear shape: declarations, one-time setup, then a repeating loop. Syntax rules keep the compiler happy; a shared style keeps sketches readable and consistent with Arduino examples.
concept
Program structure: setup() and loop()
void setup() {
}
void loop() {
}
Global declarations (constants, variables, objects) usually sit above setup. setup() runs once after reset or power-up for configuration such as pinMode and Serial.begin. loop() then runs for the life of the program: read inputs, decide, write outputs. Both functions must exist even if one body is empty.
Build it: Lesson 03 · Lesson 04
returnType name(parameters) {
}
A function is a named block that runs when called. setup and loop are functions. Write your own to name a task, pass parameters, return a value, and reuse logic. void means no return value. Keep functions short so each one is easy to test.
Build it: Lesson 06
note
Code conventions overview
A shared style keeps sketches organised, easy to mark, and closer to official Arduino examples and libraries. The rules below cover naming, braces, spacing, and pin constants. Follow them in this course unless a brief says otherwise.
Build it: Lesson 04
Use clear, descriptive names-not single letters or vague labels. Match the style to the kind of name.
| Element | Style | Good examples | Avoid |
|---|
| Variables | camelCase | sensorValue, ambientTemp, buttonState | x, n, val, data, temp, a |
| Pin / constants | SNAKE_CASE | LED_PIN, BUTTON_PIN, MAX_SPEED | p, pin, led, 13 (magic number) |
| Functions | camelCase | readSensor(), calculateAverage(), updateDisplay() | doStuff(), fn(), process() |
| Custom types / classes | PascalCase | MotorController, ButtonState | thing, myclass, data |
Tip: Names are case-sensitive. ledPin and LEDPIN are different identifiers.
Good names versus weak names
A good name tells the next reader (or marker) what the value means without hunting through the sketch.
| Avoid | Prefer | Why |
|---|
| int x = analogRead(A0); | int sensorValue = analogRead(A0); | Says what is stored, not just a letter |
| int temp = 22; | float ambientTemp = 22.0; | temp could mean temporary or temperature |
| const int pin = 2; | const int BUTTON_PIN = 2; | Names the role; UPPERCASE marks a fixed pin |
| digitalWrite(13, HIGH); | digitalWrite(LED_PIN, HIGH); | No magic number; one change updates every use |
| void doIt() { … } | void updateAlarm() { … } | Function names should say the action |
| bool flag; | bool alarmActive; | flag says nothing about the meaning |
Tip: If you must use a short loop index, i or j is fine inside a small for loop. Everywhere else, prefer a name that reads like English.
Build it: Lesson 04
syntax
Curly braces and indentation
void loop() {
if (sensorValue > 500) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
Braces mark the start and end of a block: function bodies, if branches, loop bodies. Every opening brace needs a matching close.
Course brace style
2 spacesIndent size
Indent each nested block by 2 spaces (Arduino IDE default). Do not mix tabs and spaces in one file.
{Opening brace
Place { on the same line as the if, else, for, while, switch, or function declaration.
}Closing brace
Place } on its own line, lined up with the statement that opened the block.
Tip: Indentation helps humans; the compiler only cares that braces match.
syntax
Spacing guidelines
int total = count + 5;
if (value == HIGH) {
digitalWrite(LED_PIN, HIGH);
}
analogWrite(LED_PIN, 128);
int total=count+5;
if(value==HIGH){
digitalWrite(LED_PIN,HIGH);
}
analogWrite(LED_PIN,128);
Spacing stops code looking clumped and makes operators and conditions easier to scan.
Where to put spaces
= + ==Binary operators
One space before and after operators such as =, +, -, ==, >, && and ||.
if (Control keywords
Space after if, while, for and switch-but not between a function name and its (.
,Commas
Space after commas in argument lists: analogWrite(LED_PIN, 128);
Build it: Lesson 05
syntax
Hardware pins and constants
const int BUTTON_PIN = 2;
const int RELAY_PIN = 7;
void setup() {
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(RELAY_PIN, OUTPUT);
}
void setup() {
pinMode(2, INPUT_PULLUP);
pinMode(7, OUTPUT);
}
Define pin numbers once at the top of the sketch with const int (preferred) or #define. Never scatter raw “magic numbers” through pinMode, digitalWrite or analogRead calls-changing a wire then means hunting every occurrence.
Tip: Name the role, not only the number: BUTTON_PIN is clearer than PIN2.
Build it: Lesson 04 · Lesson 07
Most statements end with a semicolon. A missing semicolon is a common compile error and may be reported on the next line. Do not place a semicolon immediately after if (...), for (...), or while (...) unless you intentionally want an empty statement.
Watch out: A stray semicolon after if (buttonPressed); creates an empty body. The following braces then always run.