fast "analogRead" for multiple flex-sensors. I'm Lost!

i have been searching all over the internet to find a simple and understandable example of an analogRead in the faster AVR type of approach, by adressing the ADC directly instead of through the arduino firmware.

i need to read 5 flex sensors as fast as possible. they are connected to the analog pins 0-3 and 6.analog pin 7 is used too so practically i need to read every analogpin except 4 and 5 because they are used for i2C
What i don't understand is all that mux and adscr setup stuff.

Any help is welcome!!!

Most of any speed gained (unless you reduce resolution) is going to be in elimination of the busy-wait in analogRead.
What are you trying to achieve?

you might find this thread of some interest - Experiment - split up analog read in 3 steps - Libraries - Arduino Forum -

Your best bet is to use the ADC conversion complete interrupt to change the multiplexer, read the ADC value, save the result in a variable, and start another conversion. The default ADC clock is 125kHz, resulting in a conversion time of about 110us. You can double the ADC clock to 250kHz with almost no loss of precision. Beyond that, you can double it again to 500kHz and get ~9 bits precision, or quadruple it again to 1MHz and get ~8 bits precision.

As an example of what you can do, I'm currently clocking the ADC at 1MHz in an induction balance metal detector project, in order to sample a 7.8kHz signal 8 times per cycle to do phase sensitive detection.

dc42:
Your best bet is to use the ADC conversion complete interrupt to change the multiplexer, read the ADC value, save the result in a variable, and start another conversion. The default ADC clock is 125kHz, resulting in a conversion time of about 110us. You can double the ADC clock to 250kHz with almost no loss of precision. Beyond that, you can double it again to 500kHz and get ~9 bits precision, or quadruple it again to 1MHz and get ~8 bits precision.

As an example of what you can do, I'm currently clocking the ADC at 1MHz in an induction balance metal detector project, in order to sample a 7.8kHz signal 8 times per cycle to do phase sensitive detection.

That is really cool! you could do obstacle avoidance like that!

though... i have troubles finding a clear example. do you have one or can post one?

i need an example that contains everything to read one analog pin and instructions on what to change to select/read another pin.

iSpider:
i need an example that contains everything to read one analog pin and instructions on what to change to select/read another pin.

I could post the code for my metal detector, but it would need a lot of adaptation to meet your needs. Why not start by looking at file wiring_analog.c in your Arduino installation, and relating it to the datasheet for the microcontroller?

i have taken a look at the wiring_analog.c and it hasn't gotten any more clear to me.

That asside, i found something interesting in this file. According to this, i should be able to switch the analog pins from 0-7 to 8-15.

So can i connect analog sensors to both sets of pins and the switch between them by setting the MUX high or low?

#elif defined(ADCSRB) && defined(MUX5)
        // the MUX5 bit of ADCSRB selects whether we're reading from channels
        // 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
        ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
#endif

So can i connect analog sensors to both sets of pins and the switch between them by setting the MUX high or low?

MUX5 is used on the ATmega2560 to specify if A0-A7 or A8-A15 is used. Do you have an Arduino Mega2560?

I do...

not using it for my project though, but you can never have enough analog pins...