Go Down

Topic: Yet Another Multitasking Question (Read 918 times) previous topic - next topic

sphCow

Hello Arduino Forum, though I've used PIC uC for a year, but I'm very new to Arduino, and in fact this is my first forum post.
My project with Arduino UNO is an PC oscilloscope, the serial communication handling, plotting results and other data analysis is performed by Wolfram Mathematica. I'm almost done but I've a few questions:

I've tried to maximize the sampling speed by setting prescaler to 16 and storing 300 samples and their acquisition time in two integer arrays. And then Arduino sends the stored the data via serial com which takes about 300ms. While the data being sent via serial, no ADC operation is performed. Is there any way to multitask these two without loosing the sampling rate?

Code: [Select]


int inByte=0;
unsigned int time[300];
unsigned int sample[300];

unsigned long timeStart;

void setup()
{
  // running with high speed clock (set prescale to 16)
  bitClear(ADCSRA,ADPS0) ;
  bitClear(ADCSRA,ADPS1) ;
  bitSet(ADCSRA,ADPS2) ;

  Serial.begin(115200);

}

void loop()
{

  if(Serial.available()>0)
  {
    inByte = Serial.read()-'0';  //convert to char data type

    if (inByte>=0 && inByte<=9)
    {
      timeStart = micros();
      for(int j=0; j<300; j++)
      {
        delayMicroseconds(100);
        time[j] = (int)(micros()-timeStart) ;
        sample[j] = analogRead(A0);
      }

      timeStart = micros();
      for(int k=0; k<300; k++)
      {
        //Serial.print(k);
        if (k==0)
        {
        Serial.print("{"); 
        }
        Serial.print("{");
        Serial.print(time[k]);
        Serial.print(",");
        Serial.print(sample[k]);
        Serial.print("}");
        //Serial.print("\n");
        if (k==299)
        {
        Serial.print("}");
        }
        else
        {
         Serial.print(",");
        }
      }
      Serial.print(micros()-timeStart);
      Serial.print("E");
      Serial.print("\n");
    }

  }

}

Graynomad

#1
Apr 06, 2012, 12:58 pm Last Edit: Apr 06, 2012, 01:54 pm by Graynomad Reason: 1
One problem you have is that you send 8+ bytes for every sample. Is it necessary to format the data like that?

Apart from that you should be able to do the two by interleaving the sampling with the serial output by using a circular buffer or double buffering.

_____
Rob
Rob Gray aka the GRAYnomad www.robgray.com

sphCow


One problem you have is that you send 8+ bytes for every sample. Is it necessary to format the data like that?

Apart from that you should be able to do the two by interleaving the sampling with the serial output by using a circular buffer or double buffering.

_____
Rob


Well, I might as well use byte data types instead of ints. Actually I thought about it but abandoned soon. Ok, I'll give it a try. I never heard of double or circular buffering. I'll study a bit on it. Meanwhile, can you show me some examples of the same?

Constantin

As of IDE 1.0, the serial buffer should be non-blocking. However, the amount of data you can buffer is likely limited. My guess is that the ADC is waiting for the Serial transmission to finish.

The only true "multitasking" that the Arduino is capable of is running the ADC independently from the main CPU (at least as far as I'm aware of). Everything else interrupts the flow of the CPU as it diverts it's attention to an ISR, serial comm that is longer than 8 bytes, or other distraction.

As others have suggested, try minimizing the impact of the serial operations by reducing the amount of data you need to send. For example, in my power logger, I would sample two channels and while the output from channel A was being processed, I would be sampling Channel B, and vice-versa. That minimized the time required later to do final data processing and each data transmission consisted of maybe 20-odd bytes. FWIW, I used no pre-scaler adjustement to maintain the quasi-10bit resolution and still got several thousand samples per channel every second.

So I too would suggest sending small squirts of data but I would also try sending them at higher speeds. I successfully interfaced two Arduinos at 1Mbit/s per the Atmel dataspec. You can't use those speeds in the IDE, however (for, IMO, bad legacy reasons) so use Putty or any other terminal instead.

Go Up