AnalogIn issues

Hello, I have been trying to get back up to speed on program my MBED after years of being away. I have a LPC1768 that I am trying to read an analog input into and shine one of the built in LEDs based on the input but I am having issues. No matter what my input is the 1768 will have LED1 on. I am using a 0 ohm -1kOhm dashpot to simulate what will turn into a sensor.
indent preformatted text by 4 spaces

#include "mbed.h"

AnalogIn ain(p19);

DigitalOut one(LED1);

DigitalOut two(LED2);

DigitalOut three(LED3);

DigitalOut four(LED4);

// main() runs in its own thread in the OS

int main()

{

   float a19 = ain;

    while (true)

    {

        int time = 500000;

        if (0.5 < a19 < 1.5)

        {

            one = 1;

            two = 0;

            three = 0;

            four = 0; 

            wait_us(time);

        }

        else if (1.5 < a19 < 2.5)

        {

            one = 0;

            two = 1;

            three = 0;

            four = 0; 

            wait_us(time);

        }

        else if (2.5 < a19 < 3.0)

        {

            one = 0;

            two = 0;

            three = 1;

            four = 0; 

            wait_us(time);

        }

        else if (3.0 < a19 < 3.3)

        {

            one = 0;

            two = 0;

            three = 0;

            four = 1; 

            wait_us(time);

        }

        else 

        {

            one = 0;

            two = 0;

            three = 0;

            four = 0;

            wait_us(time);

        }

        wait_us(time);

       // ThisThread::sleep_for(2s);

    }

}
indent preformatted text by 4 spaces

Hello,

please be so kind and for correct formatting use ``` before and after your code.

```
//your code here
```

The problem of your code is you have read the value of AnalogIn only one time before loop. So the loop runs with same value without any update.

You can try the example (for one LED) of AnalogIn from Mbed’s documentation.

BR, Jan

1 Like

I think another issue here is that analog inputs always return between 0 (meaning 0V) and 1 (meaning 3.3V).

1 Like

I’m sorry, I didn’t know how to do that.
Thank you for your help, you really pointed me in the right direction.

Thank you! I wish I could split the solution check box between both of you because this really helped me too!

The code from this question that ended up working, incase someone down the road needs an example:

#include "mbed.h"
#include <cmath>
#include <cstdio>

AnalogIn ain(p19);
DigitalOut one(LED1);
DigitalOut two(LED2);
DigitalOut three(LED3);
DigitalOut four(LED4);


// main() runs in its own thread in the OS
int main()
{
    printf("the main code has been reached....\n");
    float ain = !ain;

    while (1)
    {
        AnalogIn ain(p19);
        printf("while loop start...\n");
        int time = 5000000;
        if (0.0 < ain and ain < 0.25)
        {
            printf("First if statement...\n");
            one = 1;
            two = 0;
            three = 0;
            four = 0; 
            wait_us(time);
        }
        else if (0.25 <= ain and ain < 0.5)
        {
            printf("Second if statement...\n");
            one = 0;
            two = 1;
            three = 0;
            four = 0; 
            wait_us(time);
        }
        else if (0.5 <= ain and ain < 0.75)
        {
            printf("Third if statement...\n");
            one = 0;
            two = 0;
            three = 1;
            four = 0; 
            wait_us(time);
        }
        else if (0.75 <= ain and ain < 0.9)
        {
            printf("Fourth if statement...\n");
            one = 0;
            two = 0;
            three = 0;
            four = 1; 
            wait_us(time);
        }
        else if (0.9 <= ain and ain <= 1.1)
        {
            printf("Fifth if statement...\n");
            int i = 0;
            for(i=0; i <= 5; ++i)
            {
                one = 1;
                two = 1;
                three = 1;
                four = 1; 
                wait_us(time/10);
                one = 0;
                two = 0;
                three = 0;
                four = 0; 
                wait_us(time/10);
                one = 1;
                two = 1;
                three = 1;
                four = 1; 
                wait_us(time/10);
                one = 0;
                two = 0;
                three = 0;
                four = 0; 
                wait_us(time/10);
                i++;
            }
        }
        else 
        {
            printf("Else statement...\n");
            one = 0;
            two = 0;
            three = 0;
            four = 0;
            wait_us(time);
        }
        float a = ain*3.3;
        float b = a*1000;
        int c = floor(b); 
        printf("the current voltage is: %d \n", c);
        wait_us(time);
       // ThisThread::sleep_for(2s);


    }
}

you not need these lines, you have that object above as global one.

    printf("the main code has been reached....\n");
    //float ain = !ain;

    while (1)
    {
        //AnalogIn ain(p19);
        printf("while loop start...\n");

BR, Jan

1 Like