9 years, 5 months ago.

Only 1 of 2 FastAnalogIn working

Firstly, thanks for this library! I'm much closer to what I want to achieve now. But there's a Problem.

I want to log 2 Analog Inputs. And the logging has to be fast, since I am trying to plot a Soundwave, with 2 electret mics. Here's the code.

Logging 2 analog inputs (200 samples)

#include "mbed.h"
#include "FastAnalogIn.h"

DigitalOut myled1(LED1);
DigitalOut myled2(LED2);
DigitalOut finishedLed(LED4);

FastAnalogIn SoundSensor1(p15, 1);
FastAnalogIn SoundSensor2(p16, 1);

DigitalOut Notused_2(p19);
DigitalOut Notused_3(p20);
DigitalOut Notused_4(p17);
DigitalOut Notused_5(p18);

DigitalIn reset(p21);
DigitalIn set(p22);

LocalFileSystem local("local");

int main() {
    
    float data1[200] = {0};
    float data2[200] = {0};
    FILE *fp1;
    FILE *fp2;

    if ( NULL == (fp1 = fopen( "/local/Sound1.csv", "w" )) )
        error( "" );
    if ( NULL == (fp2 = fopen( "/local/Sound2.csv", "w" )) )
        error( "" );
    
    //The Trigger. The program will loop here until it recognizes a peek in one of the input signals
    while((SoundSensor1.read() < 0.60 && SoundSensor1.read() > 0.40) && (SoundSensor2.read() < 0.60 && SoundSensor2.read() > 0.40) && !set);
    
    //set the led to 1 while the samples are stored in arrays
    finishedLed = 1;
    
    for ( int i = 0; i < 200; i++ ) {
        data1[i] = SoundSensor1.read();
        data2[i] = SoundSensor2.read();
    }

    finishedLed   = 0;
    
    //write the samples in files    
    for ( int i = 0; i < 200; i++ ) {
        fprintf( fp1, "%f\n", data1[i] );
    }
    
    for ( int i = 0; i < 200; i++ ) {
        fprintf( fp2, "%f\n", data2[i] );
    }

    fclose( fp1 );
    fclose( fp2 );
    
}

The Problem is, that only the data of SoundSensor2 is getting logged correctly. The 200 values from SoundSensor1 don't change at all (they're all around 0.54955, changes a bit each try). What am I doing wrong? By the way, If I declare the FastAnalogIn like this (with a "0" as parameter)

FastAnalogIn declaration

FastAnalogIn SoundSensor1(p15, 0);
FastAnalogIn SoundSensor2(p16, 0);

it works for both channels. But they doesn't seem to be much faster then the normal AnalogIn (I didn't investigate too much, though).

Would be glad for every bit of help!

Question relating to:

Class similar to AnalogIn that uses burst mode to run continious background conversions so when the input is read, the last value can immediatly be returned. AnalogIn, burst, fast

hello if you read the info at right you will find out the problem, only 1 FastAnalogIn is working. so you would need an external fast switch for your signals and one digital out to select the channel. that is all.

posted by Helmut Deichmann 04 Nov 2014

IIRC on the LPC11u24 it should be possible to run multiple FastAnalogIns in parallel. I will have a look at it later on.

posted by Erik - 04 Nov 2014

I forgot to write that I'm using an LPC11u24. Sorry!

posted by Tobias Kuhn 04 Nov 2014

As a noobie sidequestion: What is the "FastAnalogIn_LPC11UXX.cpp" file good for? I included the "FastAnalogIn.h" in my main file, but I am not able to figure out the usage of "FastAnalogIn_LPC11UXX.cpp". BTW. I measured the sampling rate of the working FastAnalogIn. It was around 90k samples / sec. (Edit: I just measured the normal AnalogIn samplerate. It was around 29k samples / sec. FastAnalogIn is therefore 3 times faster)

posted by Tobias Kuhn 04 Nov 2014

That file contains the actual code for the LPC11u24. It is automatically connected to the .h file by the compiler, so you don't need to do anything with it.

And it is indeed quite a bit faster, but even faster should be doable. Also did you use read or read_u16?

posted by Erik - 04 Nov 2014

I used the read-function. Please allow me to ask you another noobie question: How did you get that file to be included to the .h file?

posted by Tobias Kuhn 04 Nov 2014

read_u16 will be faster if you need that.

And regarding that file, it automatically works that way. With the #include "FastAnalogIn.h" the cpp file has access to that header file, and the compiler automatically links everything.

posted by Erik - 04 Nov 2014

1 Answer

9 years, 5 months ago.

Bug fixed, thanks for reporting :)

Issue was a small one: In the constructor of the class the control register was written with the required settings, but this overwrote the active channel bits, resulting in the previously contructed one being disabled. Apparantly in testing I (at least think I added that one) didn't check constructing both as active. Most likely I did have one active, one inactive, and after that I tried different combinations with the enable function.

Anyway fixed now, if you update the lib (right mouse button, update) it should also work for you.

Accepted Answer

Hello Erik Olieman, It works! Thank you so much!

posted by Tobias Kuhn 05 Nov 2014