Monday, January 07, 2013

Using a tilt switch to play an audio file

I am helping with an Arduino and Sound module at the Glasgow School of Art this month, so had a wee play with their kit of parts to create a quick demo. Here is a wee video:



The tilt switch is inputting into the arduino. The arduino is running the sketch below. It is outputting to an optocoupler which is in turn controlling a PCB from an old keyboard. That PCB is sending a [space] keystroke to the computer, which has Winamp open and set to play a telephone ring .wav when spacebar is pressed.

The optocoupler is ideal as it physically separates the Arduino circuit from the keyboard PCB circuit, and acts as a switch, shorting out whichever contacts you choose on the keyboard PCB, which is how these things work in the original keyboard. The datasheet with pinouts for that sort of optocoupler is here.

It took a wee bit of trial and error to find the pins to short on the keyboard PCB to get a [space] keystroke. After I'd done it I discoverred these wee Windows programs that will show you on screen what keypresses the computer is receiving:
http://www.romeosa.com/osdHotkey/help.html
http://www.donationcoder.com/Software/Skrommel/index.html#ShowOff
On a Mac apparently you can do the same in System Preferences:
Universal Access > Keyboard "Enable sticky keys" > Display pressed
keys on screen.
Winamp is ideal because you can customise it in preferences extensively - you can set any key combination to play, or any other function in the program! The shortcut for play in iTunes is [space].

The sketch (copy and paste this into the arduino software):


/* 
This sketch allows you to send a keystroke back to the computer
upon tilting a tilt switch. This keystroke can be used to control
any other program as a keyboard would, typically simulating a
SPACE keypress to play an audio file in an audio player.

Wiring setup:
Tilt sensor between +5v and pin2
10k resistor between pin2 and GND (for debouncing)
Optocoupler pins 1, 2 4 and 5 to Arduino pin7, GND, keyboard PCB
pinA and pinB respectively. You will have to identify  the appropriate
two pins on your keyboard PCB to simulate the desired
keystroke - plug into your computer and short them out to test.
Keyboard PCB connected to computer.
LED long leg to Arduino pin8 and short leg to GND.
*/

int tiltPin = 2;  // variable for setting the tilt sensor pin
int optPin = 7;  // variable for setting the optocoupler pin
int ledPin = 8;  // variable for setting the led pin
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // put your setup code here, to run once:
  pinMode(tiltPin, INPUT); // sets up tiltPin as an Input
  pinMode(optPin, OUTPUT); // sets up optPin as an Output
  pinMode(ledPin, OUTPUT); // sets up ledPin as an Output
  //  Serial.begin(9600);  // begins a Serial communication
}

void loop() {
  // put your main code here, to run repeatedly: 
  buttonState = digitalRead(tiltPin);  // sets buttonState to value of the tilt sensor
  if (buttonState == HIGH) {      
    digitalWrite(optPin, HIGH);   // turn on optocoupler
    digitalWrite(ledPin, HIGH);   // turn on LED
    delay(100);  // wait 0.1ms
    digitalWrite(optPin, LOW);   // turn off optocoupler
    delay(1000);  // wait 1s
  }
  else {
    digitalWrite(optPin, LOW);  // turn off optocoupler
    digitalWrite(ledPin, LOW);   // turn off LED
  }
  //  Serial.println(tiltPin);    // print the stated variable
}




No comments: