Hi, I am working on a project including the teensyduino and the LV-MaxSonar-EZ0 Ultrasonic Range Finder
I am trying to get the teensyduino to output a tone that changes in pitch depending on what distance the rangefinder is detecting
here is the arduino code:
//#include <math.h>
int VIBRATION_PIN = 19;
int SOUNDOUT_PIN = 17;
int INPUT_PIN = 18;
int VIBRATE = 0, SOUND = 1, CURRENT_MODE = 0;
double scaleFactor;
unsigned long duration, value;
long inputVal; //pwm value from the rangefinder
int x = 1;
long FLOOR, CEILING, DIST;
int TONE_CEILING = 400;
int count = 0, ceiling = 1000;
void setup(){
pinMode( INPUT_PIN, INPUT);
digitalWrite( INPUT_PIN, HIGH); // turn on internal pullup, pin will be HIGH unless connected to GND by switch
scaleFactor = 0; //ratio comprised of rangefinder input value over max rangefinder value, applied to max output(audio or vibration) to get output value
//setMode( SOUND ); //just do this for now until we get vibration working
//Serial.begin( 9600 );
FLOOR = 300;
CEILING = 10,000;
DIST = CEILING - FLOOR;
Serial.println( "Done" );
}
//main loop
void loop(){
//pinMode( INPUT_PIN, INPUT );
//duration = pulseIn( INPUT_PIN, HIGH );
//duration = microsecondsToInches( duration );
//Serial.println( duration );
count++;
if( count > ceiling ){
duration = ping();
count = 0;
}
//send feedback in appropriate mode
//if( CURRENT_MODE == SOUND){
pinMode( SOUNDOUT_PIN, OUTPUT );
writeTone( duration );
//}
}
unsigned long ping(){
/*pinMode( INPUT_PIN, OUTPUT );
digitalWrite( INPUT_PIN, LOW );
delayMicroseconds( 2 );
digitalWrite( INPUT_PIN, HIGH );
delayMicroseconds( 5 );
digitalWrite( INPUT_PIN, LOW );*/
pinMode( INPUT_PIN, INPUT );
//digitalWrite( INPUT_PIN, HIGH );
//calculate a ratio from the found range/max range and apply it to the max tone to calculate the tone to output
duration = pulseIn( INPUT_PIN, HIGH );
scaleFactor = ( DIST - duration ) / DIST;
inputVal = scaleFactor * TONE_CEILING;
//Serial.println( (int)value );
return inputVal;
}
//send tone signal
void writeTone( int freq ){
digitalWrite( SOUNDOUT_PIN, HIGH );
delayMicroseconds( freq );
digitalWrite( SOUNDOUT_PIN, LOW );
delayMicroseconds( freq );
}
though the arduino does output a tone, it is a very low tone and does not change with the rangefinder whatsoever.
we have tested the rangefinder with a multimeter and the voltage changed as we moved our hand farther and closer, so we know the rangefinder is working. We also called writeTone( 200 ) and a high-pitched tone came out, so we know that that works correctly. We have checked the pin connections and they correspond correctly with the pin variables we have defined.
what are we doing wrong?
edit: sorry for not posting this in interfacing