Go Down

Topic: How to execute a function for "t" time without delay + using external interrupt (Read 944 times) previous topic - next topic

8bit_Biker

Hi everybody,
After some research inside this forum and google, I've a lot of doubt about Time management with Arduino, so I've decide to ask you some help. The goal is to create a switch case loop inside "void loop()" that execute a certain subroutine for a certain time (for example 3 minutes). The pseudocode I've thought is:

Code: [Select]
void setup() {
  Serial.begin(9600);
  Serial.println("Waiting for command");
}

void loop() {
       
  // this is where the "polling" occurs
  if(Serial.available()){
    char ch=Serial.read();
    switch(ch)
    {
      case '1':
      // do something
      break;
     
      case '2':
      //do something
      break;
     
      case '3':
      void Tempfunct();
      break;
     
      default:
      Serial.print(ch);
      Serial.println(" : unknown command!");
    }   
}
}
 

// function to be called
void Tempfunct() {
//do something for  3*60*60*1000 s and return to switch case selection
}


I consider this solution a starting point, because I think a solution involving interrupts would be a better solution. Specifically I expect something like this pseudocode:


Code: [Select]
void setup() {
  Serial.begin(9600);
  Serial.println("Waiting for command");
}

void loop() {
       
  // this is where the "polling" occurs
  if(Serial.available()){
    char ch=Serial.read();
    switch(ch)
    {
      case '1':
      // do something
      break;
     
      case '2':
      //do something
      break;
     
      case '3':
      void Tempfunct();
      break;
     
      default:
      Serial.print(ch);
      Serial.println(" : unknown command!");
    }   
}
}
 

// function to be called
void Tempfunct() {
//do something until an interrupt condition is received from Serial (such a key pressed by user) and then return to switch case selection
}


Can someone help me?
Thank you all!

Chagrin

Look in the example for the "TimeAlarms" addition in the [http://www.arduino.cc/playground/uploads/Code/Time.zip]Time[/url] library.

You topic subject mentions "+ using external interrupt"; I assume you meant for the timing only or is there something else you were trying to accomplish?

8bit_Biker

Hi, thank for you soon reply!
I've got the library yet, but it's a little bit to understand to me how to use it to time a function. I'm trying to keep hard an eye on to understand it.
The interrupt I would try to set is only for the timed function. I.e.: pushing a key on the keyboard cause the start of a measurement (for a certain and yet set sensor); pushing another button cause the stop of the measurement. During the function I would like Arduino will get values from the sensor and send value to the serial port (simply using Serial.print(....)) using a set time period (for example one serial printing every two second). After every printing Arduino should search the serial port for the "stop key", used as interrupt. I would like to use the sensor data to plot a graph on Processing. Thank you very much!

dhenry

Easy:

Code: [Select]

void run_me(void) {
  static unsigned char execution_count=0; //execution counter

  if (++execution_counter > MAX_RUN) return; //if total number of runs exceed, do nothing

  //put your code here
}


All you need to do is to specify MAX_RUN.

8bit_Biker

@dhenry:
uhm... Can you explain your code???
Thank you very much! 

Go Up