Hi
I have a LV-MaxSonar EZ1 and I using this code to get readouts of distance measurements. This is the code that I am using for that:
//Feel free to use this code.
//Please be respectful by acknowledging the author in the code if you use or modify it.
//Author: Bruce Allen
//Date: 23/07/09
//Digital pin 7 for reading in the pulse width from the MaxSonar device.
//This variable is a constant because the pin will not change throughout execution of this code.
const int pwPin = 7;
//variables needed to store values
long pulse, inches, cm;
void setup() {
//This opens up a serial connection to shoot the results back to the PC console
Serial.begin(9600);
}
void loop() {
pinMode(pwPin, INPUT);
//Used to read in the pulse that is being sent by the MaxSonar device.
//Pulse Width representation with a scale factor of 147 uS per Inch.
pulse = pulseIn(pwPin, HIGH);
//147uS per inch
inches = pulse/147;
//change inches to centimetres
cm = inches * 2.54;
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(500);
}
I have a potentiometer that sends MIDI data over a DIN-5 MIDI connector attached to my breadboard using this code:
/*
* 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 a MIDI note when the button is pressed and sends MIDI
* CC data when a potentiometer is turned.
*/
int ledPin = 11; //choose the pin for the LED - needs to be (3,5,6,9,10, or 11)
int buttonPin = 2; //choose the input pin for a pushbutton
int potPin = 0; //choose the input pin for a potentometer
int buttonVal = 0; //variable for reading the button status
int buttonState = 0; //variable to hold the buttons current state
int bounceCheck = 0; //variable for debouncing
int potVal = 0; //variable for reading potentiometer value
int mappedPotVal = 0; //variable for holding remapped pot value
int prevPotVal = 0; //variable for storing our prev pot value
int THRESHOLD = 2; //threshold amount
void setup() {
pinMode(ledPin, OUTPUT); //declare LED as output
pinMode(buttonPin, INPUT); //declare pushbutton as input
Serial.begin(31250); //MIDI communicates at 31250 baud
}
void loop(){
buttonVal = digitalRead(buttonPin); //read input value from button
delay(10); //wait 10ms
bounceCheck = digitalRead(buttonPin); //check again
if(buttonVal == bounceCheck){ //if val is the same then not a bounce
if (buttonVal == HIGH && buttonState == 1) { //check if the input is HIGH
digitalWrite(ledPin, LOW); //turn LED OFF
midiOUT(0x90, 60, 0); //Note ON (CH 1), middle C, zero 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 from potentiometer
mappedPotVal = map(potVal, 0, 1023, 0, 127); //map value to 0-127
if(abs(mappedPotVal - prevPotVal) >= THRESHOLD){
midiOUT(0xB0, 7, mappedPotVal); //Control Change (Ch 1), Controller 7 - default control for 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 is inputting values over Pulse Width, whereas the Pot in the MIDI example is inputting values through an Analog pin.
What code will I need to replace the Analog Potentiometer with a Sonar input over Pulse Width?
Thanks