43
In the first basic tutorial we showed a couple of simple LED examples, here is another couple to try
A reminder of the schematic if you need it
Code examples
Start up the Arduino IDE, a barebones sketch will appear, delete everything and enter the following
Example 1: Leds on and off – one at a time
[codesyntax lang=”cpp”]
/* * Blink many LEDs example * switches an LED on and off one at a time */ void setup() { pinMode(2, OUTPUT); // set pin 2 as output pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); } void loop() { digitalWrite(2, HIGH); delay(1000); digitalWrite(2, LOW); delay(1000); digitalWrite(3, HIGH); delay(1000); digitalWrite(3, LOW); delay(1000); digitalWrite(4, HIGH); delay(1000); digitalWrite(4, LOW); delay(1000); digitalWrite(5, HIGH); delay(1000); digitalWrite(5, LOW); delay(1000); digitalWrite(6, HIGH); delay(1000); digitalWrite(6, LOW); delay(1000); digitalWrite(7, HIGH); delay(1000); digitalWrite(7, LOW); delay(1000); digitalWrite(8, HIGH); delay(1000); digitalWrite(8, LOW); delay(1000); }
[/codesyntax]
Example 2: Switch the LEDs on one a time and then off again
[codesyntax lang=”cpp”]
/* * Blink many LEDs example * switches an LED on one at a time then off one at a time */ void setup() { pinMode(2, OUTPUT); // set pin 2 as output pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); } void loop() { digitalWrite(2, HIGH); delay(1000); digitalWrite(3, HIGH); delay(1000); digitalWrite(4, HIGH); delay(1000); digitalWrite(5, HIGH); delay(1000); digitalWrite(6, HIGH); delay(1000); digitalWrite(7, HIGH); delay(1000); digitalWrite(8, HIGH); delay(1000); digitalWrite(2, LOW); delay(1000); digitalWrite(3, LOW); delay(1000); digitalWrite(4, LOW); delay(1000); digitalWrite(5, LOW); delay(1000); digitalWrite(6, LOW); delay(1000); digitalWrite(7, LOW); delay(1000); digitalWrite(8, LOW); delay(1000); }
[/codesyntax]
Example 3 : All on the All off
[codesyntax lang=”cpp”]
/* * Blink many LEDs example * switches all LEDs on then off */ void setup() { pinMode(2, OUTPUT); // set pin 2 as output pinMode(3, OUTPUT); pinMode(4, OUTPUT); pinMode(5, OUTPUT); pinMode(6, OUTPUT); pinMode(7, OUTPUT); pinMode(8, OUTPUT); } void loop() { digitalWrite(2, HIGH); digitalWrite(3, HIGH); digitalWrite(4, HIGH); digitalWrite(5, HIGH); digitalWrite(6, HIGH); digitalWrite(7, HIGH); digitalWrite(8, HIGH); delay(1000); digitalWrite(2, LOW); digitalWrite(3, LOW); digitalWrite(4, LOW); digitalWrite(5, LOW); digitalWrite(6, LOW); digitalWrite(7, LOW); digitalWrite(8, LOW); delay(1000); }
[/codesyntax]
Of course there are better ways of doing the examples above, we will show you these soon