Created
July 9, 2015 12:57
-
-
Save andysheen-zz/a9e44170479389acb8a8 to your computer and use it in GitHub Desktop.
Midi to DIY Synth
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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