Showing posts with label microcontrollers. Show all posts
Showing posts with label microcontrollers. Show all posts

December 22, 2007

Arduino Theremin

When I got my Arduino protoshield from sparkfun, I wanted to make something cool with it. And since I had seen a few Theremin projects before, and already thought about making some, I finally made one, using the Parallax PING sonar sensor. Here are some informations about how I made it. You can see all the pictures here.

Arduino Theremin

First, the schematics:

Arduino Theremin circuit

Arduino Theremin speaker schematicArduino Theremin pushbutton schematicArduino Theremin Ping schematic

Here's the commented code for the Arduino:
int pingPin = 7;
int buzzPin = 10;
int btnPin = 1;
int val;
 
void setup()
{
  pinMode(buzzPin, OUTPUT);  //set speaker pin to output
  pinMode(btnPin, INPUT);    //set pushbutton pin to input
}
 
int getPing()
{
  //send a 10us pulse to wake up the sonar
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(pingPin, LOW);
  
  //get the raw value from the sonar, corresponding to the
  //actual time of travel of the ultrasound waves
  pinMode(pingPin, INPUT);
  return pulseIn(pingPin, HIGH);  //return this value
}
 
void loop()
{
  if (digitalRead(btnPin) == HIGH)  //only play when the button is pressed
  {
    val = getPing() / 5;  //you can tune the pitch by dividing by a different number
    
    //generate the pulse
    digitalWrite(buzzPin, HIGH);
    delayMicroseconds(val);
    digitalWrite(buzzPin, LOW);
    delayMicroseconds(val);
  }
}

And that's about it! Now you should start to learn how to play it... Kinda hard at the beginning, but that's where the fun begins!
Here are some videos of me playing (jingle bells and tetris). It's also kinda noisy... maybe a better sonar would be better (if you have a different sonar, and manage to make a better 'thereduino', I want to see it!!!). Enjoy (or at least try to :p).


If I improve my skills, I'll post new videos... But I also want to see YOU playing YOUR Arduino Theremin! Feel free to send me an email or write a comment.

October 30, 2007

In holiday with Arduino

I have a 10-day holiday, and, as usual, I am in Rome during my holidays (to see my family, to take my piano lessons, etc.). Of course, I couldn't much stuff, so I just brought my Arduino and the components that came with the "Arduino workshop kit" I bought this summer on an Italian site (yep, the Arduino is make in Italy!!), i.e. LEDs, resistors, caps, pushbuttons, hook-up wires, etc. but I also brought a speaker, some sensors and servos, to have some fun with the Arrrrduino.

I actually like the Arduino platform very much, especially the C language, which isn't available with BASIC Stamps or BASIC Atoms that I use for my boe bot and lynxmotion BRAT robot. It's pretty powerful compared to the normal BS2 (although I am missing the multi-processing power of my favorite, the Parallax Propeller), since it uses an AVR ATMega168 (on the most recent boards, including the last one, the "Arduino Diecimila", which is the one I have), but there are a few inconveniences I think, like waiting at least 10 seconds each time you upload a program, and especially the lack of some coding functions that the BASIC Stamp provides, which are very useful and practical, like PULSOUT/IN, SHIFTOUT/IN, even though I think they will be coming soon, if they haven't already. Powering it through USB is very handy, since you don't have to carry around batteries, but of course, you can use any power supply with it. I miss the little breadboard that's on the Board of Education, but I will probably be getting a "protoShield" from Sparkfun as soon as I can: it's a board of the size of the Arduino itself, that you just plug in the Arduino female headers, so that you can use a tiny breadboard directly; but a good thing of it are the 6 analog input pins, and the 6 PWM output pins, which make it very easy to get input directly from an analog sensor, or anything like that.

So one of the first programs I made when I got it was getting an input from a potentiometer, that would change the output frequency of a speaker. That was pretty easy to do, even though the way I did it isn't very high level. Here is the code I used:

int buzzerPin = 10;
int inputPin = 0;
int val; 
 
void setup()
{
    pinMode(buzzerPin, OUTPUT);
} 
 
void loop()
{
    val = analogRead(inputPin);
    digitalWrite(buzzerPin, HIGH);
    delayMicroseconds(val);
    digitalWrite(buzzerPin, LOW);
    delayMicroseconds(val);
}

A few weeks ago, I saw a MAKE magazine weekend projects podcast where I discovered the "Theremin" instrument, which I find very interesting and cool. So I thought I could do something similar, but in a different way: with distance detection. In fact I brought a Sharp GP2D12 sensor and replaced the potentiometer with it, and with the exact same code, it kinda sorta worked: if the hand is perfectly in the perpendicular plane of the sensor, then it controls the pitch, but as soon as the hand doesn't reflect perfectly the IR beam, it gets messier. And because of the non-linear behavior of this analog sensor, and of its somewhat noisy data , it's hard to play something coherent...

I'll try to post some videos soon.

Later, I thought I could try to make it change pitch by tilting an accelerometer (either the Memsic 2125 from Parallax, which I brought here with me, of maybe even with a wiimote and through serial communication with the computer!)