Go Down

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

8bit_Biker


That sounds reasonable. I don't know what buffering the Processing interface applies when writing to the serial port - you would need to find out whether it is able to write single character commands as you are assuming; quite likely it is.

Nothing you're describing on the Arduino side requires your sketch to use interrupts - all the behaviour you're describing can be achieved by straight forward coding within loop().


Yes, Processing can write single character to serial port: this is what i do with Processing.
For me, this situation is like having a sort of "measurement loop" inside one of the "case". When Arduino receive a preset "start-key" (start character), it starts measuring. When the user press another button in the Processing interface, it send preset "stop-key" (stop character) to Arduino: Arduino feels like to have an external interrupt, stops the measurement and return to void loop(){...} cycle.

PeterH


For me, this situation is like having a sort of "measurement loop" inside one of the "case". When Arduino receive a preset "start-key" (start character), it starts measuring. When the user press another button in the Processing interface, it send preset "stop-key" (stop character) to Arduino: Arduino feels like to have an external interrupt, stops the measurement and return to void loop(){...} cycle.


No, that's NOT how I'd do it.

You want to avoid designing your code so that it stops and waits for something to happen (the command to end monitoring); that's a blocking design which would need you to replicate logic all over the place. The benefit of following the non-blocking approach demonstrated in the 'blink without delay' example is that your sketch can handle multiple actions and events in a consistent way without all your separate bits of code needing to explicitly cooperate or depend on each other; it gives you a nice simple architecture that scales very well.

In loop() add your code to receive commands from the serial port and set a 'monitoring enabled' flag to indicate whether the regular monitoring should be happening based on the commands received. Also in loop() test whether the 'monitoring enabled' flag is set and then whether it's time to collect a sample, and if it is then you collect and transmit the sample. Other pieces of code in loop() could deal with other things you might need to do concurrently such as blinking LEDs or whatever.
I only provide help via the forum - please do not contact me for private consultancy.

8bit_Biker



No, that's NOT how I'd do it.

You want to avoid designing your code so that it stops and waits for something to happen (the command to end monitoring); that's a blocking design which would need you to replicate logic all over the place. The benefit of following the non-blocking approach demonstrated in the 'blink without delay' example is that your sketch can handle multiple actions and events in a consistent way without all your separate bits of code needing to explicitly cooperate or depend on each other; it gives you a nice simple architecture that scales very well.

In loop() add your code to receive commands from the serial port and set a 'monitoring enabled' flag to indicate whether the regular monitoring should be happening based on the commands received. Also in loop() test whether the 'monitoring enabled' flag is set and then whether it's time to collect a sample, and if it is then you collect and transmit the sample. Other pieces of code in loop() could deal with other things you might need to do concurrently such as blinking LEDs or whatever.



Thank you for your reply!This solution seems good, but I haven't understand it very well. Can you explain it to me with an example?

8bit_Biker


Just FYI:

Code: [Select]
#include <Time.h>
#include <TimeAlarms.h>

void setup() {
  Serial.begin(9600);
  Serial.println("Waiting for command");
}

AlarmID_t temptimer;
 
void loop() {
       
  // this is where the "polling" occurs
  if(Serial.available()){
    char ch=Serial.read();
    switch(ch)
    {
      case '1':
      // start recording?
      temptimer = Alarm.timerRepeat(1, Tempfunct);
      break;
     
      case '2':
      // stop recording?
      Alarm.disable(temptimer);
      break;
           
      default:
      Serial.print(ch);
      Serial.println(" : unknown command!");
    }   
  }
}

// function to be called
void Tempfunct() {
   // do whatever reading
   Serial.println(analogRead(A0));
}


This is perhaps kinda heavy for what you appear to be doing (should use the "blink without delay" method), but just wanted to followup.

Also, TimeAlarms.cpp needs to be updated for Arduino 1.0.

Replace:
Code: [Select]
#include <WProgram.h>

With:
Code: [Select]

#if ARDUINO >= 100
#include <Arduino.h>
#else
#include <WProgram.h>
#endif




Hi, I've tried to use your method, but it seems it doesn't work for me! after the Tempfunct has been called, nothing happens! Why??

Go Up