Named pins in Arduino
const int beats a bare pin number scattered through a sketch.
// Name every pin once; const int beats a bare number scattered about.
const int PUMP_PIN = 7;
const int VALVE_PIN = 8;
const unsigned long SOAK_MS = 1500;
void setup() {
pinMode(PUMP_PIN, OUTPUT);
pinMode(VALVE_PIN, OUTPUT);
}
void loop() {
digitalWrite(VALVE_PIN, HIGH);
digitalWrite(PUMP_PIN, HIGH);
delay(SOAK_MS);
digitalWrite(PUMP_PIN, LOW);
digitalWrite(VALVE_PIN, LOW);
delay(SOAK_MS * 4);
}
How it works
- Every pin gets one name at the top:
PUMP_PIN,VALVE_PIN. const unsigned longfits the durations delay expects.- The loop reads as a watering recipe, not a number soup.
Keywords and builtins used here
HIGHLOWOUTPUTconstdelaydigitalWriteintlonglooppinModesetupunsignedvoid
The run, in numbers
- Lines
- 18
- Characters to type
- 401
- Tokens
- 86
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 74 seconds.
Step 3 of 3 in The sketch, step 3 of 27 in Language basics.