Go Down

Topic: timed interrupt - stop - serial.print? (Read 532 times) previous topic - next topic

memotick

I am trying to use a timed interrupt to fill an array with ADC values ~400us apart (as an example).  I then want to stop the timed interrupts and do a serial print of this array.  Any tips on how to do this?  I was thinking of something like the following:



Code: [Select]
int TOP=50; // this sets OCR1A for the CTC counter
int buffSize=60;
// buff containing the ADC Samps
float* buff;

// Every Arduino program must have a setup section
void setup() {

  Serial.begin(57600); 
  // set compare match register to desired timer count:
  // also known as the TOP
  OCR1A = TOP;
  buff = (float *) malloc(sizeof(float)* buffSize);
  sei();//enable interrupts
}

ISR(TIMER1_COMPA_vect) {
  for (int i= 0 ; i < buffSize - 1 ; i++){
    buff[i] = buff[i+1];
  }
  buff[buffSize - 1] = analogRead(0);
}

void loop() {
  delay(1000);
  cli();
  // Somehow I have to enable the serial print but disable
  // the timed interrupts
  for (int i= 0 ; i < buffSize - 1 ; i++){
    Serial.println(buff[i]);
  }
}


Thanks for the help!

PaulS

If you are going to create a dynamic array with a fixed size, why won't a static array work?

Code: [Select]
ISR(TIMER1_COMPA_vect) {
  for (int i= 0 ; i < buffSize - 1 ; i++){
    buff[i] = buff[i+1];
  }
  buff[buffSize - 1] = analogRead(0);
}

A circular array would stop all that shuffling.

memotick


If you are going to create a dynamic array with a fixed size, why won't a static array work?

Code: [Select]
ISR(TIMER1_COMPA_vect) {
  for (int i= 0 ; i < buffSize - 1 ; i++){
    buff[i] = buff[i+1];
  }
  buff[buffSize - 1] = analogRead(0);
}

A circular array would stop all that shuffling.


I'm not crazy about the shuffling either, but could you give me an example of a circular array?  Is it a matter of modifying the index "i" only?

Thanks

PeterH

I only provide help via the forum - please do not contact me for private consultancy.

memotick


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy