Skip to content

Instantly share code, notes, and snippets.

@hhayley
Last active February 14, 2018 11:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hhayley/8e64ad0947b3f3d1475b008c3a8370ac to your computer and use it in GitHub Desktop.
Save hhayley/8e64ad0947b3f3d1475b008c3a8370ac to your computer and use it in GitHub Desktop.
#include <AcceleroMMA7361.h>
AcceleroMMA7361 accelero;
int x;
int y;
int z;
int accelState = 0;
int lastAccelState = 0;
long interval = 0;
long previousMillis = 0;
int peakValue = 0;
int threshold = 30;
int noise = 5;
void setup()
{
Serial.begin(9600);
Serial1.begin(31250); //MIDI serial
accelero.begin(13, 12, 11, 10, A0, A1, A2);
accelero.setARefVoltage(3.3); //sets the AREF voltage to 3.3V
accelero.setSensitivity(HIGH); //sets the sensitivity to +/-6G
accelero.calibrate();
}
void loop()
{
unsigned long currentMillis = millis();
//Seesaw
y = accelero.getYAccel();
//Swing
int stretchSensorLow = analogRead(A6) - 330;
int stretchSensor = map(stretchSensorLow, 0, 40, 20, 127);
// interval = stretchSensor;
int thisNote = y + 48;
Serial.println(thisNote);
int thisCommand = 0;
//** Peak Detection
if (stretchSensor > peakValue) {
peakValue = stretchSensor;
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
unsigned int thres = lastAccelState - y;
if (y != lastAccelState) {
thisCommand = 0x90;
} else {
thisCommand = 0x80;
}
midiCommand(thisCommand, thisNote, stretchSensor); // for changing Velocity with stretch sensor value
}
lastAccelState = y;
}
//Peak detection initialize
if (stretchSensor <= threshold - noise) {
if (peakValue > threshold + noise) {
// Serial.println(peakValue);
peakValue = 0;
}
}
/*
//Pitch Bend
if (stretchSensor > 10 ) {
byte msb = highByte(stretchSensor << 1); // high bits
byte lsb = lowByte(stretchSensor); // low bits
bitWrite(lsb, 7, 0);
midiCommand(0xE0, lsb, msb); //pitch bend message
}
*/
//Change Instrument
if (stretchSensor > 100) {
midiCommand(0xC0, 0, 10); // program (change instrumnet (Command, Control#,ControlValue-1)
}
}
// send a 3-byte midi message
void midiCommand(byte cmd, byte data1, byte data2) {
Serial1.write(cmd); // command byte (should be > 127)
Serial1.write(data1); // data byte 1 (should be < 128)
Serial1.write(data2); // data byte 2 (should be < 128)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment