11 years, 3 months ago.

Non blocking getc (apparently...)

Hi I've put together a program that delivers live flowrate and graviational angle (from a pulsing flowmeter and a triple axis analog accelerometer) to terminal. However I now need to adjust the offsets so as to calibrate this display, from a keyboard. I copied the pc.getc from the servo control example, but this only gives an output when a key is pressed, rather than a live display I can then adjust with the right keys. Any idea how I can do what I need? Best Robin

  1. include "counter.h"
  2. include "mbed.h"

Serial pc(USBTX, USBRX); counter SF800 (p29); Timer t_a, t_b; AnalogIn z(p20); AnalogIn y(p19); AnalogIn x(p18);

int main() {

t_a.start(); t_b.start(); int a_init_pulses = 0; int b_init_pulses = 0; float flowconvert = 2500; float flowrate = 0; float inclin = 0; float inc_offset = 0; int time_window = 1000;

bool a_b_flip_flop = true; if true use window a

pc.printf("\n\r***\n\rMBED restarted\n\r***\n");

while (1) { wait(0.25);

pc.printf("Before switch\n");

switch(pc.getc()) { case '7': flowconvert += 1; break; case '1': flowconvert -= 1; break; case '9': inc_offset += 1; break; case '3': inc_offset -= 1; break; }

pc.printf("After switch\n");

if (a_b_flip_flop) { flowrate = (float)(SF800.getPulses() - a_init_pulses)/(flowconvert * t_a.read_ms() / 60000); } else { flowrate = (float)(SF800.getPulses() - b_init_pulses)/(flowconvert * t_b.read_ms() / 60000); }

if (t_a.read_ms() > time_window) { switch to window B

pc.printf("\rswitching to window B = %i pulses detected in window A\n", SF800.getPulses() - a_init_pulses);

t_a.reset(); a_b_flip_flop = false; a_init_pulses = SF800.getPulses(); }

if (t_a.read_ms() > time_window/2 && !a_b_flip_flop) { switch to window A

pc.printf("\rswitching to window A = %i pulses detected in window B\n", SF800.getPulses() - b_init_pulses);

t_b.reset(); a_b_flip_flop = true; b_init_pulses = SF800.getPulses(); }

inclin = (atan2(y.read(), z.read()) * 180.0 / 3.141592654) + inc_offset;

pc.printf("\rflowrate = %.3f ml/min", flowrate); pc.printf(" Inclination G %.1f degrees", inclin); } }

If you surround code blocks with <<code>> and <</code> it is much more readable.

posted by Stephen Paulger 11 Dec 2012

1 Answer

11 years, 3 months ago.

First what Stephen said, please use code tags to make it readable. Talking about readable, thats probably also what you want. Check pc.readable(), and only when it is true call getc. Or for example use an RX interrupt.

Accepted Answer

Sorry! Will remember next time. So am I right in thinking I could put the getc inside an IF governed by pc.readable? Such that the ELSE option is just to print the values live? Best Robin

posted by Robin Hague 11 Dec 2012

That should work yes, if it is readable read it, and do something about it, if not, just skip that step.

posted by Erik - 11 Dec 2012

Thanks. I was blocked on this for last few hours. This really helped.

posted by Jatin Sharma 16 Nov 2015