Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int length = 15; // the number of notes
- char notes[] = "ccggaagffeeddc "; // a space represents a rest
- int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
- char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
- int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
- int tempo = 300;
- int speakerPin = 8;
- int inputPin = 0;
- const int groundpin = A2; // analog input pin 2
- const int powerpin = A0; // analog input pin 0
- const int xpin = A5; // x-axis of the accelerometer
- const int ypin = A4; // y-axis
- const int zpin = A3; // z-axis
- void playTone(int tone, int duration) {
- Serial.print("tone is ");
- Serial.println(tone);
- for (long i = 0; i < duration * 1000L; i += tone * 2) {
- digitalWrite(speakerPin, HIGH);
- delayMicroseconds(tone);
- digitalWrite(speakerPin, LOW);
- delayMicroseconds(tone);
- }
- }
- void playNote(char note, int duration) {
- // play the tone corresponding to the note name
- for (int i = 0; i < 8; i++) {
- if (names[i] == note) {
- playTone(tones[i], duration);
- }
- }
- }
- /*
- * getVoltage() – return the voltage on the analoge input defined by pin
- *
- */
- float getVoltage(int pin) {
- return (analogRead(pin) * .004882813);
- // reading a digital pin returns a value between 0 and 1024, which corresponds to a voltage range
- // between 0V and 5V.
- // So, one digital step means 5V/1024 = ~0.005V, or about 5 millivolt
- }
- void setup() {
- Serial.begin(9600);
- pinMode(speakerPin, OUTPUT);
- // Provide ground and power by using the analog inputs as normal
- // digital pins. This makes it possible to directly connect the
- // breakout board to the Arduino. If you use the normal 5V and
- // GND pins on the Arduino, you can remove these lines.
- pinMode(groundpin, OUTPUT);
- // pinMode(powerpin, OUTPUT);
- digitalWrite(groundpin, LOW);
- digitalWrite(powerpin, HIGH);
- }
- void playInput(int input) {
- int i;
- Serial.print("pot input: ");
- Serial.println(input);
- for (i = 0; i <= 1023; i++) {
- if (input < i*(1024/8)) {
- // playNote('c', 2500);
- playTone(tones[i-1], 250);
- break;
- }
- }
- }
- void loop() {
- int input = analogRead(inputPin);
- double xval = analogRead(xpin);
- double yval = analogRead(ypin);
- double zval = analogRead(zpin);
- double mag_squared = xval * xval + yval * yval + zval * zval;
- if (mag_squared > 450000) {
- Serial.println("found note");
- playInput(input);
- delay(150);
- }
- delay(10);
- // for (int i = 0; i < length; i++) {
- // if (notes[i] == ' ') {
- // delay(beats[i] * tempo); // rest
- // } else {
- // playNote(notes[i], beats[i] * tempo);
- // }
- // // pause between notes
- // delay(tempo / 2);
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement