Skip to content

Instantly share code, notes, and snippets.

@tatsuro-ueda
Created July 10, 2010 23:13
Show Gist options
  • Save tatsuro-ueda/471120 to your computer and use it in GitHub Desktop.
Save tatsuro-ueda/471120 to your computer and use it in GitHub Desktop.
const int xAxisPin = 0;
const int yAxisPin = 1;
const int zAxisPin = 2;
#include "pitches.h"
// notes in the melody:
int melody[] = {
NOTE_C4, NOTE_G3,NOTE_G3, NOTE_A3, NOTE_G3,0, NOTE_B3, NOTE_C4};
// note durations: 4 = quarter note, 8 = eighth note, etc.:
int noteDurations[] = {
4, 8, 8, 4,4,4,4,4 };
#include <Servo.h>
Servo myservo;
void setup() {
myservo.attach(9);
myservo.write(0);
}
void loop() {
int xAxisValue = analogRead(xAxisPin);
int yAxisValue = analogRead(yAxisPin);
int zAxisValue = analogRead(zAxisPin);
float xAxisG = map(xAxisValue, 0, 1023, -2500, 2500) / 1000.0;
float yAxisG = map(yAxisValue, 0, 1023, -2500, 2500) / 1000.0;
float zAxisG = map(zAxisValue, 0, 1023, -2500, 2500) / 1000.0;
if(yAxisG > 0.3){
myservo.write(180);
for (int thisNote = 0; thisNote < 8; thisNote++) {
// to calculate the note duration, take one second
// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000/noteDurations[thisNote];
tone(8, melody[thisNote],noteDuration);
// to distinguish the notes, set a minimum time between them.
// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
}
}
myservo.write(0);
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment