to left align the ADC value, so I can read highest 8 bits from the ADCH register. But this is for use with only one analog-input pin. Instead, I need to read all 8 analog pins – so I’m still using analogRead().
The above line of code seems to have no effect on analogRead(), where I’m still getting only the two most significant bits with highByte(), separated from the rest of the bits in lowByte().
So what’s the best way to read the 8 most siginifcant bits in one byte, from all eight analog pins?
If you look at wiring_analog.c at the source for analogRead you see this:
// set the analog reference (high two bits of ADMUX) and select the
// channel (low 4 bits). this also sets ADLAR (left-adjust result)
// to 0 (the default).
#if defined(ADMUX)
ADMUX = (analog_reference << 6) | (pin & 0x07);
#endif
So it is undoing your change and setting ADLAR back to 0.
To do what you want you may have to roll your own analogRead function.
Having read it, I’ve decided a simple divide-by-4 is probably the best way. I tried it and it works, returning a number between 0 and 255 for the byte receiving it.
Here’s a copy of the complete sketch. It’s in an Arduino Pro Mini, radioing back data on heart rate, skin conductance, temperature, etc.
The “max(1,” on each line is to prevent each from ever sending a “0” byte, because I’m using “0” to mean:
“Set complete. Begin a new line.”
Well thank you both for the additional code and ideas.
Although an interesting concept, the first idea probably isn't what I want, because it adds several more lines of code for the AVR to execute (including the call of two more functions, one being the original "analogRead()").
But I'm now using the second idea of replacing the line:
bb[0] = max(1,analogRead(pin)/4);
with the line:
bb[0] = max(1,analogRead(pin) >> 2);
because this change has the single effect of making my sketch run faster.