Sonar and MIDI Mapping


hi

i have lv-maxsonar ez1 , using code readouts of distance measurements.  code using that:

code: [select]

//feel free use code.
//please respectful acknowledging author in code if use or modify it.
//author: bruce allen
//date: 23/07/09

//digital pin 7 reading in pulse width maxsonar device.
//this variable constant because pin not change throughout execution of code.
const int pwpin = 7;

//variables needed store values
long pulse, inches, cm;


void setup() {
  //this opens serial connection shoot results pc console
  serial.begin(9600);
}


void loop() {
  pinmode(pwpin, input);
    //used read in pulse being sent maxsonar device.
  //pulse width representation scale factor of 147 per inch.

  pulse = pulsein(pwpin, high);
  //147us per inch
  inches = pulse/147;
  //change inches centimetres
  cm = inches * 2.54;
  serial.print(inches);
  serial.print("in, ");
  serial.print(cm);
  serial.print("cm");
  serial.println();
  delay(500);
}



i have potentiometer sends midi data on din-5 midi connector attached breadboard using code:

code: [select]

/*
* 7.1 midi out controller
* roguescience.org
*
* digital pin 11 (output/pwm) --> led
* digital pin 2 (input) <-- button
* analog pin 0 <-- pot
* tx1 <-- pin 5 (5 pin din aka midi jack)
*
* send midi note when button pressed , sends midi
* cc data when potentiometer turned.
*/

int ledpin = 11;                //choose pin led - needs (3,5,6,9,10, or 11)
int buttonpin = 2;               //choose input pin pushbutton
int potpin = 0;                  //choose input pin potentometer
int buttonval = 0;                    //variable reading button status
int buttonstate = 0;            //variable hold buttons current state
int bouncecheck = 0;            //variable debouncing
int potval = 0;                //variable reading potentiometer value
int mappedpotval = 0;          //variable holding remapped pot value
int prevpotval = 0;               //variable storing our prev pot value
int threshold = 2;            //threshold amount

void setup() {
  pinmode(ledpin, output);      //declare led output
  pinmode(buttonpin, input);     //declare pushbutton input
  serial.begin(31250);            //midi communicates @ 31250 baud
}

void loop(){

  buttonval = digitalread(buttonpin);     //read input value button
  delay(10);                              //wait 10ms
  bouncecheck = digitalread(buttonpin);   //check again
  if(buttonval == bouncecheck){           //if val same not bounce
    if (buttonval == high && buttonstate == 1) {   //check if input high
      digitalwrite(ledpin, low);         //turn led off
      midiout(0x90, 60, 0); //note on (ch 1), middle c, 0 velocity turns note off
      buttonstate = 0;
    }
    if(buttonval == low && buttonstate == 0){
      digitalwrite(ledpin, high);       //turn led on
      midiout(0x90, 60, 127); //note on (ch 1), middle c, velocity 127
      buttonstate = 1;
    }

  }

   potval = analogread(potpin);         //read input potentiometer
   mappedpotval = map(potval, 0, 1023, 0, 127);  //map value 0-127
   if(abs(mappedpotval - prevpotval) >= threshold){
     midiout(0xb0, 7, mappedpotval); //control change (ch 1), controller 7 - default control volume.
     digitalwrite(ledpin, high);
     prevpotval = mappedpotval;
   }
   else{
      digitalwrite(ledpin, low);
   }

}

void midiout(char command, char value1, char value2) {
  serial.print(command, byte);
  serial.print(value1, byte);
  serial.print(value2, byte);

}


the sonar inputting values on pulse width, whereas pot in midi example inputting values through analog pin.


what code need replace analog potentiometer sonar input on pulse width?

thanks

code: [select]
   potval = analogread(potpin);         //read input potentiometer
here, potval number relates position of pot wiper.

code: [select]
  pulse = pulsein(pwpin, high);
here, pulse number relates distance whatever ping sensor seeing.

code: [select]
   mappedpotval = map(potval, 0, 1023, 0, 127);  //map value 0-127
here, potval mapped range of 0-127.

you need like
code: [select]
   mappedpulse = map(pulse, start, end, 0, 127);  //map value 0-127

you need work out start , end of range of numbers ping sensor returns. don't have one, don't know.

ps. please don't cross post. if didn't understand posted on http://arduino.cc/forum/index.php/topic,99709.msg748238.html, have said so, , i, or else, have expanded on it.


Arduino Forum > Using Arduino > Programming Questions > Sonar and MIDI Mapping


arduino

Comments

Popular posts from this blog

Thread: PKI Client 5.00 install (for eToken Pro)

ATmega2560-Arduino Pin Mapping

Crossfader Arduino Tutorial