Featured Posts

PComp | Week 3 | Hand-Controlled Sound

Final draft of the week

This week started with trying to write a program for a button switch, that made it so that...

  1. If the button is first pressed the LED will remain turned on, and

  2. if the button is pressed again, the LED will remain turned off.

Initially, I wrote something along these lines:

#define switchPin 2 #define redLED 3

int ledState = 0;

void loop {

int switchState = digitalRead(switchPin);

if (switchState){



if (ledState%2==0){ //modulus 2 so that it returns either 0 or 1

digitalWrite(redLED, LOW); //if 0, turn LED on

} else {

digitalWrite(redLED, HIGH); //if 1, turn LED off



I used the variable ledState to record the number of the switch being pressed (even means off, odd means on).

However, the problem was when I press the switch once, the program read the HIGH value multiple times as it operated in a very high frequency. Pressing the switch button for the first time would then sometimes not yield in odd ledState, and pressing the button again often did not change ledState into even number.

So I re-strategised and included one more variable to tell the program to listen to the switch or not. When the first cycle of the program reads HIGH from the switch, it stops listening to the switch until the switch is no longer pressed (i.e. reads LOW).

boolean isOn = false; //initial cycle: LED is turned off boolean isListen = true; //initial cycle: tell the program to listen to switch

void loop {

int switchState=digitalRead(switchPin);

if (isListen){

if (switchState){

isOn = !isOn; //if the switch is pressed, swap the value of isOn


isListen = !isListen; //after this if nest is called, stop listening


if (!switchState){

isListen = !isListen; //when switched is released (LOW), start listening


digitalWrite(redLED, isOn); //turn ON or OFF the LED

digitalWrite(greenLED, !isOn);


And it worked! Albeit with unnecessary & complicated lines of code. Only later on the day I learned about debouncing a switch (basically adding a delay in the first cycle).

My next stop was this lab.

I only have 01 x FSR, so I used potentiometer as the other input module. All was well and working fine.

Then I thought of playing around with a speaker, since sound would make a more recognisable (and pleasant) analog output than LED intensity.

I adjusted the circuit and found this tutorial (I think by Tom?) to help me program my sound output.

Some primordial DJ set

It turned out that turning the pot knob was not that fun, and since I have some IR distance sensor, I thought, "let's make some kind of theremin (prototype) !".

Source: wikipedia

I wired the IR sensor in, read the range of value analogRead() gave me, and basically used it as a replacement for the potentiometer.

Here is the code:

int melody[] = { NOTE_C5, NOTE_D5, NOTE_E5, NOTE_F5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_C6 }; //this is the note range

void loop() { forceVal = digitalRead(A1); //reads if the FSR is pressed val = analogRead(sensorpin); // reads the value of the distance sensor pitchMap = map(val, 0, 600, 0, 7);

if (forceVal){ //if the FSR is pressed, play notes if(val > 100){ tone(speaker, melody[pitchMap], 250);

Serial.print("distance: "); Serial.println(val); Serial.print("pitch: "); Serial.println(pitchMap); delay(100); } } }

Here is the video again

What I noted though, is that since the IR sensor is meant for the distance range of 20-150cm, it was not ideal to use it as a tabletop sensor for your hands. When the distance becomes < 20cm, the values given by the sensor become unreliable and flipped (hence why the sound went higher as I moved my hand closer).

Note to self: use a suitable sensor next time, and maybe pre-record some of the sounds so that it won't be so annoying to listen to.

#PComp #PhysicalComputing

Related Posts
Recent Posts
Search By Tags
No tags yet.

Hafiyyandi | Creative Technologist

New York, New York | hafiyyandi@gmail.com