MIDI

Dependencies:   mbed USBDevice

Revision:
1:185dc77ef1ee
Parent:
0:81e431d882ef
--- a/main.cpp	Mon Jul 06 00:47:29 2020 +0000
+++ b/main.cpp	Wed Jul 08 03:33:13 2020 +0000
@@ -1,50 +1,101 @@
-// Edited from the Hello World example for the USBMIDI library
-// to play notes from the PwmOut using beep.....
- 
+/*
+ * Copyright (c) 2006-2020 Arm Limited and affiliates.
+ * SPDX-License-Identifier: Apache-2.0
+ */
 #include "mbed.h"
 #include "USBMIDI.h"
-#include "beep.h"
- 
-//USBMIDI object
+
 USBMIDI midi;
- 
-Beep buzzer(D13); // piezo one of two( this only needs one,
-Beep buzzer2(D12); // but piezo two made it more fun).....
-AnalogIn pot (D11); // and a potentiometer.....
- 
-int freq = 0;
- 
-// an array of frequencies from C2-C4 for freq.....
-int freqs[] = {261,277,293,311,329,349,369,391,415,440,466,493,523,
-                554,587,622,659,698,739,783,830,880,932,987,1046};
- 
-//defining your midi notes as points in the array.....
-void show_message(MIDIMessage msg) {
-    if (msg.type() == MIDIMessage::NoteOnType) {
-        if (msg.velocity()!=0) {
-            freq = freqs[(msg.key()-48)];
-        }
-        else {
-            freq = 0;
-        }
+
+DigitalIn nota1(D8);
+DigitalIn nota2(D9);
+DigitalIn nota3(D10);
+DigitalIn nota4(D11);
+DigitalIn nota5(D12);
+DigitalIn nota6(PTA17);
+AnalogIn escala(A0);
+DigitalOut myled(LED1);
+
+int fact=0;
+
+void key1(){
+        midi.write(MIDIMessage::NoteOn(12+fact));
+        myled=1;
+        wait_ms(175);
+        midi.write(MIDIMessage::NoteOff(12+fact));
+        myled=0;
+        wait_ms(175);
+}
+
+void key2(){
+        midi.write(MIDIMessage::NoteOn(13+fact));
+        myled=1;
+        wait_ms(175);
+        midi.write(MIDIMessage::NoteOff(13+fact));
+        myled=0;
+        wait_ms(175);
+}
+
+void key3(){
+        midi.write(MIDIMessage::NoteOn(14+fact));
+        myled=1;
+        wait_ms(175);
+        midi.write(MIDIMessage::NoteOff(14+fact));
+        myled=0;
+        wait_ms(175);
+}
+
+void key4(){
+        midi.write(MIDIMessage::NoteOn(15+fact));
+        myled=1;
+        wait_ms(175);
+        midi.write(MIDIMessage::NoteOff(15+fact));
+        myled=0;
+        wait_ms(175);
+}
+
+void key5(){
+        midi.write(MIDIMessage::NoteOn(16+fact));
+        myled=1;
+        wait_ms(175);
+        midi.write(MIDIMessage::NoteOff(16+fact));
+        myled=0;
+        wait_ms(175);
+}
+
+void key6(){
+        midi.write(MIDIMessage::NoteOn(17+fact));
+        myled=1;
+        wait_ms(175);
+        midi.write(MIDIMessage::NoteOff(17+fact));
+        myled=0;
+        wait_ms(175);
+}
+
+void Botones(){
+    if(nota1==1){
+        key1();
+    }else if (nota2==1) {
+        key2();
+    }else if (nota3==1) {
+        key3();
+    }else if (nota4==1) {
+        key4();
+    }else if (nota5==1) {
+        key5();
+    }else if (nota6==1) {
+        key6();
+    }else{
+        midi.write(MIDIMessage::NoteOff(57));
     }
 }
- 
-int main() {
-    
-    // call back for midi messages received
-    midi.attach(show_message);
-    
-    while (1) {
-    
-    float time = (pot/2); // assign the pot to the time...
-    if (freq > 0){
-        buzzer.beep(freq,0.1); // between this beep...
-        wait (time);
-        buzzer2.beep((freq*2),0.1); // and this beep (1 octave up).
-        wait (time);
-        }
-    
-    wait (0.0001); // wait a millesecond and carry on...
+
+
+
+int main(){
+
+    while (1){
+        fact=escala.read()*64;
+        Botones();    
     }
 }
\ No newline at end of file