Skip to content

Instantly share code, notes, and snippets.

@brianfay
Created August 10, 2014 19:04
Show Gist options
  • Save brianfay/d9332cdd2d24364a05ab to your computer and use it in GitHub Desktop.
Save brianfay/d9332cdd2d24364a05ab to your computer and use it in GitHub Desktop.
Add udev rule - triggered when Arduino Uno is connected
#Rule to run script when Arduino Uno is connected
KERNEL=="ttyACM0",ACTION=="add",RUN+="/path/to/script"
// Six button arduino MIDI device
#include <midi_Settings.h>
#include <midi_Defs.h>
#include <midi_Message.h>
#include <midi_Namespace.h>
#include <MIDI.h>
int buttonStates[6];
int defaultStates[6];
//send notes on this channel:
MIDI_CREATE_DEFAULT_INSTANCE();
int MIDIChannel = 10;
void setup(){
//start serial connection
//configure pin2 as an input and enable the internal pull-up resistor
for(int i = 0; i < 6; i++){
pinMode(i+2, INPUT_PULLUP);
}
pinMode(13, OUTPUT);
for(int i = 0; i < 6; i++){
buttonStates[i] = defaultStates[i] = digitalRead(i+2);
}
MIDI.begin(4);
Serial.begin(115200);
}
void loop(){
//read the pushbutton value into a variable
int sensorValues[6];
for(int i = 0; i < 6; i++){
sensorValues[i] = digitalRead(i+2);
if(buttonStates[i] != sensorValues[i]){
if(sensorValues[i] != defaultStates[i]){
//Serial.print("button ");
//Serial.print(i);
//Serial.println(" ON");
MIDI.sendNoteOn(i,127, MIDIChannel);
}
else{
//Serial.print("button ");
//Serial.print(i);
//Serial.println(" OFF");
MIDI.sendNoteOff(i, 0, MIDIChannel);
}
}
buttonStates[i] = sensorValues[i];
}
// Keep in mind the pullup means the pushbutton's
// logic is inverted. It goes HIGH when it's open,
// and LOW when it's pressed. Turn on pin 13 when the
// button's pressed, and off when it's not:
if (sensorValues[0] == HIGH) {
digitalWrite(13, LOW);
}
else {
digitalWrite(13, HIGH);
}
}
#prints device info about arduino uno
udevadm info -a -p $(udevadm info -q path -n /dev/ttyACM0) | less
#!/bin/sh
#connects arduino Uno as a MIDI device
ttymidi -s /dev/ttyACM0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment