Pages: [1]   Go Down
Author Topic: Euclidean Rhythm programming  (Read 1275 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!  I just got an Arduino uno for Father's day.  I have been playing with some of the LED samples and others and am trying to get familiar with everything.  Pretty fun so far.

Independently I have been reading about the Euclidean Algorithm and specifically thinking about this implementation... http://www.hisschemoller.com/2011/euclidean-midi-patterns/

I'd like to initially, very basically, use this Euclidean Rhythm idea just to light a single LED and modify it's blinking with a couple of potentiometers.  One controlling the total number of "steps" the other controlling the "notes per steps".  "steps" and "Fills" from the link above.  The big problem here is I am more of a hardware guy than software so I have some hurdles initially to overcome while I learn the c programming language. Can someone help me out with this basic one LED example? I'm thinking if I can see how the code would be for a simple  LED I can take that that and scale it up and add complexity.

Any advice at this point will help.  I'm just not sure where to start.
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm looking into this as it sounds like something interesting and is rather fun to play with smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, I'm trying.  Here's a very very rough basic stab at it.  I am having difficulty reporting the quotient and remainder from the division together.  Those values will then define my pattern.  Because it's not complete I have been testing the code in a compiler to make sure the Euclid function works.  Which it appears to.

Code:
// These variables will change depending on input values by user
int steps; //  Defines # of steps in sequence
int hits; // k Defines how many hits(notes) per squence
long intervalYellow; // defined by steps
long intervalGreen;  //  defined by hits

// These variables will not change
int pin5 = 5; // yellow LED, blinks to # of steps in sequence
int pin7 = 7; // green LED, blinks to hits in sequence


void setup() {               
  // initialize the digital pin as an output.
  pinMode(pin5, OUTPUT);
  pinMode(pin7, OUTPUT); 
}

void loop() {
  int steps = 8;
  int hits = 3;
  int euclid(int steps, int hits)
  {
    if (hits == 0)
      return steps;
    else
      return euclid(hits,steps % hits);
  }

// Trying to figure out how to return (hits,steps) values to then drop in and define [X..X..X.] in this (3,8) example. Would this be an array?

}
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well instead of returning them - save them to variables and check the variables afterwards.

You probably do want to then save it into an array - I need to have a bit more of a read into it.

You could define the array with the number of steps so the length of the array would be easy enough, then assign the steps to the different parts of the array.

Then recall the array in a loop flashing the LED when array[n] = 1 or something.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 505
Posts: 31337
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Trying to figure out how to return (hits,steps) values
You can't you can only return one value from a function.
You could pass the function a pointer to an array and update the array in the function but then you return nothing.

Or you could use global variables. ............. I will leave now before the C++ police shoot me.
Logged

North Yorkshire, UK
Offline Offline
Faraday Member
**
Karma: 104
Posts: 5531
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well it aint as if global variables wouldn't work - it's by far the easiest way smiley-razz
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 505
Posts: 31337
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes I know but some people are so against them.
By the way what has Euclid got to do with the rhythm, sounds a bit pretentious.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 7
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It's based on the Euclidean algorithm.

http://cgm.cs.mcgill.ca/~godfried/publications/banff.pdf
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 4
Love Open Source
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I`m working in same direction with arduino. And I have euclide routine done in software. My problem is to add midi and buttons and rotary encoder with it in one thing for quick twiking a pattern. And of couse I want my box to follow midi clock.
Maybe someone want join to project? and help me with this?

this code calculates and store pattern in memory, and show it on LCD

Quote
// include the library code:
#include <LiquidCrystal.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int remainder[16];
int count[16];
int level;
int steps;
int pulses;
int pauses;
int patit=0;
char pattern[16];

void setup() {
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  Serial.begin(9600);
}

void loop() {
  // set the cursor to column 0, line 1
  // (note: line 1 is the second row, since counting begins with 0):
  lcd.setCursor(0, 1);
  
  compute_bitmap(7,3); // Here we add how many Steps/Pulses we need
  
  lcd.setCursor(0, 0);
  lcd.print(steps);lcd.print("/");lcd.print(pulses);
  pauses = steps - pulses;
      Serial.println();              //debug
//if (pauses > pulses) {
//  for (int l = steps; l >= 0; l--) {  //for pauses > pulses
//  lcd.setCursor(l, 0); 
//  lcd.print(pattern[l]);
//  }
//} else {
  for (int l = 0; l < steps; l++) {  //for pauses < pulses
//  lcd.setCursor(l, 0); 
//  lcd.print(pattern[l]);
      Serial.print(pattern[l]);
}
//}
      delay (1000);
      
}

void compute_bitmap (int num_slots,int num_pulses)  {
    if (num_pulses > num_slots) {num_pulses = num_slots;}             // Sane input
  int divisor = num_slots - num_pulses;
  steps = num_slots; pulses = num_pulses;
  remainder[0] = num_pulses;
  level = 0;
  do {
    count[level] = divisor / remainder[level];
    remainder[level+1] = divisor % remainder[level];
    divisor = remainder[level];
    level = level +1; }
    while (remainder[level] > 1);

    count[level] = divisor; 
    build_string (level);
 
}

void build_string (int level)  {
  if (level == -1) {

  lcd.print('0'); 
    pattern[patit]='0'; //0 into array pattern
    patit=patit+1;      //plus 1
  }
  else if (level == -2)  {   

  lcd.print('1'); 
    pattern[patit]='1'; //1 into array pattern
    patit=patit+1;      //plus 1
  }
  else {
    for (int i = 0; i < count[level]; i++)
    build_string(level-1);
    if (remainder[level] !=0)
    build_string(level-2);
  }//end else

}//end build_string
  
    

Logged

Pages: [1]   Go Up
Jump to: