Workshop 2
1. Plugging in and setting up Arduino.
2. First simple Arduino sketch (program) - blink an LED.
We’ll start by blinking the built-in LED. You’ll need to
type in the following sketch to do this.
int led = 13;
void setup() {
pinMode(led,
OUTPUT);
}
void loop() {
digitalWrite(led,
HIGH);
delay(1000);
// wait for a second
digitalWrite(led,
LOW);
delay(1000);
// wait for a second
}
3. Make an external LED blink.
This uses the same sketch as above, but uses an LED that is
plugged across pins 13 and GND. The image below shows how the LED should be
plugged into the Arduino: SHORT leg in
GND, LONG leg in pin 13.
4. Flickering LEDs (fire/candles effect)
For this, you’ll need 5 LEDs: red, yellow and white. You can
choose which ones you use.
Insert the LEDs into the breadboard so that the long legs go
into the row next to the red line. The short legs go into separate columns.
In the same row, insert a lead (see right of image above)
and plug the other end of the lead into the 5V socket on the Arduino.
In the same columns as the short legs of the LEDs, place a
220 ohm resistor. You’ll need to first chop the legs so that they are about ½
inch long. One side of the resistor goes into the same column as the LED, the
other must go into one of the columns on the lower side of the board, as shown
above.
Connect the other end of each resistor to pins 2, 3, 4, 5
and 6. It doesn’t matter which one goes in which.
Type in this sketch:
const int FLICKER_INTERVAL = 50;
const int FLICKERSPEED = 3;
const int LEDPIN1 = 2;
const int LEDPIN2 = 3;
const int LEDPIN3 = 4;
const int LEDPIN4 = 5;
const int LEDPIN5 = 6;
void setup() {
pinMode(LEDPIN1,
OUTPUT);
pinMode(LEDPIN2,
OUTPUT);
pinMode(LEDPIN3,
OUTPUT);
pinMode(LEDPIN4,
OUTPUT);
pinMode(LEDPIN5,
OUTPUT);
randomSeed(analogRead(0));
}
void loop() {
while (true) {
flickerLED(LEDPIN1);
flickerLED(LEDPIN2);
flickerLED(LEDPIN3);
flickerLED(LEDPIN4);
flickerLED(LEDPIN5);
}
}
void flickerLED(int LED) {
digitalWrite(LED, HIGH);
delay(random(FLICKER_INTERVAL));
if
(random(FLICKERSPEED) == 1) {
digitalWrite(LED, LOW);
}
}
Save it as candles.ino
and upload to the Arduino.
What happens if you change the value of FLICKER_INTERVAL?
(Anything between about 20 and 200 works well).
What happens if you change the value of FLICKERSPEED? (Try
values between 3 and 10)
INTERACTIVE KEYBOARD
No comments:
Post a Comment