Advertisement
Guest User

Pawntatonic

a guest
Apr 8th, 2015
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.87 KB | None | 0 0
  1. #include <MIDI.h>
  2. #include <midi_Defs.h>
  3. #include <midi_Message.h>
  4. #include <midi_Namespace.h>
  5. #include <midi_Settings.h>
  6. #include <Oscil.h> // oscillator template
  7. #include <tables/sin2048_int8.h> // sine table for oscillator
  8.  
  9. #include <LiquidCrystal.h>
  10. #include "pitches.h"
  11.  
  12.  
  13. // 2-dimensional array of row pin numbers:
  14. const int row[8] = {53,51,47,45,41,37,33,29};
  15.  
  16. // 2-dimensional array of column pin numbers:
  17. const int col[8] ={ 22,26,30,34,38,42,46,50 };
  18.    
  19. // 2-dimensional array of pixels:
  20. //int pixels[8][8];      
  21.  
  22. int currentStep = 0;
  23.  
  24. int poti = A1;
  25. //set beats per minute
  26. int bpm = 200;
  27.  
  28. //set scale
  29. int changeScale = A2;
  30. int isPent = 0;
  31.  
  32. void setup() {
  33.  
  34.   Serial.begin(31250);
  35.  
  36.   // initialize the I/O pins as outputs
  37.   // iterate over the pins:  
  38.   for (int thisPin = 0; thisPin < 8; thisPin++) {
  39.  
  40.     // initialize output and input pins:
  41.     pinMode(col[thisPin], OUTPUT);
  42.     pinMode(row[thisPin], INPUT);  
  43.    
  44.     //switch for scales
  45.     pinMode(changeScale, INPUT);
  46.  
  47.     //Fields off:
  48.     digitalWrite(col[thisPin], LOW);  
  49.   }
  50. }
  51.  
  52. void loop() {
  53.  
  54.   currentStep = (currentStep + 1) % 8;
  55.  
  56.   // Aktuelle Spalte unter Strom setzen
  57.   for (int c = 0; c < 8; c++) {
  58.     if (c == currentStep)
  59.       digitalWrite(col[c], HIGH);
  60.     else
  61.       digitalWrite(col[c], LOW);
  62.   }
  63.  
  64.   // delay von 1-5
  65.   delay(5);
  66.  
  67.   //Serial.print(currentStep);
  68.   //Serial.print(":\t");
  69.  
  70.   for (int r = 0; r < 8; r++) {
  71.     int v = digitalRead(row[r]);
  72.     if (v == HIGH) {
  73.       //Serial.print("X");
  74.       playTone(r);
  75.      }
  76.     else {
  77.       //Serial.print(".");
  78.     }
  79.    
  80.     //pixels[x][y] = v;
  81.   }
  82.   //Serial.println();
  83.   delay(readPoti());
  84.  
  85.   //audioHook();
  86. }
  87.  
  88. void playTone(int height){
  89.    //play tone
  90.    //1 = highest
  91.    //8 = lowest
  92.    int mytone;
  93.    int miditone;
  94.    
  95.    isPent = (digitalRead(changeScale));
  96.    
  97.    //Serial.println(switchState);
  98.    
  99.    if(isPent == 1){
  100.     //use pentatonic scale
  101.      switch(height){
  102.        case 0:
  103.          mytone = NOTE_C4;
  104.          miditone = 72;
  105.          break;
  106.         case 1:
  107.          mytone = NOTE_D4;
  108.          miditone = 74;
  109.          break;
  110.          case 2:
  111.          mytone = NOTE_E4;
  112.          miditone = 76;
  113.          break;
  114.         case 3:
  115.          mytone = NOTE_G4;
  116.          miditone = 79;
  117.          break;
  118.         case 4:
  119.          mytone = NOTE_A4;
  120.          miditone = 81;
  121.          break;
  122.         case 5:
  123.          mytone = NOTE_C5;
  124.          miditone = 84;
  125.          break;
  126.            case 6:
  127.          mytone = NOTE_D5;
  128.          miditone = 86;
  129.          break;
  130.         case 7:
  131.          mytone = NOTE_E5;
  132.          miditone = 88;
  133.          break;  
  134.      }
  135.    }
  136.    else {
  137.     //use major scale
  138.      switch(height){
  139.        case 0:
  140.          mytone = NOTE_C4;
  141.          miditone = 72;
  142.          break;
  143.         case 1:
  144.          mytone = NOTE_D4;
  145.          miditone = 74;
  146.          break;
  147.            case 2:
  148.          mytone = NOTE_E4;
  149.          miditone = 76;
  150.          break;
  151.         case 3:
  152.          mytone = NOTE_F4;
  153.          miditone = 77;
  154.          break;
  155.         case 4:
  156.          mytone = NOTE_G4;
  157.          miditone = 79;
  158.          break;
  159.         case 5:
  160.          mytone = NOTE_A4;
  161.          miditone = 81;
  162.          break;
  163.            case 6:
  164.          mytone = NOTE_B4;
  165.          miditone = 83;
  166.          break;
  167.         case 7:
  168.          mytone = NOTE_C5;
  169.          miditone = 84;
  170.          break;  
  171.      }
  172.    }
  173.  
  174.    tone(52, mytone);
  175.    noteOn(0x90,miditone,0x7f);
  176.    
  177.    delay(50);
  178.    
  179.    noTone(52);
  180.  
  181. }
  182.  
  183. //send midi to output
  184. void noteOn(int cmd, int pitch, int velocity) {
  185.   Serial.write(cmd);
  186.   Serial.write(pitch);
  187.   Serial.write(velocity);
  188. }
  189.  
  190. //read from poti to set speed
  191. int readPoti() {
  192.   // set bpm
  193.   int src = analogRead(poti);
  194.  
  195.   bpm = src;
  196.   //Serial.print(bpm);
  197.  
  198.   //optional: transform to bmp
  199.  
  200.   return bpm;
  201. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement