uint16_t sArray1[16];
uint16_t sArray2[16];
int sLoopCounter;
void setup() {
Serial.begin(115200);
// Serial.begin(115200); // BUG! The 2. call disables output in Wokwi
}
void loop() {
Serial.print(F("sLoopCounter="));
Serial.println(sLoopCounter++);
// MVCE for https://github.com/wokwi/avr8js/issues/136
ADMUX = 0xC0; // select A0
ADCSRA = ((1 << ADEN) | (1 << ADSC) | (1 << ADATE) | (1 << ADIF) | 5);
for (unsigned int i = 0; i < 16; i++) {
loop_until_bit_is_set(ADCSRA, ADIF);
ADCSRA |= _BV(ADIF); // clear bit to enable recognizing next conversion has finished with "loop_until_bit_is_set(ADCSRA, ADIF)".
uint16_t tValue = ADCL | (ADCH << 8);
sArray1[i] = tValue; // store value at current counter index
}
ADCSRA = ((1 << ADEN) | (1 << ADIF) | 5); // Disable auto-triggering (free running mode), but the last conversion is still running
delay(1);
ADMUX = 0xC1;// select A1
ADCSRA = ((1 << ADEN) | (1 << ADSC) | (1 << ADATE) | (1 << ADIF) | 5);
for (unsigned int i = 0; i < 16; i++) {
loop_until_bit_is_set(ADCSRA, ADIF);
ADCSRA |= _BV(ADIF); // clear bit to enable recognizing next conversion has finished with "loop_until_bit_is_set(ADCSRA, ADIF)".
uint16_t tValue = ADCL | (ADCH << 8);
sArray2[i] = tValue; // store value at current counter index
}
ADCSRA = ((1 << ADEN) | (1 << ADIF) | 5); // Disable auto-triggering (free running mode), but the last conversion is still running
Serial.println(F("Print i=<ValueA0>|<ValueA1>"));
/*
We expect i=<ValueA0>|<ValueA1>, but this is only true the first time after boot,
Next loops we see i=<ValueA1>|<ValueA0>
and after 12 to 13 samples i=<ValueA0>|<ValueA0> or i=<ValueA1>|<ValueA1>
*/
for (unsigned int i = 0; i < 16; i++) {
Serial.print(i);
Serial.print('=');
Serial.print(sArray1[i]);
Serial.print('|');
Serial.print(sArray2[i]);
Serial.print(F(", "));
}
Serial.println();
delay(2000);
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
pot1:VCC
pot1:SIG
pot1:GND
pot2:VCC
pot2:SIG
pot2:GND
A0
A1