Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
5 years, 9 months ago.
Calling The Wait() Function and Running Another Function Concurrently C++
I have a class where I have 2 functions as below;
float read_samples(float *data, int num_samples) { float sum = 0.0; float average = 0.0;
for(int i = 1; i < 12; i++ ) { data[i] = temperature.read(); sum = sum + data[i]; wait(1); } average = sum / (num_samples - 1); return average; }
int main() { int num_samples = P / S; float data[num_samples]; while(1) { wait(12); float result = read_samples(data, num_samples); lcd.printf("%.2f\n", result); data[0]; } }
When I call the "wait(12);" method in Main, I want the code to at the same time to go execute the "read_samples" method while waiting, and when the "read_samples" is done return the value computed, then go ahead to display the computed result in the Main function. The computation should be during the 12 seconds while waiting.
However, what is happening right now is that during the 12 seconds while waiting, no execution is carried out, so after waiting for 12 seconds it goes to call the "read_samples" method and carries out execution for another 11 seconds. So the total time spent to display the next result is (12 + 11) 23 seconds. Please how do I write my code to execute the "read_samples" method during the 12 seconds wait.
1 Answer
5 years, 9 months ago.
Simple solution: Remove the wait(12).
When you call a function the program won't move on to the next statement until that function is complete.
The only way that main wouldn't wait is if you were using a timer interrupt to time the readings or if the ADC readings were being done by a different thread.
Also in the future please use
<<code>> your code <</code>>
so that things get formatted correctly.
float read_samples(float *data, int num_samples) { float sum = 0.0; float average = 0.0; for(int i = 1; i < 12; i++ ) { data[i] = temperature.read(); sum = sum + data[i]; wait(1); } average = sum / (num_samples - 1); return average; } int main() { int num_samples = P / S; float data[num_samples]; while(1) { //wait(12); // don't wait here. float result = read_samples(data, num_samples); lcd.printf("%.2f\n", result); data[0]; // this line does nothing. } }
Thank you for the suggestion Andy, could you please explain with written code how to implement the timer interrupt as with my code.
posted by 14 Feb 2019To read the ADC on an interrupt you'd do something like this.
Now the main loop does have to wait but rather than using a fixed time instead loop waiting for the samples to complete.
Or the software could go off and do something else useful during this time. The whole point is that the ADC reads will happen in the background and stop when the correct number are done no matter what the main code is doing.
All the main code has to worry about is not reading the results until after the correct number of reads have been done, something it can easily check without having to resort to timers or waits.
Ticker readTick; volatile int readCount; volatile float readTotal; int num_samples; void onReadTick() { readTotal += temperature.read(); readCount++; if (readCount == num_samples) readTick.attach(null,1); // automatically stop once we have the correct number } int main() { num_samples = P / S; while(1) { readCount = 0; readTotal = 0; readTick.attach(&onReadTick,1.0); // onReadTick will get called every second until canceled. while (readCount < num_samples) { // do nothing, wait until all the samples are in. } float result = readTotal/num_samples; lcd.printf("%.2f\n", result); } }
This may help:
https://os.mbed.com/questions/80284/Whats-difference-Threadwait-and-wait-fun/
posted by Paul Staron 14 Feb 2019