Somatosensory Birthday Music Box

Somatosensory Birthday Music Box

The music box keeps playing the Happy Birthday to You all the time. Left-leaning, it will speed up playing. Right-leaning, it will slow down. Forward-leaning, the volume will rise. And back-leaning, the volume will lower.

The following is the program:

#include “MPU6050_6Axis_Microduino.h”

float x;

float y;

MPU6050 mpu;

Quaternion q;

float ypr[3];

uint8_t mpuMode;

bool mpuReady;

 

#define BuzzerPin6 6

int tone_list[] = {262, 294, 330, 349, 392, 440, 494, 523, 587, 659, 698, 784, 880, 988, 1046, 1175, 1318, 1397, 1568, 1760, 1967};//The frequency array of the bass 1 to treble 7.

int music_4[] = {5, 5, 6, 5, 8, 7, 5, 5, 6, 5, 9, 8, 5, 5, 12, 10, 8, 7, 6, 11, 11, 10, 8, 9, 8, 0};//The tone order of the Happy Birthday to You.

float rhythm_4[] = {0.5, 0.5, 1, 1, 1, 2, 0.5, 0.5, 1, 1, 1, 2, 0.5, 0.5, 1, 1, 1, 1, 1, 0.5, 0.5, 1, 1, 1, 3};//The rhythm order of the Happy Birthday to You.

 

void setup()

{

x = 0;//Rhythm ratio coefficient

y = 0;//Tone ratio coefficient

mpuMode = MODE_DMP;

mpuReady = mpu.begin(mpuMode);

Serial.begin(9600);

}

 

void loop()

{

 

for (int a = 0; music_4[a] != 0; a++) {//Times of repetition depend on the number of the tones of the Happy Birthday to You.

Serial.print(“a:”); Serial.println(a);

if (!mpuReady) return;//The first time to read the DMP buffer. It won’t be used, but to clear the old data in the cache.

mpu.getYawPitchRoll(ypr);

if (!mpuReady) return;//The second time to read the DMP buffer. This is the latest attitude angle.

mpu.getYawPitchRoll(ypr);

x = ypr[1];//Read the attitude angle on X axis.

x = constrain(x, -40, 40);//Limit x.

x = map(x, -40, 40, 10, 100);//x maps to 0~100.

for (int i = 0; i < rhythm_4[a] * x; i++) {//Times of repetition are the tone rhythm*x, so as to change the rhythm proportion through changing attitude to change X.

Serial.print(“i:”); Serial.println(i);

if (!mpuReady) return;

mpu.getYawPitchRoll(ypr);

if (!mpuReady) return;

mpu.getYawPitchRoll(ypr);

x = ypr[1];

x = constrain(x, -40, 40);

x = map(x, -40, 40, 10, 100);

y = (ypr[2] >= 0 ? 180 – ypr[2] : (ypr[2] * -1) – 180);//Because chip mpu6050 is welded on the ventral side ofmCookie, the Y axis attitude angle needs conversion and reverse.

y = constrain(y, -40, 40);

if (y > 0) {

y = map(y, 0, 40, 100, 400);

}

else if (y < 0) {

y = map(y, 0, -40, -100, -400);

}

else {

y = 100;

}

y = y / 100;//According to the Y axis attitude angle, the variable y can be adjusted from 4.00 to 1.00 or -1.00 to-4.00. So as to change the pitch proportion.

if (music_4[a] != 22) {//Tone 22 is the specific rest, so it doesn’t sound when the tone is 22.

if (y >= 0) {

y = tone_list[music_4[a] – 1] * y;//Y times pitch frequency.

}

else {

y = tone_list[music_4[a] – 1] / (y * -1);//Y times pitch frequency.

}

tone(BuzzerPin6, y);//The buzzer sounds in y frequency.

}

else {

noTone(BuzzerPin6);

}

delay(1);

}

noTone(BuzzerPin6);

delay(10);

}

delay(1000);

}

 

The following is the address of the video:

http://video.weibo.com/show?fid=1034:679f8448d08c7f69b55a22849bb68a02

 

Related Post

Microduino-Joypad Getting started

Hardware building If you are not familiar with Microduino, please refer to:  Microduino_Getting_start,  otherwise, go ahead. Required modules Module Quantity Function…

Leave a Reply

Your email address will not be published. Required fields are marked *