Skip to content

Instantly share code, notes, and snippets.

@Bradshaw
Created September 18, 2018 10:55
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Bradshaw/a64a7a8b454b92b5d443d066eff50295 to your computer and use it in GitHub Desktop.
Save Bradshaw/a64a7a8b454b92b5d443d066eff50295 to your computer and use it in GitHub Desktop.
int d = 10;
int led = 13;
int t = 0;
unsigned long lastMicros;
unsigned long deltaMicros;
double timeStep;
double timeUntil;
// the setup routine runs once when you press reset:
void setup() {
timeUntil = timeStep;
timeStep = (double)1/(double)8;
lastMicros = micros();
for (int i = 0; i<8; i++){
pinMode(i+2, OUTPUT);
}
pinMode(A6, INPUT);
pinMode(A5, INPUT);
pinMode(11, OUTPUT);
}
int reado = -1;
int sel = 0;
int par = 0;
int beat(int t){
if (t%256==0){
sel = map(
analogRead(A6),
0, 1023,
0, 6
);
par = analogRead(A5);
}
int p;
switch (sel) {
case 0:
p = map(par, 0, 1023, 0, 31);
return ((t<<1)^((t<<1)+(t>>7)&t>>12))|t>>(4-(1^7&(t>>p)))|t>>7;
case 1:
p = map(par, 0, 1023, 0, 31);
return t*(t>>((t>>11)&p))*(t>>9&1)<<2;
case 2:
p = map(par, 0, 1023, 0, 31);
return t>>4+!(-t>>13&7)+2*!(t>>17)|t*t*(t>>(t>>12^t>>11)%3+10)/(7+(t>>10&t>>14&3))*!(t&512)<<3+(t>>p&1);
case 3:
p = map(par, 0, 1023, 0, 31);
return t>>4|t*t*(t>>6&8^8)*(t>>11^t/3>>12)/(7+(t>>10&t>>p&3));
case 4:
p = map(par, 0, 1023, 0, 127);
return t*t/(t>>12&t>>8&p)<<7;
case 5:
p = map(par, 0, 1023, 0, 127);
return t*(((t>>12)|(t>>8))&(p&(t>>4)));
default:
p = map(par, 0, 1023, 0, 127);
return ((t >> 10) & p) * t;
}
}
// the loop routine runs over and over again forever:
void loop() {
deltaMicros = micros()-lastMicros;
lastMicros = micros();
timeUntil-=((double)deltaMicros / (double)1000);
if (timeUntil<=0){
timeUntil+=timeStep;
t++;
digitalWrite(11, (t%1024!=0 ? LOW : HIGH));
int n = beat(t);
for (int i = 0; i<8; i++){
int one = i+2;
digitalWrite(one, (n%2==0 ? LOW : HIGH));
n/=2;
}
}
}
@noahms456
Copy link

I think I'm going to give this a try and put it in a eurorack module... thanks for the code. How many cases do you think could be fit in the code section?

@Bradshaw
Copy link
Author

Woop, sorry, didn't see your comment
I'm just getting into eurorack myself, ordered my first case and a few modules that should arrive shortly!

Your limitation there will be memory, I think. I don't think adding more cases will require more cycles to calculate, so the performance ought to stay the same

If you're interested in this kind of thing, maybe check out a track I wrote in C using bytebeat techniques a while ago: https://gist.github.com/Bradshaw/3fb5b13938a47c547b39
You can also listen to a rendering of the track here: https://soundcloud.com/freelancer-epic/blastwave-hq-sox-oggenc-encode

Feel free to ping me if you get good results on your eurorack module!

@noahms456
Copy link

I don't think I have the skills, yet, to implement the R2R DAC aspect of it! although having 8 bits/jacks of crunchy noisy pulses out would be terrific. I've been tinkering with it off and on and probably shouldn't float the grounds on the pots I have, since they don't seem too responsive the way I have them set up, now. It's sort of fun listening to the individual pins' signals out, though. And if you just leave a strand of ungrounded wire floating out of the analog in pins, you get a really interesting theremin thing happening - the Arduino code docs even mentions this side-effect of my sloppy electronics skills. Pretty fun, though! I may add an input pot to adjust the velocity - thanks again

@Bradshaw
Copy link
Author

The R2R DAC isn't too complicated, it just uses a lot of resistors. There's a good schematic of the module that inspired me here: https://www.olegtron.com/olegtron-r2r-1

The exact values of the resistors are kind of irrelevant, it's mostly only important that the legs have double the resistance of the spine. And if you only have one kind of resistor, you can put two in series to get double the resistance too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment