Skip to content

Instantly share code, notes, and snippets.

@jsleeio
Last active January 2, 2016 03:19
Show Gist options
  • Save jsleeio/8243555 to your computer and use it in GitHub Desktop.
Save jsleeio/8243555 to your computer and use it in GitHub Desktop.
Arduino - reading multiple pots asynchronously with a free-running ADC
#include "avr/interrupt.h"
uint8_t pots[4] = { 0, 0, 0, 0 };
uint16_t samples[4] = { 0, 0, 0, 0 };
const uint8_t nextpot[4] = { _BV(ADLAR) | 0b0001, _BV(ADLAR) | 0b0010, _BV(ADLAR) | 0b0011, _BV(ADLAR) | 0b0000 };
void setup() {
Serial.begin(9600);
ADMUX = _BV(ADLAR) | _BV(MUX1) | _BV(MUX0);
ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADATE) | _BV(ADIE) | _BV(ADPS0)| _BV(ADPS1) | _BV(ADPS2);
ADCSRB = 0; // free-running - all ADTS bits cleared
}
void loop() {
char buf[150];
snprintf(buf,149,"cutoff:%-3d/%-6d resonance:%-3d/%-6d envelope:%-3d/%-6d decay:%-3d/%-6d",
pots[0], samples[0], pots[1], samples[1], pots[2], samples[2], pots[3], samples[3]);
Serial.println(buf);
delay(1000);
}
ISR(ADC_vect) {
uint8_t reading = ADCH << 4;
static uint8_t first = 1;
if (first) {
first = 0;
return;
}
uint8_t mux;
mux = ADMUX & 0b00000111;
ADMUX = nextpot[mux];
pots[mux] = reading;
samples[mux]++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment