Project 1.01 Blink x2
In this project, we bring a second LED, LED3, into the mix. We are essentially doing the same thing as the last project, but this time we are using two LEDs.
Project Code:
/////////////////////////////////////////////////// //Project 1.01 Blink x 2 byte LED1 = 13; byte LED3 = 7; void setup(){ pinMode(LED1,OUTPUT); pinMode(LED3,OUTPUT); } void loop(){ digitalWrite(LED1,HIGH); digitalWrite(LED3,HIGH); delay(1000); digitalWrite(LED1,LOW); digitalWrite(LED3,LOW); delay(1000); digitalWrite(LED1,HIGH); digitalWrite(LED3,LOW); delay(1000); digitalWrite(LED1,LOW); digitalWrite(LED3,HIGH); delay(1000); digitalWrite(LED1,LOW); digitalWrite(LED3,LOW); delay(1000); } ///////////////////////////////////////////////////
*If you’re copying and pasting the code, or typing from scratch, delete everything out of a new Arduino sketch and paste / type in the above text.
Like all sketches, this simple sketch includes a setup() and a loop() function. Before the setup() function, we declare two variables that refer to the pin numbers for LED1 and LED3:
byte LED1 = 13; byte LED3 = 7;
In the setup() function, we set both pins to OUTPUT using two pinMode statements:
pinMode(LED1,OUTPUT); pinMode(LED3,OUTPUT);
In the loop() function, we first switch both LEDs on by setting the pins to HIGH using two digitalWrite statements:
void loop(){ digitalWrite(LED1,HIGH); digitalWrite(LED3,HIGH); delay(1000);
After a 1-second delay, we turn both LEDs off:
digitalWrite(LED1,LOW); digitalWrite(LED3,LOW); delay(1000);
Next, we turn only LED1 on:
digitalWrite(LED1,HIGH); digitalWrite(LED3,LOW); delay(1000);
And then switch so that only LED3 is on:
digitalWrite(LED1,LOW); digitalWrite(LED3,HIGH); delay(1000);
Finally, we turn both LEDs off for 1 second before the loop() function reaches its closing bracket } and begins again at the top:
digitalWrite(LED1,LOW); digitalWrite(LED3,LOW); delay(1000); }