Skip to content

Instantly share code, notes, and snippets.

@andysheen-zz
Created July 9, 2015 12:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andysheen-zz/a9e44170479389acb8a8 to your computer and use it in GitHub Desktop.
Save andysheen-zz/a9e44170479389acb8a8 to your computer and use it in GitHub Desktop.
Midi to DIY Synth
#include <TimerOne.h>
const byte PWMDAC1pin = 9; // PWM DAC, only pins 9 and 10 are allowed
const byte PWMDAC2pin = 10; // example using second DAC
const byte period = 32; // for 8 bit DAC
byte commandByte;
byte noteByte;
byte velocityByte;
byte noteOn = 144;
//light up led at pin 13 when receiving noteON message with note = 60
void setup(){
pinMode(PWMDAC1pin, OUTPUT);
Timer1.initialize(period);
Serial.begin(115200);
pinMode(13,OUTPUT);
digitalWrite(13,LOW);
pinMode(9,OUTPUT);
}
void checkMIDI(){
do{
if (Serial.available()){
commandByte = Serial.read();//read first byte
noteByte = Serial.read();//read next byte
velocityByte = Serial.read();//read final byte
if (commandByte == noteOn){//if note on message
//check if note == 60 and velocity > 0
if ( velocityByte > 0){
digitalWrite(13,HIGH);//turn on led
// analogWrite(9,noteByte);
Timer1.pwm(PWMDAC1pin,(abs)((int)noteByte*8-1023));
pinMode(5,INPUT);
}
}
else if(commandByte == 128){
digitalWrite(13,LOW);
digitalWrite(9,LOW);
pinMode(5,OUTPUT);
}
}
}
while (Serial.available() > 2);//when at least three bytes available
}
void loop(){
checkMIDI();
delay(1);
//digitalWrite(13,LOW);//turn led off
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment