typestar

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

  1. Every pin gets one name at the top: PUMP_PIN, VALVE_PIN.
  2. const unsigned long fits the durations delay expects.
  3. The loop reads as a watering recipe, not a number soup.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 3 in The sketch, step 3 of 27 in Language basics.

← Previous Next →