Lachlan's avatar@lachlanjc/edu
All Creative Computing

Week 3: Change – Log

This week we’re combining analog input & output, stepping toward creating a meaningful interaction with a device.

So shut up & do it.
—Dan

The potentiometer

A potentiometer is a simple knob that provides a variable resistance, which we can read into the Arduino board as an analog value.
Arduino docs

To make use of the “mother of all resistors,” we’ll need to wire up the Arduino with a resistor, power cables, and the potentiometer itself. Here’s how that looks:

Breadboard with Arduino and potentiometer

Integrating with software

First in setup, we’re beginning Serial communication, the in loop we’re printing out the values being read. Not too complex.

(println puts each output on its own line, print outputs inline text.)

void setup() {
Serial.begin(9600);
}
void loop() {
int input = analogRead(A7);
Serial.println(input);
}

9600 refers to possible number of changes-of-state, which here is 1 byte per second. The Arduino could handle more—19200 would be double the throughput—but 9600 is fast enough for our purposes.

This week’s project

I’m stressed. All the time. I need a good way to calm down. Let’s turn stress into productivity & build a meditation device!

We’re implementing pulse-width modulation, otherwise known as fading the brightness of an LED over time at our command. The LED will guide us through calm breathing.

Credit to this tutorial for teaching me about PWM!

Inspiration

Remember the 2008 MacBook Pro’s sleep indicator? (Probably not, but it was, literally, lit.)

So like that, but with an Arduino.

Getting started

First, I attempted to wire up the circuit. This is, tragically, where the results ended.

My circuit

I could get the LED to turn on when I stuck its connectors directly in power + ground, but when I attempted to connect one leg into the circuit, I could not find a destination for the leg and appropriate code to illuminate the LED.

This presents the reality that I have no real understanding of how this circuit works, and the assistance I secured was inadequate.

I failed—& don’t at all enjoy admitting this.

It’s not my brand, one could say.

Next steps

However, I had big plans. Here’s the code I was planning if the setup of the project had worked—except it didn’t, so I never got to properly implement it.

After initializing the LED, we’d loop from 0 to 255 (off to full brightness), adjusting the brightness continuously. Then we’d loop from 255 back down to 0, hold on for a second, and the loop would restart itself. The exact timing I’d have to tweak once I saw the project in action, but I didn’t get a chance to do that, so I kept the intervals at power-of-ten integers.

int pin = 13;
void setup() {
pinMode(pin, OUTPUT);
}
void loop() {
for (int i = 0; i <= 255; i++) {
analogWrite(pin, i);
delay(10);
}
for (int i = 255; i >= 0; i--) {
analogWrite(pin, i);
delay(10);
}
delay(1000);
}

Adding the potentiometer

One thing I love about iOS device displays is how they sense the current brightness level & automatically adjust, so you’re not blinded in the dark or unable to read in the light. Since meditation can be done at all times of day—what if we used the potentiometer to implement Auto-Brightness in our meditation device?

This was the next step. I’d map the value of the potentiometer to a brightness scale in the room, then replace the 255 in the code above with a max value calculated at the beginning of the loop. I think this would actually work.


This project ended in despair & frustration, but there’s always next week! I have lots more ideas.