Advertisement
Guest User

HCI Lab 2, Group 12

a guest
Mar 6th, 2013
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.70 KB | None | 0 0
  1. int length = 15; // the number of notes
  2. char notes[] = "ccggaagffeeddc "; // a space represents a rest
  3. int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
  4.  
  5.  
  6. char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  7. int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
  8.  
  9.  
  10. int tempo = 300;
  11. int speakerPin = 8;
  12. int inputPin = 0;
  13.  
  14. const int groundpin = A2; // analog input pin 2
  15. const int powerpin = A0; // analog input pin 0
  16. const int xpin = A5; // x-axis of the accelerometer
  17. const int ypin = A4; // y-axis
  18. const int zpin = A3; // z-axis
  19.  
  20. void playTone(int tone, int duration) {
  21.   Serial.print("tone is ");
  22.   Serial.println(tone);
  23.   for (long i = 0; i < duration * 1000L; i += tone * 2) {
  24.     digitalWrite(speakerPin, HIGH);
  25.     delayMicroseconds(tone);
  26.     digitalWrite(speakerPin, LOW);
  27.     delayMicroseconds(tone);
  28.   }
  29. }
  30.  
  31. void playNote(char note, int duration) {
  32.   // play the tone corresponding to the note name
  33.   for (int i = 0; i < 8; i++) {
  34.     if (names[i] == note) {
  35.       playTone(tones[i], duration);
  36.     }
  37.   }
  38. }
  39.  
  40. /*
  41. * getVoltage() – return the voltage on the analoge input defined by pin
  42. *
  43. */
  44. float getVoltage(int pin) {
  45. return (analogRead(pin) * .004882813);
  46. // reading a digital pin returns a value between 0 and 1024, which corresponds to a voltage range
  47. // between 0V and 5V.
  48. // So, one digital step means 5V/1024 = ~0.005V, or about 5 millivolt
  49. }
  50.  
  51. void setup() {
  52.   Serial.begin(9600);
  53.   pinMode(speakerPin, OUTPUT);
  54.  // Provide ground and power by using the analog inputs as normal
  55.  // digital pins. This makes it possible to directly connect the
  56.  // breakout board to the Arduino. If you use the normal 5V and
  57.  // GND pins on the Arduino, you can remove these lines.
  58.  pinMode(groundpin, OUTPUT);
  59.  // pinMode(powerpin, OUTPUT);
  60.  digitalWrite(groundpin, LOW);
  61.  digitalWrite(powerpin, HIGH);
  62. }
  63.  
  64. void playInput(int input) {
  65.   int i;
  66.   Serial.print("pot input: ");
  67.   Serial.println(input);
  68.   for (i = 0; i <= 1023; i++) {
  69.     if (input < i*(1024/8)) {
  70.       // playNote('c', 2500);
  71.       playTone(tones[i-1], 250);
  72.       break;
  73.     }
  74.   }
  75. }
  76.  
  77. void loop() {
  78.   int input = analogRead(inputPin);
  79.  
  80.   double xval = analogRead(xpin);
  81.   double yval = analogRead(ypin);
  82.   double zval = analogRead(zpin);
  83.  
  84.   double mag_squared = xval * xval + yval * yval + zval * zval;
  85.   if (mag_squared > 450000) {
  86.     Serial.println("found note");
  87.     playInput(input);
  88.     delay(150);
  89.   }
  90.  
  91.   delay(10);
  92.   // for (int i = 0; i < length; i++) {
  93.   //   if (notes[i] == ' ') {
  94.   //     delay(beats[i] * tempo); // rest
  95.   //   } else {
  96.   //     playNote(notes[i], beats[i] * tempo);
  97.   //   }
  98.  
  99.   //   // pause between notes
  100.   //   delay(tempo / 2);
  101.   // }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement