8 years, 6 months ago.

coding for ir sensor

i want to make a coding with input of ir sensor and the output of two different leds; red and green. can u help me or suggest the coding that i should make

1 Answer

5 years, 7 months ago.

Hello there,

I do understand that this question has been asked 2 years ago. But I would like to contribute to the answer. The below code is a rough sketch that works well. Here is my code on Mbed as well. https://os.mbed.com/users/shiva_shankar/code/LDR/

IR example

#include "mbed.h"


DigitalIn IR(PB_3);

BusOut leds(PB_4, PB_5, PA_11, PA_8);

int main()
{

    /* Optional: set mode as PullUp/PullDown/PullNone/OpenDrain */
    IR.mode(PullNone);

    while(1) {
        printf("IR has value of %d \n", IR.read());
        if (IR.read() == 0) {
            leds = 0xff;
            printf("leds are ON \n");
        } else {
            leds = 0x00;
            printf("leds are OFF \n");
        }
    }
}


If you have any questions, please feel free to ask. I am a Mbed fan, so I will try to answer them as much as possible.