A sorta-kinda Theremin using two Sharp IR rangers, my A2D I2C adapters, and my simple pwm/sound/beep library. Demo video: http://www.youtube.com/watch?v=6HEsA3RBoPc

Dependencies:   Beep mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "Beep.h"
00003 
00004 /**
00005  * Pseudo Theremin using Sharp IR Rangers + my A2D I2C adapters
00006  *
00007  * Demo Video: http://www.youtube.com/watch?v=6HEsA3RBoPc
00008  * A2D adapters: https://code.google.com/p/bot-thoughts-sharpi2c/
00009  *
00010  * One ranger controls pitch, the other on/off.
00011  * Simple PWM sound generation is used.  The output pin p22 feeds
00012  * into a simple common-emitter BJT amplifier on my breadboard.
00013  */
00014 DigitalOut myled(LED1);
00015 I2C i2c(p28, p27);       // sda, scl
00016 Serial pc(USBTX, USBRX); // tx, rx
00017 Timer t;
00018 Beep buzzer(p22);
00019 
00020 #define PITCH   0x26
00021 #define PITCHR  (PITCH<<1)|1
00022 #define PITCHW  (PITCH<<1)
00023 #define VOLUME  0x27
00024 
00025 int main() {
00026     int p = 0;
00027     int v = 0;
00028     
00029     i2c.frequency(100000);
00030     pc.baud(115200);
00031     pc.printf("Hello world!\n");
00032     t.start();
00033     t.reset();
00034     
00035     while(1) {
00036         char data[3];
00037         float range = 0.0;
00038 
00039         pc.printf("%8u ", t.read_ms());
00040 
00041         if ( i2c.read(VOLUME<<1, data, 2) != 0) {
00042             pc.printf("~0x%02x ", VOLUME);
00043         } else {
00044             v = data[0]<<8 | data[1];
00045             pc.printf("%03u %03u v=%u ", data[0], data[1], v);
00046         }            
00047 
00048         if ( i2c.read(PITCHR, data, 2) != 0) {
00049             pc.printf("~0x%02x", PITCH);
00050         } else {
00051             p = data[0]<<8 | data[1];
00052             if (v > 2300){
00053                 range = 500 + 500 * powf(4187.8 / p, 1.1060);
00054                 buzzer.beep(int(range));
00055             } else {
00056                 buzzer.nobeep();
00057             }
00058             pc.printf("%03u %03u p=%u r=%f", data[0], data[1], p, range);
00059         }
00060         pc.printf("\n");
00061 
00062         wait(0.005);
00063     }
00064 }