Pages: [1]   Go Down
Author Topic: How to change modulation at real time in an arpeggiator?  (Read 1260 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here is mi code (only with one chord, the others are technically the same)

#include <ardumidi.h>

//Tempo
int dur = 100;
int wait = 20;

// Octavas
float octmode=0;
float oct=0;

// Velocidad
float vel=97;


void setup()
{
  Serial.begin(115200); //Hairless Midi-serial Baud rate
//Acordes
  pinMode(23, INPUT); //Do Mayor
  pinMode(25, INPUT); //Fa Mayor
  pinMode(27, INPUT); //Sol Mayor
  pinMode(29, INPUT); //La menor
  pinMode(31, INPUT); //Re menor
  pinMode(33, INPUT); //Mi Mayor
 
//Modo
  pinMode(35, INPUT); //Modo -> LOW=1 -> HIGH=2
 
//Octava + o -
  pinMode(37, INPUT);
  pinMode(38, INPUT);
 
//Velocidad + o -
  pinMode(39, INPUT);
  pinMode(40, INPUT); 
}


void loop() {
 
//////////////////////////////DO MAYOR/////////////////////////////////////////////
 if (digitalRead(23)==HIGH)
  {
    while ((digitalRead(25)==LOW) && (digitalRead(27)==LOW) && (digitalRead(29)==LOW) && (digitalRead(31)==LOW) && (digitalRead(33)==LOW) && (digitalRead(35)==LOW))
    {
       
    midi_note_on(0, 72+oct, vel);
    delay(dur);                                                 //1-DO
    midi_note_off(0, 72+oct, vel);
    delay(wait);
   
    midi_note_on(0, 76+oct, vel);
    delay(dur);                                                 //2-MI
    midi_note_off(0, 76+oct, vel);
    delay(wait);
   
    midi_note_on(0, 79+oct, vel);
    delay(dur);                                                 //3-SOL
    midi_note_off(0, 79+oct, vel);
    delay(wait);
   
    midi_note_on(0, 72+oct, vel);
    delay(dur);                                                 //4-DO
    midi_note_off(0, 72+oct, vel);
    delay(wait);
   
    midi_note_on(0, 84+oct, vel);
    delay(dur);                                                 //5-DO^
    midi_note_off(0, 84+oct, vel);
    delay(wait);
   
    midi_note_on(0, 79+oct, vel);
    delay(dur);                                                 //6-SOL
    midi_note_off(0, 79+oct, vel);
    delay(wait);
   
    midi_note_on(0, 76+oct, vel);
    delay(dur);                                                 //7-MI
    midi_note_off(0, 76+oct, vel);
    delay(wait);
   
    midi_note_on(0, 79+oct, vel);
    delay(dur);                                                 //8-SOL
    midi_note_off(0, 79+oct, vel);
    delay(wait);
    }
   
if ((digitalRead(37)==HIGH) && (72+oct-12 > 0)) {oct=oct-12;}
if ((digitalRead(38)==HIGH) &&(91+oct+12 <=127)) {oct=oct+12;}
if ((digitalRead(39)==HIGH) && (vel+15 <= 127)) {vel=vel+15;}
if((digitalRead(40)==HIGH) && (vel-15 > 0)) {vel=vel-15;}   
 
   
   
    while ((digitalRead(25)==LOW) && (digitalRead(27)==LOW) && (digitalRead(29)==LOW) && (digitalRead(31)==LOW) && (digitalRead(33)==LOW) && (digitalRead(35)==HIGH))
    {
   
    midi_note_on(0, 91+oct, vel);
    delay(dur);                                                 //1-SOL^
    midi_note_off(0, 91+oct, vel);
    delay(wait);
   
    midi_note_on(0, 88+oct, vel);
    delay(dur);                                                 //2-MI^
    midi_note_off(0, 88+oct, vel);
    delay(wait);
   
    midi_note_on(0, 84+oct, vel);
    delay(dur);                                                 //3-DO^
    midi_note_off(0, 84+oct, vel);
    delay(wait);
   
    midi_note_on(0, 79+oct, vel);
    delay(dur);                                                 //4-SOL
    midi_note_off(0, 79+oct, vel);
    delay(wait);
   
    midi_note_on(0, 88+oct, vel);
    delay(dur);                                                 //5-MI^
    midi_note_off(0, 88+oct, vel);
    delay(wait);
   
    midi_note_on(0, 84+oct, vel);
    delay(dur);                                                 //6-DO^
    midi_note_off(0, 84+oct, vel);
    delay(wait);
   
    midi_note_on(0, 79+oct, vel);
    delay(dur);                                                 //7-SOL
    midi_note_off(0, 79+oct, vel);
    delay(wait);
   
    midi_note_on(0, 76+oct, vel);
    delay(dur);                                                 //8-MI
    midi_note_off(0, 76+oct, vel);
    delay(wait);
     
      }

if ((digitalRead(37)==HIGH) && (72+oct-12 > 0)) {oct=oct-12;}
if ((digitalRead(38)==HIGH) &&(91+oct+12 <=127)) {oct=oct+12;}
if ((digitalRead(39)==HIGH) && (vel+15 <= 127)) {vel=vel+15;}
if((digitalRead(40)==HIGH) && (vel-15 > 0)) {vel=vel-15;}

    }
 
 




//Sorry for the spanish!



So i want change the modulation with one of these (http://www.davidmiguel.com/arduino/wp-content/uploads/2013/03/sensor-ultrasonico-hc-sr04-para-arduino-pic-robotica_MLA-F-2928480380_072012.jpg) plugged it in an analog pin. The idea is that i may change the modulation ->during<- every note, moving my hand through the sensor.


I have to say that i'm a little bit noob, this is a final project for the high school and i've never worked in arduino before. If you have more ideas to optimize the code... Say it!

//sorry for my low budget english  smiley-roll-blue
Logged

France
Offline Offline
Newbie
*
Karma: 0
Posts: 46
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

To optimize the code, what I would do is to create a table where you would load your notes :

Code:
uint16_t arpeg_notes[8];
uint16_t arpeg_notes_start[8];

You can initialize it in your setup :

Code:
arpeg_notes_start[0] = 72
arpeg_notes_start[1] = 76
...

Code:
arpeg_notes[0] = 72
arpeg_notes[1] = 76
...


Then in your loop :

Code:
loop()
{
  if(step>7) step=0;
  else step ++;

  midi_note_on(0, arpeg_notes[step], vel);
  delay(dur);                                         
  midi_note_off(0, arpeg_notes[step], vel);
  delay(wait);

  read_analog();
}

Then you make a read_analog function where you read your IR sensor on analog input 0, for exemple.
And you want to change the note of all your table, and add the value of IR (transform the 10 bits analog value to 6 bits)

Code:
read_analog()
{
  change_note(analogRead(0)>>4);
}

change_note(uint16_t cn)
{
  for(int i = 0; i<8; i++)
  {
    arpeg_notes[i] = arpeg_notes_start[i] + cn;
  }
}


That's just an exemple, but in your change_note() function you can do what you want.
Of course, doing that in your main loop() section is not good, because the execution time is depending on the computations made in the cycle, and your bpm will be varying a little.
You should use arduino timers, but it's another story.

And it's better if your code is tagged, like I do.
« Last Edit: May 13, 2013, 03:32:42 am by gaith » Logged

Build a groove box with Arduino Due :
http://groovuino.blogspot.com/

Pages: [1]   Go Up
Jump to: