This works with the 2x16 serial display from parallax. The function playnote takes any number between one and 150. I also have a compass connected so I can tell which direct is that I'm pinging, but the out is incomprehensible. This is designed to work with png sensor. For some reason my project only works when the device is connected through USB and not when connect to a 9 volt adapter.
I'm using..
1 Arduino Uno rev 3
1 2x16 serial display from parallax
1 PING))) Ultrasonic Distance Sensor from parallax
1 Compass Module 3-Axis HMC5883L
photo is attached.
const int TxPin = 6;
const int pingPin = 11;
unsigned int scale, duration, inches;
const int sensitivity = 3;
#include <SoftwareSerial.h>
#include <Wire.h>
#define Addr 0x1E // 7-bit address of HMC5883 compass
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);
void setup() {
int scale = 0;
mySerial.begin(9600);
delay(100);
Wire.begin();
// Set operating mode to continuous
Wire.beginTransmission(Addr);
Wire.write(byte(0x02));
Wire.write(byte(0x00));
Wire.endTransmission();
pinMode(TxPin, OUTPUT);
digitalWrite(TxPin, HIGH);
mySerial.write(17); // Turn backlight on
}
void playNote(int myDistance){
int note = floor(myDistance/sensitivity);
// String sa3;
if(note>=1&¬e<=12){
scale = 0;
}
if(note>=13&¬e<=24){
scale = 1;
}
if(note>=25&¬e<=36){
scale = 2;
}
if(note>=37&¬e<=48){
scale = 3;
}
if(note>=49&¬e<=60){
scale = 4;
}
int playNote = 219 + (note-(scale12));
int offset = note-(scale12);
int playScale = 215 + scale;
mySerial.write(playScale); //bass low octave
mySerial.write(208);
mySerial.write(playNote);
mySerial.print(inches); // 64th
}
void loop() {
int x, y, z;
mySerial.write(12);
// Initiate communications with compass
Wire.beginTransmission(Addr);
Wire.write(byte(0x03)); // Send request to X MSB register
Wire.endTransmission();
Wire.requestFrom(Addr, 6); // Request 6 bytes; 2 bytes per axis
if(Wire.available() <=6) {
// If 6 bytes available
mySerial.print("Wire av");
x = Wire.read() << 8 | Wire.read();
z = Wire.read() << 8 | Wire.read();
y = Wire.read() << 8 | Wire.read();
}
// Print raw values
mySerial.print("X=");
mySerial.print(x);
mySerial.print(", Y=");
mySerial.print(y);
mySerial.print(", Z=");
mySerial.println(z);
pinMode(pingPin, OUTPUT); // Set pin to OUTPUT
digitalWrite(pingPin, LOW); // Ensure pin is low
delayMicroseconds(2);
digitalWrite(pingPin, HIGH); // Start ranging
delayMicroseconds(5); // with 5 mi1crosecond burst
digitalWrite(pingPin, LOW); // End ranging
pinMode(pingPin, INPUT); // Set pin to INPUT
duration = pulseIn(pingPin, HIGH); // Read echo pulse
inches = duration / 74 / 2;
int inverseIN = 150 - inches;
playNote(inverseIN);
}