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;
}
}
}
@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