// // Based on sample code for SpikenzieLabs.com' Serial-to-MIDI library // #define BUTTON_ONE 7 #define BUTTON_TWO 8 #define BUTTON_THREE 9 #define BUTTON_FOUR 10 #define LDR_ONE 0 #define LDR_TWO 1 #define NOTE_ON_DELAY 200 int val = 0; float fval = 0; void setup() { Serial.begin(57600); // Default speed of the Serial to MIDI Converter serial port pinMode(BUTTON_ONE, INPUT); pinMode(BUTTON_TWO, INPUT); pinMode(BUTTON_THREE, INPUT); pinMode(BUTTON_FOUR, INPUT); } void loop() { if (digitalRead(BUTTON_ONE) == HIGH) ButtonActivate(1); if (digitalRead(BUTTON_TWO) == HIGH) ButtonActivate(2); if (digitalRead(BUTTON_THREE) == HIGH) ButtonActivate(3); if (digitalRead(BUTTON_FOUR) == HIGH) ButtonActivate(4); // Send LDR_ONE input on CC1 and CC2 val = ScaleAnalog(LDR_ONE); // 0-1023 scaling SendMidi(176, 1, val); // CC1 SendMidi(176, 2, val); // CC2 // Send LDR_TWO input on CC3 and CC4 val = ScaleAnalog(LDR_TWO); SendMidi(176, 3, val); // CC3 SendMidi(176, 4, val); // CC4 } int ScaleAnalog(int pin) { fval = analogRead(pin); // Analog is 0 to 1023, but useful range of diode is smaller, say 100-800 //fval = fval/1023.0f; fval = (fval-100)/800; if (fval < 0.0) fval = 0.0; // Make sure we're at least 0.0 if (fval > 1.0) fval = 1.0; // Make sure we're no greater than 1.0 fval = fval*127; return int(fval); } void ButtonActivate(int index) { int noteToPlay = -1; switch (index) { case 1: noteToPlay = 64; // E3 break; case 2: noteToPlay = 60; // C3 break; case 3: noteToPlay = 65; // F3 break; case 4: noteToPlay = 62; // D3 break; } if (noteToPlay != -1) PlayNote(noteToPlay); } void PlayNote(int note) { SendMidi(144, note, 127); // Note on delay(NOTE_ON_DELAY); // Sustain SendMidi(128, note, 127); // Note off } void SendMidi(unsigned char msg, unsigned char pitch, unsigned char velocity) { Serial.print(msg); Serial.print(pitch); Serial.print(velocity); }