Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. //mux variables
  2. #include <SPI.h>
  3. #include <Wire.h>
  4. #include <MIDI.h>
  5. #define MPU 0x68  // I2C address of the MPU-6050
  6.  
  7. const byte input = 11;   // digital input to arduino from mux
  8. const byte c=10;
  9. const byte b=9;
  10. const byte a=8;
  11.  
  12. const int button1Pin = 4;
  13. const int button2Pin = 5;
  14. const int button3Pin = 2;
  15. const int button4Pin = 7;
  16.  
  17. const int DEFAULT_MIDI_CHANNEL = 1;
  18. MIDI_CREATE_DEFAULT_INSTANCE();
  19.  
  20.  
  21. int button1State = 0;
  22. int button2State = 0;
  23. int button3State = 0;
  24. int button4State = 0;
  25.  
  26.  
  27. int note1 = 0;
  28. int note2 = 0;
  29. int note3 = 0;
  30. int note4 = 0;
  31. int note5 = 0;
  32. int note6 = 0;
  33. int note7 = 0;
  34. int note8 = 0;
  35. int note9 = 0;
  36. int note10 = 0;
  37. int note11 = 0;
  38. int note12 = 0;
  39.  
  40. int led1 = 3;
  41. int led2 = 6;
  42. int led3 = A1; // bush button LED
  43. int led4 = A2; // on off LED
  44.  
  45.  
  46.  
  47. int brightness1 = 0;
  48. int brightness2 = 0;
  49.  
  50.  
  51.  
  52. // to store the values from mux
  53. // bool val[] = {0,0,0,0,0,0,0,0};  
  54. byte numero;
  55.  
  56. double AcX,AcY,AcZ;
  57. int Pitch, Roll;
  58. int Yf;
  59. int Yb;
  60. int Xf;
  61. int Xb;
  62.  
  63. void setup() {
  64.  
  65.   MIDI.begin(MIDI_CHANNEL_OMNI);
  66.    Serial.begin(31250);
  67.  
  68.   init_MPU(); // Inizializzazione MPU6050
  69.  
  70.    
  71.   pinMode(c, OUTPUT);
  72.   pinMode(b, OUTPUT);
  73.   pinMode(a, OUTPUT);
  74.  
  75.  
  76.   pinMode(led1, OUTPUT);
  77.   pinMode(led2, OUTPUT);
  78.   pinMode(led3, OUTPUT);
  79.  
  80.  
  81.  
  82.   pinMode(button1Pin, INPUT);
  83.   pinMode(button2Pin, INPUT);
  84.   pinMode(button3Pin, INPUT);
  85.   pinMode(button4Pin, INPUT);
  86.   pinMode(input, INPUT);    
  87.  
  88.  
  89. }
  90.  
  91. void loop() {
  92.  
  93. int mc1 =0;
  94. int mc2  =0;
  95. int mc3  =0;
  96. int mc4  =0;
  97.  
  98.   FunctionsMPU(); // Acquisisco assi AcX, AcY, AcZ.
  99.    
  100.   Roll = FunctionsPitchRoll(AcX, AcY, AcZ);   //Calcolo angolo Roll
  101.   Pitch = FunctionsPitchRoll(AcY, AcX, AcZ);  //Calcolo angolo Pitch
  102.  
  103.   int xposition = map(Roll, -90, 90, 0, 179);
  104.   int yposition = map(Pitch, -90, 90, 179, 0);
  105.  
  106.   int button1State = digitalRead(button1Pin);
  107.   int button2State = digitalRead(button2Pin);
  108.   int button3State = digitalRead(button3Pin);
  109.   int button4State = digitalRead(button4Pin);
  110.  
  111.   analogWrite(led1, brightness1);
  112.   analogWrite(led2, brightness2);
  113.  
  114.   digitalWrite(led3, LOW);
  115.   digitalWrite(led4, HIGH);
  116.  
  117.  
  118.  
  119.  
  120.   numero = 0;
  121.   for(byte i=0;i<8;i++) {
  122.  
  123.          numero = numero | ( digitalRead(input) << i) ;
  124.  
  125.     //select mux input
  126.     digitalWrite(a, bitRead(i, 0));
  127.     digitalWrite(b, bitRead(i, 1));
  128.     digitalWrite(c, bitRead(i, 2));
  129.  
  130.  
  131.  button1State = digitalRead(button1Pin);
  132.  button2State = digitalRead(button2Pin);
  133.  button3State = digitalRead(button3Pin);
  134.  button4State = digitalRead(button4Pin);
  135.  
  136.  
  137.  
  138.  
  139.    
  140.   }
  141.  
  142.  
  143.  if (button1State == HIGH) {
  144.     // if note not playing
  145.  
  146.    
  147.     if (note1 == 0) {
  148.       // play note (note number, velocity, channel)
  149.       // more info: http://arduinomidilib.sourceforge.net/a00001.html
  150.       // MIDI notes chart http://www.phys.unsw.edu.au/jw/notes.html
  151.       // 55 = G3, 127 = trigger note with max volume
  152.    
  153.    MIDI.sendNoteOn(42, 127, DEFAULT_MIDI_CHANNEL);
  154.       // note is playing
  155.       note1 = 1;
  156.     }
  157.   // when button released
  158.          digitalWrite(led3, HIGH);
  159.  } else {
  160.  
  161.  
  162.     // if note playing
  163.     if (note1 == 1) {
  164.       // if playing - stop
  165.      
  166.       MIDI.sendNoteOff(42, 0, DEFAULT_MIDI_CHANNEL);
  167.     }
  168.     // if button released note is off
  169.     note1 = 0;
  170.  
  171.   }
  172.  
  173.    
  174.      if (button2State == HIGH) {
  175.        
  176.  
  177.     // if note not playing
  178.     if (note2 == 0) {
  179.            
  180.  
  181.    MIDI.sendNoteOn(43, 127, DEFAULT_MIDI_CHANNEL);
  182.       // note is playing
  183.       note2 = 1;
  184.     }
  185.   // when button released
  186.           digitalWrite(led3, HIGH);
  187.  } else {
  188.     // if note playing
  189.     if (note2 == 1) {
  190.       // if playing - stop
  191.      
  192.       MIDI.sendNoteOff(43, 0, DEFAULT_MIDI_CHANNEL);
  193.     }
  194.     // if button released note is off
  195.     note2 = 0;
  196.  
  197.   }
  198.    
  199.      if (button3State == HIGH) {
  200.     // if note not playing
  201.     if (note3 == 0) {
  202.  
  203.    MIDI.sendNoteOn(44, 127, DEFAULT_MIDI_CHANNEL);
  204.       // note is playing
  205.       note3 = 1;
  206.     }
  207.   // when button released
  208.             digitalWrite(led3, HIGH);
  209. } else {
  210.     // if note playing
  211.     if (note3 == 1) {
  212.       // if playing - stop
  213.       MIDI.sendNoteOff(44, 0, DEFAULT_MIDI_CHANNEL);
  214.     }
  215.     // if button released note is off
  216.     note3 = 0;
  217.   }
  218.  
  219.      if (button4State == HIGH) {
  220.     // if note not playing
  221.     if (note4 == 0) {
  222.  
  223.    MIDI.sendNoteOn(45, 127, DEFAULT_MIDI_CHANNEL);
  224.       // note is playing
  225.       note4 = 1;
  226.     }
  227.   // when button released
  228.              digitalWrite(led3, HIGH);
  229.  } else {
  230.     // if note playing
  231.     if (note4 == 1) {
  232.       // if playing - stop
  233.       MIDI.sendNoteOff(45, 0, DEFAULT_MIDI_CHANNEL);
  234.     }
  235.     // if button released note is off
  236.     note4 = 0;
  237.   }
  238.  
  239.   if (numero == 1) {
  240.     // if note not playing
  241.     if (note5 == 0) {
  242.  
  243.    MIDI.sendNoteOn(46, 127, DEFAULT_MIDI_CHANNEL);
  244.       // note is playing
  245.       note5 = 1;
  246.     }
  247.   // when button released
  248.               digitalWrite(led3, HIGH);
  249.  } else {
  250.     // if note playing
  251.     if (note5 == 1) {
  252.       // if playing - stop
  253.       MIDI.sendNoteOff(46, 0, DEFAULT_MIDI_CHANNEL);
  254.     }
  255.     // if button released note is off
  256.     note5 = 0;
  257.   }
  258.  
  259.     if (numero == 2) {
  260.     // if note not playing
  261.     if (note6 == 0) {
  262.  
  263.    MIDI.sendNoteOn(47, 127, DEFAULT_MIDI_CHANNEL);
  264.       // note is playing
  265.       note6 = 1;
  266.     }
  267.   // when button released
  268.               digitalWrite(led3, HIGH);
  269.  } else {
  270.     // if note playing
  271.     if (note6 == 1) {
  272.       // if playing - stop
  273.       MIDI.sendNoteOff(47, 0, DEFAULT_MIDI_CHANNEL);
  274.     }
  275.     // if button released note is off
  276.     note6 = 0;
  277.   }
  278.  
  279.       if (numero == 4) {
  280.     // if note not playing
  281.     if (note7 == 0) {
  282.  
  283.    MIDI.sendNoteOn(48, 127, DEFAULT_MIDI_CHANNEL);
  284.       // note is playing
  285.       note7 = 1;
  286.     }
  287.   // when button released
  288.               digitalWrite(led3, HIGH);
  289.  } else {
  290.     // if note playing
  291.     if (note7 == 1) {
  292.       // if playing - stop
  293.       MIDI.sendNoteOff(48, 0, DEFAULT_MIDI_CHANNEL);
  294.     }
  295.     // if button released note is off
  296.     note7 = 0;
  297.   }
  298.  
  299.         if (numero == 8) {
  300.     // if note not playing
  301.     if (note8 == 0) {
  302.  
  303.    MIDI.sendNoteOn(49, 127, DEFAULT_MIDI_CHANNEL);
  304.       // note is playing
  305.       note8 = 1;
  306.     }
  307.   // when button released
  308.                digitalWrite(led3, HIGH);
  309.  } else {
  310.     // if note playing
  311.     if (note8 == 1) {
  312.       // if playing - stop
  313.       MIDI.sendNoteOff(49, 0, DEFAULT_MIDI_CHANNEL);
  314.     }
  315.     // if button released note is off
  316.     note8 = 0;
  317.   }
  318.  
  319.           if (numero == 16) {
  320.     // if note not playing
  321.     if (note9 == 0) {
  322.  
  323.    MIDI.sendNoteOn(50, 127, DEFAULT_MIDI_CHANNEL);
  324.       // note is playing
  325.       note9 = 1;
  326.     }
  327.   // when button released
  328.                digitalWrite(led3, HIGH);
  329.  } else {
  330.     // if note playing
  331.     if (note9 == 1) {
  332.       // if playing - stop
  333.       MIDI.sendNoteOff(50, 0, DEFAULT_MIDI_CHANNEL);
  334.     }
  335.     // if button released note is off
  336.     note9 = 0;
  337.   }
  338.  
  339.             if (numero == 32) {
  340.     // if note not playing
  341.     if (note10 == 0) {
  342.  
  343.    MIDI.sendNoteOn(51, 127, DEFAULT_MIDI_CHANNEL);
  344.       // note is playing
  345.       note10 = 1;
  346.     }
  347.   // when button released
  348.                 digitalWrite(led3, HIGH);
  349.  } else {
  350.     // if note playing
  351.     if (note10 == 1) {
  352.       // if playing - stop
  353.       MIDI.sendNoteOff(51, 0, DEFAULT_MIDI_CHANNEL);
  354.     }
  355.     // if button released note is off
  356.     note10 = 0;
  357.   }
  358.  
  359.               if (numero == 64) {
  360.     // if note not playing
  361.     if (note11 == 0) {
  362.  
  363.    MIDI.sendNoteOn(52, 127, DEFAULT_MIDI_CHANNEL);
  364.       // note is playing
  365.       note11 = 1;
  366.     }
  367.   // when button released
  368.                 digitalWrite(led3, HIGH);
  369.  } else {
  370.     // if note playing
  371.     if (note11 == 1) {
  372.       // if playing - stop
  373.       MIDI.sendNoteOff(52, 0, DEFAULT_MIDI_CHANNEL);
  374.     }
  375.     // if button released note is off
  376.     note11 = 0;
  377.   }
  378.  
  379.                 if (numero == 128) {
  380.     // if note not playing
  381.     if (note12 == 0) {
  382.  
  383.    MIDI.sendNoteOn(53, 127, DEFAULT_MIDI_CHANNEL);
  384.       // note is playing
  385.       note12 = 1;
  386.     }
  387.   // when button released
  388.                 digitalWrite(led3, HIGH);
  389.  } else {
  390.     // if note playing
  391.     if (note12 == 1) {
  392.       // if playing - stop
  393.       MIDI.sendNoteOff(53, 0, DEFAULT_MIDI_CHANNEL);
  394.     }
  395.     // if button released note is off
  396.     note12 = 0;
  397.   }
  398.  
  399.   //serial printing
  400.   //Serial.print("Numero: ");
  401.   //Serial.println(numero);
  402.  
  403. if (yposition < 80 && yposition > 10) {
  404.  
  405. int Yf = yposition;
  406.  brightness1 = map(Yf, 79, 11, 0, 255);
  407.  
  408.   mc1 = map(Yf, 79, 11, 0, 127);
  409.    MIDI.sendControlChange(1, mc1, DEFAULT_MIDI_CHANNEL);
  410.  
  411. }
  412.  
  413. if (yposition > 100 && yposition < 170) {
  414.  
  415. int Yb = yposition;
  416.  
  417.  brightness1 = map(Yb, 101, 169, 0, 255);
  418.  
  419.   mc2 = map(Yb, 101, 169, 0, 127);
  420.    MIDI.sendControlChange(2, mc2, DEFAULT_MIDI_CHANNEL);
  421.  
  422.  
  423. }
  424.  
  425. if (xposition < 80 && xposition > 10) {
  426.  
  427. int xf = xposition;
  428.  
  429.  brightness2 = map(xf, 79, 11, 0, 255);
  430.  
  431.  
  432.   mc3 = map(xf, 79, 11, 0, 127);
  433.   MIDI.sendControlChange(3, mc3, DEFAULT_MIDI_CHANNEL);
  434.  
  435.  
  436. }
  437.  
  438. if (xposition > 100 && xposition < 170) {
  439.  
  440. int xb = xposition;
  441.  
  442.  brightness2 = map(xb, 101, 169, 0, 255);
  443.  
  444.   mc4 = map(xb, 101, 169, 0, 127);
  445.    MIDI.sendControlChange(4, mc4, DEFAULT_MIDI_CHANNEL);
  446.  
  447.  
  448. }
  449.  
  450. }
  451.  
  452. void init_MPU(){
  453.   Wire.begin();
  454.   Wire.beginTransmission(MPU);
  455.   Wire.write(0x6B);  // PWR_MGMT_1 register
  456.   Wire.write(0);     // set to zero (wakes up the MPU-6050)
  457.   Wire.endTransmission(true);
  458.   delay(1000);
  459. }
  460.  
  461. //Function to calculate angles
  462. double FunctionsPitchRoll(double A, double B, double C){
  463.   double DatoA, DatoB, Value;
  464.   DatoA = A;
  465.   DatoB = (B*B) + (C*C);
  466.   DatoB = sqrt(DatoB);
  467.  
  468.   Value = atan2(DatoA, DatoB);
  469.   Value = Value * 180/3.14;
  470.  
  471.   return (int)Value;
  472. }
  473.  
  474. //Function to get  X,Y,Z from MPU6050
  475. void FunctionsMPU(){
  476.   Wire.beginTransmission(MPU);
  477.   Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  478.   Wire.endTransmission(false);
  479.   Wire.requestFrom(MPU,6,true);  // request a total of 14 registers
  480.   AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
  481.   AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  482.   AcZ=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  483. }