Skip to content

Instantly share code, notes, and snippets.

@sivieri
Created August 31, 2011 15:09
Show Gist options
  • Save sivieri/1183790 to your computer and use it in GitHub Desktop.
Save sivieri/1183790 to your computer and use it in GitHub Desktop.
Code for reading from two IR proximity sensors with Arduino and outputting their values as MIDI CC
/*
Copyright (c) 2011 Alessandro Sivieri <alessandro.sivieri@gmail.com>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License version 3 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#define BUFFER 5
int rightLed = 11;
int leftLed = 3;
int rightButton = 12;
int leftButton = 2;
int rightSensor = 0;
int leftSensor = 1;
int channel = 0;
int previousIntRight = HIGH, previousIntLeft = HIGH, pRight = 0, pLeft = 0, previousValueRight = 100, previousValueLeft = 100;
int valueRight[BUFFER], valueLeft[BUFFER];
boolean previousRight = false, previousLeft = false;
int mean(int values[], int n) {
int total = 0, i;
for (i = 0; i < n; ++i) {
total += values[i];
}
return (int) total / n;
}
void noteOn(int cmd, int pitch, int velocity) {
Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}
void setup() {
Serial.begin(31250);
pinMode(rightLed, OUTPUT);
pinMode(leftLed, OUTPUT);
pinMode(rightButton, INPUT);
pinMode(leftButton, INPUT);
delay(5000);
}
void loop() {
int curRight, curLeft;
// right
curRight = digitalRead(rightButton);
if (curRight != previousIntRight) {
if (curRight == LOW) {
previousRight = !previousRight;
}
previousIntRight = curRight;
}
if (previousRight) {
analogWrite(rightLed, 255);
noteOn(0xB0 | (channel & 0x0F), 0x3D, map(previousValueRight, 100, 600, 0, 127));
}
else {
curRight = analogRead(rightSensor);
curRight = constrain(curRight, 100, 600);
valueRight[pRight++] = curRight;
if (pRight == BUFFER) {
curRight = mean(valueRight, BUFFER);
previousValueRight = curRight;
analogWrite(rightLed, map(curRight, 100, 600, 0, 255));
noteOn(0xB0 | (channel & 0x0F), 0x3D, map(curRight, 100, 600, 0, 127));
pRight = 0;
}
}
// left
curLeft = digitalRead(leftButton);
if (curLeft != previousIntLeft) {
if (curLeft == LOW) {
previousLeft = !previousLeft;
}
previousIntLeft = curLeft;
}
if (previousLeft) {
analogWrite(leftLed, 255);
noteOn(0xB0 | (channel & 0x0F), 0x3C, map(previousValueLeft, 100, 600, 0, 127));
}
else {
curLeft = analogRead(leftSensor);
curLeft = constrain(curLeft, 100, 600);
valueLeft[pLeft++] = curLeft;
if (pLeft == BUFFER) {
curLeft = mean(valueLeft, BUFFER);
previousValueLeft = curLeft;
analogWrite(leftLed, map(curLeft, 100, 600, 0, 255));
noteOn(0xB0 | (channel & 0x0F), 0x3C, map(curLeft, 100, 600, 0, 127));
pLeft = 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment