Emotional tones

http://a.parsons.edu/~kimm541/DSC_4228.MOV

 

 

#define  v     18000
#define  x     10000
#define  z     8000
#define  c     3830    // 261 Hz
#define  d     3400    // 294 Hz
#define  e     3038    // 329 Hz
#define  f     2864    // 349 Hz
#define  g     2550    // 392 Hz
#define  a     2272    // 440 Hz
#define  b     2028    // 493 Hz
#define  C     1912    // 523 Hz
#define  D     1680
#define  E     1500
#define  F     1350
#define  G     1200
#define  A     1050
#define  B     912
// Define a special note, ‘R’, to represent a rest
#define  R     0

// SETUP ============================================
// Set up speaker on a PWM pin (digital 9, 10 or 11)
int speakerOut = 9;
// Do we want debugging on serial out? 1 for yes, 0 for no
int DEBUG = 1;

void setup() {
pinMode(speakerOut, OUTPUT);
if (DEBUG) {
Serial.begin(9600); // Set serial out if we want debugging
}
}

// MELODY and TIMING  =======================================
//  melody[] is an array of notes, accompanied by beats[],
//  which sets each note’s relative length (higher #, longer note)
int hellom[] = {c,  c};
int beats[]  = {16, 64};
int dyingm[] = {  v,  R, z, v };
int beats2[] = { 200, 8, 8, 32 };
int happym[] = {  c,  C,  g,  e,  C,  g,  z,  e,  z,    c,  C,  g,  f,  C,  g,  z,  f,  z,     c,  C,  g,  e,  C,  g,  z,  e,  z,     d,  e,  f,  z,  f,  g,  g,  z,  g,  a,  a,  z,  C,  R};
int beats3[] = { 16, 16, 16,  16, 8,  8, 16, 16, 16,   16, 16, 16, 16,  8,  8, 16, 16, 16,    16, 16, 16,  16, 8,  8, 16, 16, 16,     8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8,  8, 16, 16};
int sadm[]   = {z,  x,  z,  z,  x,  x};
int beats4[] = {36, 36, 36, 36, 36, 200};
int yesm[]   = {d, b, E};
int beats5[] = {8, 16, 32};
int nom[]    = {B,  B,  B,  B,  B,  B};
int beats6[] = {8,  8,  8,  8,  8,  8};

int MAX_COUNT = sizeof(hellom) / 2;
int MAX_COUNT2 = sizeof(dyingm) / 2;
int MAX_COUNT3 = sizeof(happym) / 2;
int MAX_COUNT4 = sizeof(sadm) / 2;
int MAX_COUNT5 = sizeof(yesm) / 2;
int MAX_COUNT6 = sizeof(nom) / 2;// Melody length, for looping.

// Set overall tempo
long tempo = 10000;
// Set length of pause between notes
int pause = 1000;
// Loop variable to increase Rest length
int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

// Initialize core variables
int tone1 = 0;
int beat = 0;
long duration  = 0;

// PLAY tone1  ==============================================
// Pulse the speaker to play a tone1 for a particular duration
void playtone1() {
long elapsed_time = 0;
if (tone1 > 0) { // if this isn’t a Rest beat, while the tone1 has
//  played less long than ‘duration’, pulse speaker HIGH and LOW
while (elapsed_time < duration) {

digitalWrite(speakerOut,HIGH);
delayMicroseconds(tone1 / 2);

// DOWN
digitalWrite(speakerOut, LOW);
delayMicroseconds(tone1 / 2);

// Keep track of how long we pulsed
elapsed_time += (tone1);
}
}
else { // Rest beat; loop times delay
for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count
delayMicroseconds(duration);
}
}
}

// LET THE WILD RUMPUS BEGIN =============================
void loop() {
// play each sound in order, with a pause between each one.
delay(3000);
hello();
delay(3000);
dying();
delay(3000);
happy();
delay(3000);
sad();
delay(3000);
yes();
delay(3000);
no();
delay(5000);
}

void hello() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT; i++) {
tone1 = hellom[i];
beat = beats[i];

duration = beat * tempo; // Set up timing

playtone1();
// A pause between notes…
delayMicroseconds(pause);
//noTone(speakerOut);

}
}

void dying() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT2; i++) {
tone1 = dyingm[i];
beat = beats2[i];

duration = beat * tempo; // Set up timing

playtone1();
// A pause between notes…
delayMicroseconds(pause);
//noTone(speakerOut);

}
}

void happy() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT3; i++) {
tone1 = happym[i];
beat = beats3[i];

duration = beat * tempo; // Set up timing

playtone1();
// A pause between notes…
delayMicroseconds(pause);
//noTone(speakerOut);

}
}

void sad() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT4; i++) {
tone1 = sadm[i];
beat = beats4[i];

duration = beat * tempo; // Set up timing

playtone1();
// A pause between notes…
delayMicroseconds(pause);
//noTone(speakerOut);

}
}

void yes() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT5; i++) {
tone1 = yesm[i];
beat = beats5[i];

duration = beat * tempo; // Set up timing

playtone1();
// A pause between notes…
delayMicroseconds(pause);
//noTone(speakerOut);

}
}

void no() {
// Set up a counter to pull from melody[] and beats[]
for (int i=0; i<MAX_COUNT6; i++) {
tone1 = nom[i];
beat = beats6[i];

duration = beat * tempo; // Set up timing

playtone1();
// A pause between notes…
delayMicroseconds(pause);
//noTone(speakerOut);

}
}