Saturday, February 6, 2010

Drunkrina Code

So it has been a bit more than a day since I planned to put up the Drunkrina code, but with Maker Faire on the horizon I used that to push myself to getting updates to this site and rise above other activities in life. So here it is.







/*
Matthew Garten
Drunkrina Rev4 May 25, 2009

//PWM code from http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1231029366

Note Frequency Wavelength(cm) timeHigh
C0 16.35 2100 30581
C#0/Db0 17.32 1990 28868
D0 18.35 1870 27248
D#0/Eb0 19.45 1770 25707
E0 20.6 1670 24272
F0 21.83 1580 22904
F#0/Gb0 23.12 1490 21626
G0 24.5 1400 20408
G#0/Ab0 25.96 1320 19260
A0 27.5 1250 18182
A#0/Bb0 29.14 1180 17159
B0 30.87 1110 16197
C1 32.7 1050 15291
C#1/Db1 34.65 996 14430
D1 36.71 940 13620
D#1/Eb1 38.89 887 12857
E1 41.2 837 12136
F1 43.65 790 11455
F#1/Gb1 46.25 746 10811
G1 49 704 10204
G#1/Ab1 51.91 665 9632
A1 55 627 9091
A#1/Bb1 58.27 592 8581
B1 61.74 559 8098
C2 65.41 527 7644
C#2/Db2 69.3 498 7215
D2 73.42 470 6810
D#2/Eb2 77.78 444 6428
E2 82.41 419 6067
F2 87.31 395 5727
F#2/Gb2 92.5 373 5405
G2 98 352 5102
G#2/Ab2 103.83 332 4816
A2 110 314 4545
A#2/Bb2 116.54 296 4290
B2 123.47 279 4050
C3 130.81 264 3822
C#3/Db3 138.59 249 3608
D3 146.83 235 3405
D#3/Eb3 155.56 222 3214
E3 164.81 209 3034
F3 174.61 198 2864
F#3/Gb3 185 186 2703
G3 196 176 2551
G#3/Ab3 207.65 166 2408
A3 220 157 2273
A#3/Bb3 233.08 148 2145
B3 246.94 140 2025
C4 261.63 132 1911
C#4/Db4 277.18 124 1804
D4 293.66 117 1703
D#4/Eb4 311.13 111 1607
E4 329.63 105 1517
F4 349.23 98.8 1432
F#4/Gb4 369.99 93.2 1351
G4 392 88 1276
G#4/Ab4 415.3 83.1 1204
A4 440 78.4 1136
A#4/Bb4 466.16 74 1073
B4 493.88 69.9 1012
C5 523.25 65.9 956
C#5/Db5 554.37 62.2 902
D5 587.33 58.7 851
D#5/Eb5 622.25 55.4 804
E5 659.26 52.3 758
F5 698.46 49.4 716
F#5/Gb5 739.99 46.6 676
G5 783.99 44 638
G#5/Ab5 830.61 41.5 602
A5 880 39.2 568
A#5/Bb5 932.33 37 536
B5 987.77 34.9 506
C6 1046.5 33 478
C#6/Db6 1108.73 31.1 451
D6 1174.66 29.4 426
D#6/Eb6 1244.51 27.7 402
E6 1318.51 26.2 379
F6 1396.91 24.7 358
F#6/Gb6 1479.98 23.3 338
G6 1567.98 22 319
G#6/Ab6 1661.22 20.8 301
A6 1760 19.6 284
A#6/Bb6 1864.66 18.5 268
B6 1975.53 17.5 253
C7 2093 16.5 239
C#7/Db7 2217.46 15.6 225
D7 2349.32 14.7 213
D#7/Eb7 2489.02 13.9 201
E7 2637.02 13.1 190
F7 2793.83 12.3 179
F#7/Gb7 2959.96 11.7 169
G7 3135.96 11 159
G#7/Ab7 3322.44 10.4 150
A7 3520 9.8 142
A#7/Bb7 3729.31 9.3 134
B7 3951.07 8.7 127
C8 4186.01 8.2 119
C#8/Db8 4434.92 7.8 113
D8 4698.64 7.3 106
D#8/Eb8 4978.03 6.9 100
*/



#include
Button hole1 = Button(3,PULLUP); //hole1 = right index finger
Button hole2 = Button(4,PULLUP); //hole2 = right middle finger
Button hole3 = Button(7,PULLUP); //hole3 = left index finger
Button hole4 = Button(2,PULLUP); //hole4 = left middle finger

int potPin = 1; // select the input pin for the potentiometer
int potval = 0; // variable to store the value coming from the sensor

int alPin = 2; // Alcohol sensor pin
int alval[7] = {0, 1000, 1000, 1000, 1000, 1000, 1000};

int PIN = 2;
int play = 0;

int ledPin = 13; // LED connected to digital pin 13

void setup() {
pinMode(9, OUTPUT);

pinMode(alPin, INPUT);
pinMode (PIN, INPUT);
pinMode(ledPin, OUTPUT);

TCCR1A = _BV(COM1A0)
| _BV(COM1B0) // toggle OC1B on compare match
| _BV(WGM10)
| _BV(WGM11);
TCCR1B = _BV(WGM12)
| _BV(WGM13); // Fast PWM mode, OCR1A as TOP
OCR1B = 0; // toggle when the counter is zero
OCR1A = 15383; // set top to the initial 65Hz
TCCR1B |= _BV(CS11); // set prescale to div 8 and start the timer
Serial.begin(9600);
}

void loop() {
unsigned int top;
unsigned int note;
int note_val;
char names[] = { 'c4', 'c#4', 'd4', 'd#4', 'e4', 'f4', 'f#4', 'g4', 'g#4', 'a4', 'a#4', 'b4', 'c5', 'c#5', 'd5', 'd#5', ' ' };
int freq[] = {262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494, 523, 554, 587, 622};


//Ionian Mode
//hole1 = right index finger
//hole2 = right middle finger
//hole3 = left index finger
//hole4 = left middle finger

potval = analogRead(potPin); // read the value from the pot sensor
//value of 2 seems to be working!
Serial.println(potval/50);

alval[0] = analogRead (PIN);
if(play == 1){
if((alval[6] - alval[0]) >= -potval/100 && (alval[3] - alval[0]) >= -potval/100 && (alval[6] - alval[3]) >= -potval/100){
play = 0;
digitalWrite(ledPin, LOW); // sets the LED off
}
}
else{
if((alval[6] - alval[0]) <= -potval/50 && (alval[3] - alval[0]) <= -potval/50 && (alval[6] - alval[3]) <= -potval/50){ play = 1; digitalWrite(ledPin, HIGH); // sets the LED on } } Serial.println (alval[0]); Serial.println (alval[6] - alval[0]); for(int i = 5; i >= 0; i--){
alval[i+1] = alval[i];
}


if(play == 0){
note = 17;
}

if(play == 1){
if (hole1.isPressed())
{
if(hole2.isPressed())
{
if(hole3.isPressed())
{
if(hole4.isPressed())
{
note=6;
}
else
{
note=10;
}
}
else
{
if(hole4.isPressed())
{
note=5;
}
else
{
note=1;
}
}
}
else
{
if(hole3.isPressed())
{
if(hole4.isPressed())
{
note=7;
}
else
{
note=11;
}
}
else
{
if(hole4.isPressed())
{
note=3;
}
else
{
note=14;
}
}
}
}
else
{
if(hole2.isPressed())
{
if(hole3.isPressed())
{
if(hole4.isPressed())
{
note=8;
}
else
{
note=12;
}
}
else
{
if(hole4.isPressed())
{
note=4;
}
else
{
note=15;
}
}
}
else
{
if(hole3.isPressed())
{
if(hole4.isPressed())
{
note=9;
}
else
{
note=13;
}
}
else
{
if(hole4.isPressed())
{
note=2;
}
else
{
note=16;
}
}
}
}
}
//Serial.println(note);

if(note == 17){
OCR1A = 30;
}
else{
note_val = freq[note-1];
//Serial.println(note_val);

top = (16000000 / (note_val * 2 * 8)) -1;
OCR1A = top;
}

delay(10);

}