mem: the map function seems like the perfect solution but I have been trying to implement it with little success:
/* New Variables */
int delay_time = 40; // delay for this amount each write cycle.
byte MIDI_channel = 1;
byte cc_number = 127;
byte printing_byte = 0;
int baud_rate = 31250;
/* Ultrasound Sensor
*------------------
*
* Reads values (00014-01199) from an ultrasound sensor (3m sensor)
* and writes the values to the serialport.
*
* http://www.xlab.se | http://www.0j0.org
* copyleft 2005 Mackie for XLAB | DojoDave for DojoCorp
*
*/
// CODE EDITED BY SEBASTIAN TOMCZAK 23 APRIL 2008
int ultraSoundSignal = 11; // Ultrasound signal pin
int val = 0;
int ultrasoundValue = 0;
int timecount = 0;
int brightness = 0;// Echo counter
int ledPin = 13;
int redPin = 2;
int bluePin = 3;
int yellowPin = 4;
int whitePin = 10;// LED connected to digital pin 13
void setup() {
Serial.begin(baud_rate);
pinMode(ledPin, OUTPUT);
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(yellowPin, OUTPUT);
pinMode(whitePin, OUTPUT); // Sets the digital pin as output
MIDI_channel = MIDI_channel - 1;
}
void loop() {
timecount = 0;
val = 0;
pinMode(ultraSoundSignal, OUTPUT); // Switch signalpin to output
/* Send low-high-low pulse to activate the trigger pulse of the sensor
* -------------------------------------------------------------------
*/
digitalWrite(ultraSoundSignal, LOW); // Send low pulse
delayMicroseconds(2); // Wait for 2 microseconds
digitalWrite(ultraSoundSignal, HIGH); // Send high pulse
delayMicroseconds(5); // Wait for 5 microseconds
digitalWrite(ultraSoundSignal, LOW); // Holdoff
/* Listening for echo pulse
* -------------------------------------------------------------------
*/
pinMode(ultraSoundSignal, INPUT); // Switch signalpin to input
val = digitalRead(ultraSoundSignal); // Append signal value to val
while(val == LOW) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
}
while(val == HIGH) { // Loop until pin reads a high value
val = digitalRead(ultraSoundSignal);
timecount = timecount +1; // Count echo pulse time
}
/* Writing out values to the serial port
* -------------------------------------------------------------------
*/
ultrasoundValue = timecount;
ultrasoundValue = map(ultrasoundValue, 0, 127, 127, 0);
// Append echo pulse time to ultrasoundValue
if((ultrasoundValue >= 110) && (ultrasoundValue < 127)){
digitalWrite(redPin, HIGH);
delay(100);
digitalWrite(redPin, LOW);
digitalWrite(bluePin, HIGH);
delay(100);
digitalWrite(bluePin, LOW);
digitalWrite(redPin, HIGH);
delay(100);
digitalWrite(redPin, LOW);
digitalWrite(bluePin, HIGH);
delay(100);
digitalWrite(bluePin, LOW);
}
else{
digitalWrite(redPin, LOW);
digitalWrite(bluePin, LOW);
}
/* BEGIN EDITED CODE */
ultrasoundValue = ultrasoundValue - 14;
ultrasoundValue = ultrasoundValue / 5;
brightness = ultrasoundValue;
analogWrite(whitePin, brightness);
if(ultrasoundValue > 127) {
printing_byte = 127;
}
else {
printing_byte = ultrasoundValue ;
}
Serial.print(B10110000 + MIDI_channel,BYTE);
Serial.print(cc_number,BYTE);
Serial.print(printing_byte,BYTE);
/* END EDITED CODE */
/* Lite up LED if any value is passed by the echo pulse
* -------------------------------------------------------------------
*/
if(timecount > 0){
digitalWrite(ledPin, HIGH);
}
/* Delay of program
* -------------------------------------------------------------------
*/
delay(delay_time);
}
All I want is for the MIDI data to be inversed, so instead of going from 0 to 127 it went the opposite way.
On a slightly side note - I have now attached a length of LED's that get brighter when the person comes closer to the sensor, however the fade is a bit jerky (i.e. not like a smooth fade) - is that because the midi data is 0 - 127 and the LED wants to have the brightness controlled from 0 to 255? or is it just bound to be a bit jerky.