/*
Original undocumented code waas written by DarwinWasWrong
(https://wokwi.com/projects/405174243721967617)
Comments added by trulyBent, in order to figure out what he was doing
For an explanation as to why the ADMUX bit 5 (ADLAR) is set (= left justified)
see this website: https://www.robotplatform.com/knowledge/ADC/adc_tutorial_3.html
*/
int Channel0 ;
uint8_t _pin_select[8] = {128, 192, 224, 240, 248, 252, 254, 255};
/*
bit patterns:
128 = 1000 0000
192 = 1100 0000
224 = 1110 0000
240 = 1111 0000
248 = 1111 1000
252 = 1111 1100
254 = 1111 1110
255 = 1111 1111
*/
// interrupt
ISR(ADC_vect) {
char ADCch = ADMUX & B00000111; // channel is zero
if (ADCch == 0) {
Channel0 = ADCH; // grab value of high byte (= 0 to 255)
ADCSRA |= B11000000; // retrigger conversion
}
}
void setup() {
Serial.begin(115200);
DDRD = DDRD | B11111100; // digital pins 0 to 7 directional (2 to 7 as outputs)
DDRB = DDRB | B00000011; // digital pins 8 to 13 (set 8 and 9 as outputs)
ADMUX = B01100000; // reference is VCC, left adjust result, select A0 as input (0000)
ADCSRB = B00000000; // free running mode
ADCSRA = B11001111; // 11000000 = start conversion
// ADCSRA Bit 5 – ADATE: ADC Auto Trigger Enable
// ADCSRA Bit 4 – ADIF: ADC Interrupt Flag
// ADCSRA Bit 3 – ADIE: ADC Interrupt Enable
// ADCSRA Bits 2:0 – ADPS[2:0]: ADC Prescaler select bits (sysclock/128)
}
void loop() {
int timed_reading = (Channel0 >> 5); // get top 3 bits of byte = 0 to 7
PORTD = _pin_select[timed_reading] << 2; // get pattern from array, shift into leds 2 to 7
PORTB = _pin_select[timed_reading] >> 6; // get pattern from array, shift to leds 8 and 9
Serial.print("A0:"); // cheat :)
Serial.print(Channel0);
Serial.print(" Reading:");
Serial.println(timed_reading);
delay(50);
}