Automation - 2
Notice that the two functions are basically the same.
The only thing that changes—flips—is the LEDs.
Once it’s 13, and once it’s 7.
One function. Pass the LED pins as parameters.
const int TOUCH_SENSOR_PIN = 2;
const int LED_1_PIN = 13;
const int LED_2_PIN = 7;
void setup() {
pinMode(TOUCH_SENSOR_PIN, INPUT);
pinMode(LED_1_PIN, OUTPUT);
pinMode(LED_2_PIN, OUTPUT);
}
void loop() {
if (digitalRead(TOUCH_SENSOR_PIN) == HIGH) {
blinkLed(LED_1_PIN, LED_2_PIN);
} else {
blinkLed(LED_2_PIN, LED_1_PIN);
}
}
void blinkLed(int blinkingLed, int otherLed) {
digitalWrite(otherLed, LOW);
digitalWrite(blinkingLed, HIGH);
delay(500);
digitalWrite(blinkingLed, LOW);
delay(500);
}
Hit ✓ Check → Hit → Upload → Touch the sensor.
Same function. Different parameters.