AaaaaaaaaanalogIn

06 Dec 2012

I'm no doubt making a simple mistake, but I've been trying myriad variations to try and get an analog in value on p20 to print in teraterm... For HOURS. Can anyone tell me where I'm going wrong? The following is supposed to give a flowrate and a voltage on a single refreshing teraterm line. But all I get is a static value for the voltage, I've had seemingly random figures and the present version is showing 0.00000 Help Robin

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

Serial pc(USBTX, USBRX); counter SF800 (p29); Timer t_a, t_b; AnalogIn ain(p20);

int main() {

t_a.start(); t_b.start(); int a_init_pulses = 0; int b_init_pulses = 0; float flowrate = 0, ain; 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.1);

if (a_b_flip_flop) { flowrate = (float)(SF800.getPulses() - a_init_pulses)/(2500 * t_a.read_ms() / 60000); } else { flowrate = (float)(SF800.getPulses() - b_init_pulses)/(2500 * 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(); }

pc.printf("\rflowrate = %f l/min", flowrate); pc.printf(" Inclination = %f", ain); } }

06 Dec 2012

Hi, Looking at your code you have an AnalogIn object called ain and then declare a float also called ain. This will probably be set to 0 when run and might be the root of the problem.

include "counter.h"
include "mbed.h"
Serial pc(USBTX, USBRX);
counter SF800 (p29);
Timer t_a, t_b;
AnalogIn ain(p20);

int main()
{

    t_a.start();
    t_b.start();
    int a_init_pulses = 0;
    int b_init_pulses = 0;
    float flowrate = 0, ain;
    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.1);

        if (a_b_flip_flop) {
            flowrate = (float)(SF800.getPulses() - a_init_pulses)/(2500 * t_a.read_ms() / 60000);
        } else {
            flowrate = (float)(SF800.getPulses() - b_init_pulses)/(2500 * 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();
        }

        pc.printf("\rflowrate = %f l/min", flowrate);
        pc.printf(" Inclination = %f", ain);
    }
}
06 Dec 2012

Hi Martin That sounds good, I will have a look. I thought I had to say "ain" was an analog input and that it was a floating number. I didn't realise they were different things! Thanks Robin

06 Dec 2012

Hi Robin, No you don't need to declare ain as a float. When you read ain using the the read() method you can assign it to a float. For example

AnalogIn ain(p20);
float val;

val = ain.read();

or maybe in your case...

AnalogIn ain(p20);

pc.printf(" Reading = %f", ain.read());

Check out AnalogIn for more info

06 Dec 2012

Pursuing this I am at least back to a number, but it is 27740! and it doesn't change still. The input is getting around 1.5 volts, and varies when I move it (an analogue accelerometer), so I expected some where in the region of 0.5 to be printed out. Any thoughts? Best Robin

06 Dec 2012

Sorry, your reply hadn't popped up on my machine when I posted, That's brilliant! It's working! Thank you very much. Now I can get on and convert it into Gees Best Robin