Advertisement
diegosiena

ArduinoDrum_UNO

Mar 5th, 2014
6,584
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. // *****************************************************************************************************************
  2. // * *
  3. // * arduinodrum.wordpress.com *
  4. // * *
  5. // * Bateria Eletrônica com Arduino *
  6. // * Diego Siena - 2014 *
  7. // * *
  8. // *****************************************************************************************************************
  9.  
  10. //*******************************************************************************************************************
  11. // Variáveis de Definição do Usuário.
  12. //*******************************************************************************************************************
  13.  
  14. unsigned char PadNote[6] = {52,16,66,63,40,65}; // Notas MIDI - 0 a 127
  15.  
  16. int PadCutOff[6] = {600,600,600,600,600,600}; // Valor Mínimo do Sensor para causar o som
  17.  
  18. int MaxPlayTime[6] = {90,90,90,90,90,90}; // Ciclos a passar antes da Segunda Batida ser acionada.
  19.  
  20. #define midichannel 0; // Canal Midi
  21.  
  22. boolean VelocityFlag = true; // Se o som será de acordo com a intensidade da Batida.
  23.  
  24.  
  25.  
  26.  
  27.  
  28. //*******************************************************************************************************************
  29. // Variáveis de uso Interno
  30. //*******************************************************************************************************************
  31.  
  32. boolean activePad[6] = {0,0,0,0,0,0}; // Salva se os pads estao ativos ou nao.
  33. int PinPlayTime[6] = {0,0,0,0,0,0}; // Contador dos ciclos desde que o pad foi acionado.
  34.  
  35. unsigned char status;
  36.  
  37. int pin = 0;
  38. int hitavg = 0;
  39.  
  40. //*******************************************************************************************************************
  41. // Setup
  42. //*******************************************************************************************************************
  43.  
  44. void setup()
  45. {
  46. Serial.begin(57600);
  47. }
  48.  
  49. //*******************************************************************************************************************
  50. // Main Program
  51. //*******************************************************************************************************************
  52.  
  53. void loop()
  54. {
  55. for(int pin=0; pin < 6; pin++) // Percorre os Pinos Analógicos
  56. {
  57. hitavg = analogRead(pin); // Lê o Valor do Sensor
  58.  
  59. if((hitavg > PadCutOff[pin])) // Verifica se o valor pego pelo sensor é maior que o Valor minimo para causar o Som
  60. {
  61. if((activePad[pin] == false)) // Verifica se o Pad já está sendo executado.
  62. {
  63. if(VelocityFlag == true) // Verifica se o som será de acordo com a Intensidade da Batida, para gerar o Sinal Midi.
  64. {
  65. // hitavg = 127 / ((1023 - PadCutOff[pin]) / (hitavg - PadCutOff[pin])); // With full range (Too sensitive ?)
  66. hitavg = (hitavg / 8) -1 ; // Upper range
  67. }
  68. else
  69. {
  70. hitavg = 127;
  71. }
  72.  
  73. MIDI_TX(144,PadNote[pin],hitavg); // Joga o SInal MIDI
  74. PinPlayTime[pin] = 0; //Seta o Ciclo para '0'
  75. activePad[pin] = true; // Altera o Pad para Ativo.
  76. }
  77. else
  78. {
  79. PinPlayTime[pin] = PinPlayTime[pin] + 1; // Caso o Pad ja esteja ativo, incrementa 1 Ciclo.
  80. }
  81. }
  82. else if((activePad[pin] == true)) // ESTA SEGUNDA PARTE É RESPONSÁVEL APENAS POR INCREMENTAR OS CICLOS E ATIVAR/DESATIVAR OS PADS.
  83. {
  84. PinPlayTime[pin] = PinPlayTime[pin] + 1;
  85.  
  86. if(PinPlayTime[pin] > MaxPlayTime[pin])
  87. {
  88. activePad[pin] = false;
  89. MIDI_TX(128,PadNote[pin],127);
  90. }
  91. }
  92. }
  93. }
  94.  
  95.  
  96. //*******************************************************************************************************************
  97. // Função que transmite o MIDI
  98. //*******************************************************************************************************************
  99. void MIDI_TX(unsigned char MESSAGE, unsigned char PITCH, unsigned char VELOCITY)
  100. {
  101. status = MESSAGE + midichannel;
  102. Serial.write(status);
  103. Serial.write(PITCH);
  104. Serial.write(VELOCITY);
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement