Lachlan's avatar@lachlanjc/eduCourses
Creative Computing

Creative Computing – Week 2: Software – Log

Software is the revolution of our times.

The “universal machine” is a long-promised device that can do anything. An iPad is pretty close, if you ask me. But today we’re just building an LED connected to a button—perhaps the most simplified essence of what an iPad is!


Step 1: lighting up the LED with software

This week, we’re going to control the Arduino microcontroller not via a physical switch, but via software written on a computer.

First, I’m setting up a basic circuit on a small breadboard with the Arduino & a resistor.

Most basic circuit on the Arduino + breadboard

Next, I’m adding an LED, and a button we can eventually control it with. The LED will be off, because we haven’t written the code that will turn it on.

Circuit with accessories

In the Arduino IDE, we can turn on the LED with some basic code:

void setup() {
pinMode(2, OUTPUT);
}
void loop() {
digitalWrite(2, HIGH);
}

Output the code to the Arduino, & voila! The LED is on.

Now we’ll make the LED blink, using the delay function.

void loop() {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
}

Fun times! But let’s add a button.

Step 3: adding a button

Here’s the code we’ll need to add support for the button:

void loop() {
if (digitalRead(4) == 1) {
digitalWrite(2, HIGH);
delay(1000);
digitalWrite(2, LOW);
delay(1000);
}
}
Finished project

Step 4: going wild

Let’s make it go a little wild with random delays in the blinking:

void loop() {
digitalWrite(2, HIGH);
delay(random(0, 1000));
digitalWrite(2, LOW);
delay(random(0, 1000));
}

And there we have it! Our Arduino understands time and manual interaction in our project now.