ADC

Hi!
I am trying to make a code where I can set the period of time where each ADC converter will work. For instance, I would read the temperature each 2 seconds, 10 minutes, etc. without stopping the rest of the program...
Does exist any library which I can use with the proper functions?
Regards,
A.

You don't begin to need a library to do that.

How to do multiple things at once ... like cook bacon and eggs

And some time read up on state machines which you'd need a library to do either.

Take a look at the Blink without Delay example.

The demo I wrote in the first post in this Thread is a more extensive example of the BWOD example showing how to manage several different things.

...R

Personally I take samples of the ADC at several moments within a second.
I use an interrupted based timer (16 Hz), use timeticks within that second (cnt1, 1..16).
Maybe you can use parts of my code and change timersettings.

//Initialize Timer1, preset to 16 Hz timetick
	cli();                    // disable global interrupts
	TCCR1A = 0;               // set entire TCCR1A register to 0
	TCCR1B = 0;               // same for TCCR1B
	OCR1A = 15624;            // set compare match register to desired timer count:
	TCCR1B |= (1 << WGM12);   // turn on CTC mode:
	TCCR1B |= (1 << CS10);    // Set CS10 and CS11 bits for 64 prescaler:
	TCCR1B |= (1 << CS11);
	TIMSK1 |= (1 << OCIE1A);  // enable timer compare interrupt:
//Interrupt every 62.5 ms (16 Hz) with timer 1, written on 20130617
ISR(TIMER1_COMPA_vect)
{
	cnt1++;                     //Increment 62.5 ms counter
	if (cnt1 == 17) {cnt1=1;}
}
void Mastercontroller() {
	if ((cnt1+1) % 3==0) { Read_analogValues(); }          //Take every 2,5,8,11,14 sixteenth of a second samples of analog inputs
        if (cnt1==7) {Serial.println("Action..."); }  //End if
        }  //End of function

@ArduinoStarter1, that seems a very complicated way to do something simple. Perhaps you are not really a starter?

...R

Why is my code a complicated solution Robin2?

If you need a time critical sampling of measuring values, you can use a timer interrupt to take samples.
In a datalogger system you require to sample at fixed moments and to add a time stamp.

The OP does not sound very time critical.

If you need a time critical sampling of measuring values, you can use a timer interrupt to take samples

...but you'd eliminate any latency by initiating the analogue read in the timer ISR, and not risk jitter caused by activity in non-interrupt context delaying your reading.

Do the analogRead in the IRQ?

Or "initiate/start" it there and catch the result later?
Can that be done? I thought we want other activities quiet during ADC. I must have this wrong.

Blink Without Delay every time.

Simple. Obvious. Adequate.

...R

GoForSmoke:
Do the analogRead in the IRQ?

No, I didn't write that. If you can fiddle with timer interrupts, you can pick apart analogRead, and use that knowledge to set the input mux and start the conversion.

Or "initiate/start" it there and catch the result later?
Can that be done? I thought we want other activities quiet during ADC. I must have this wrong.

Quiet? As in the busy wait inside analogRead, with the processor running full pelt? What's the difference?

The ATMEL docs mention things turned off during ADC but I can no longer cut&paste from the PDF.....

hang on, sorry, that's a special mode of operation to say the least.