@@ 1,127 @@
+
+#include "pitches.h"
+
+//joystick
+#define VRX_PIN A0 // Arduino pin connected to VRX pin
+#define VRY_PIN A1 // Arduino pin connected to VRY pin
+
+// distance sensor
+#define trigPin 3
+#define echoPin 4
+
+//joystick
+int xValue = 0; // To store value of the X axis
+int yValue = 0; // To store value of the Y axis
+float new_xValue = 0;
+float new_yValue = 0;
+
+// distance sensor
+long duration;
+int distance;
+
+bool doit = true;
+
+// notes in the melody:
+int melody[] = {
+ // 0 1 2 3 4 5 6 7 8 9 10 11
+ NOTE_C3, NOTE_C5, NOTE_C6, NOTE_C7, NOTE_B3, NOTE_B5, NOTE_B6, NOTE_B7, NOTE_A3, NOTE_A5, NOTE_A6, NOTE_A7
+};
+
+// leaving the duration here in a list in case I want to change it later
+int noteDurations[] = {
+ 4, 4, 4, 4, 4, 4, 4, 4,4,4,4,4
+};
+
+void play_it(int thisNote) {
+ 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);
+ // stop the tone playing:
+ noTone(8);
+}
+
+
+void setup() {
+ Serial.begin(9600) ;
+ pinMode(trigPin, OUTPUT);
+ pinMode(echoPin, INPUT);
+}
+
+void loop() {
+
+ // distance sensor logics
+ // Clear the trigPin by setting it LOW:
+ digitalWrite(trigPin, LOW);
+ delayMicroseconds(5);
+
+ // Trigger the sensor by setting the trigPin high for 10 microseconds:
+ digitalWrite(trigPin, HIGH);
+ delayMicroseconds(10);
+ digitalWrite(trigPin, LOW);
+
+ // Read the echoPin, pulseIn() returns the duration (length of the pulse) in microseconds:
+ duration = pulseIn(echoPin, HIGH);
+ // Calculate the distance:
+ distance = duration * 0.034 / 2;
+
+
+ // read analog X and Y analog values
+ xValue = analogRead(VRX_PIN);
+ yValue = analogRead(VRY_PIN);
+ new_xValue = xValue/100;
+ new_yValue = yValue/100;
+
+ int thisNote;
+
+ // print data to Serial Monitor on Arduino IDE
+ Serial.print("x = ");
+ Serial.print(new_xValue,0);
+ Serial.print(", y = ");
+ Serial.println(new_yValue,0);
+ Serial.print("distance = ");
+ Serial.println(distance);
+
+ thisNote = 0;
+
+
+ if ((distance >= 1) && (distance < 5)){
+ thisNote = 0;
+
+ } else if ((distance >= 5) && (distance < 12)){
+ thisNote = 5;
+
+ } else if (distance >= 12){
+ thisNote = 10;
+
+ }
+
+
+ if ((new_xValue == 2) && (new_yValue == 8)){
+ //right
+ thisNote = thisNote + 1;
+ } else if ((new_xValue == 0) && (new_yValue == 4)){
+ //down
+ thisNote = thisNote + 2;
+ } else if ((new_xValue == 4) && (new_yValue == 0)){
+ //left
+ thisNote = thisNote + 3;
+ }
+
+ // if up, it's the default
+
+ if ((new_xValue == 4) && (new_yValue == 4)) {
+ doit = false;
+ } else {
+ doit = true;
+ }
+
+ if ( doit ){
+ play_it(thisNote);
+ }
+
+
+ delay(200);
+}