Doing other stuff while wait()

27 Oct 2011

Is there anyway to allow the mbed to continue sending readings to the PC and have it hold a part of the code in stasis? Below is a little snippet of my code.

while (1) {
  CurrentV = 3.3 * Current.read();
  printf("\r\n%o.4f",CurrentV);
  if(CurrentV < 2.180) {
     phase_A = 0.8;
     wait(10);
  } else {
     phase_A = 0.635;
     wait(5);
  }
}
26 Oct 2011

Paul,

From your snippet above I would have thought the mbed Ticker\attach would be a good candidate, ie using interrupts.

Dave.

27 Oct 2011

I have also thought about using Ticker\attach but I am unsure on how to implement them.

Explanation on my snippet: phase_A is a PWM output from mbed LPC1768. I would like to let the PWM to run at the specific duty cycle before checking CurrentV (Voltage analog input from a current transducer) for the value to trigger the change. While this is going on, I would like to continuously send the value of CurrentV to the PC through the USB.

27 Oct 2011

Paul Try something like this.

#include "mbed.h"

Ticker readCurrentTicker;
 float CurrentV;
 float phase_A;
 
void readCurrent()
{
  CurrentV = 3.3 * Current.read();
  printf("\r\n%.4f",CurrentV);
}


int main(){

Serial pc(USBTX, USBRX);
readCurrentTicker.attach(&readCurrent, 0.010);

while (1) {
  
  if(CurrentV < 2.180) {
     phase_A = 0.8;
    
     wait(10);
  } else {
     phase_A = 0.635;
     
     wait(5);
  }
}
}

Dave.

28 Oct 2011

Paul,

Perhaps Timeout could give something close to what you want. I haven't even compiled the following code, but try something like this -

#include "mbed.h"

// put other stuff here, as needed

// Timeout stuff -
Timeout Whenever;

static volatile int /* Bool */ Loitering = 0 /* NO */; // semaphore

static void fini(void) { // called at end of Timeout
  Whenever.detach(void);
  Loitering = 0 /* NO */;
}

static void Loiter(float duration) {
  Loitering = 1 /* YES */;
  Whenever.attach(&fini, duration);
}

// read and print stuff -
static float CurrentV;

static void readCurrent(void) {
  CurrentV = 3.3 * Current.read();
  printf("\r\n%.4f",CurrentV);
}

// main program w/ infinite loop -
void main(void) {

  // other set-ups go here

  while (1) { // forever loop
    readCurrent(void);
    if (CurrentV < 2.180)
      { phase_A =   0.8; Loiter(10.0); }
    else
      { phase_A = 0.635; Loiter( 5.0); }
    while (Loitering) readCurrent(void); // pace set by printf()?
  } // end while(1)

  // return; // this should never be reached!
} // end main()

Fred