The code below takes in values from LDR but only one LED is lit. The other...

Free

50.1K

Verified Solution

Question

Electrical Engineering

The code below takes in values from LDR but only one LEDis lit. The other 3 LEDs are always off. What can I do to correctthis ?

#include \"mbed.h\"

AnalogIn ADC(A5);

BusOut myLEDs(D10, D11, D12, D13); //Using Bus out instead ofDigitalOut for Group of signals in order to control multipleoutputs  

PwmOut Led1(D10);

PwmOut Led2(D11);

PwmOut Led3(D12);

PwmOut Led4(D13);

  

  

int main(void)

  

{

    while(1)

    {

        float brightness;

      //LEDs.period(1);

        brightness=1-ADC.read();

        float I_val;

        I_val=brightness*100;//Intensity value= I_val

        printf(\"lightLevel =%3.1f%%\n\", I_val);//maximum of 5 significant figures andmaximum of 2 decimal places.

        wait(1);

     

    Led1.period(1.0f);//period to be 1second  

    Led2.period(1.0f);

    Led3.period(1.0f);

    Led4.period(1.0f);

     

     

     

    if(I_val<25)

     

    {

        Led1.write(0.0f);//duty cycle as 20% of 1 period (1 second)

        Led2.write(0.0f);

        Led3.write(0.0f);

        Led4.write(0.0f);

        break;

    }  

    else if(I_val >=25 && I_val<50)

     

    {

       Led1.write(0.25f);

       Led2.write(0.25f);

       Led3.write(0.25f);

       Led4.write(0.25f);        

        break;

    }

     

    else if(I_val >=50 && I_val<75)

    {

       

       Led1.write(0.50f);

       Led2.write(0.50f);

       Led3.write(0.50f);

       Led4.write(0.50f);

        break;

    }

     

    else if (I_val >=75 &&I_val<100)

     

    {

         

       Led1.write(0.75f);

       Led2.write(0.75f);

       Led3.write(0.75f);

       Led4.write(0.75f);

        break;

    }

     

    else if (I_val ==100)    

   {       

        Led1.write(1);

        Led2.write(1);

        Led3.write(1);

        Led4.write(1);

        break;

    }

    else if (I_val<0 && I_val>100)

    {

        printf(\"ERROR!\");

    }

    }

     

     

}

The task is to use mbed library with  Nucleo64 bits STM32f303RE ARM board and the four redleds as lighting devices. There are five required levelsof lighting, 1, 2, 3, 4 and off. The lighting level is controlledby the lightness/darkness detected by the LDR sensor, for examplewhen it is very light, all the leds will be off, and when it isvery dark all the four leds will be on. All the 5 levels oflighting will be corresponded to the evenly distributed ‘ReadIn’value from the LDR. PWM control is not required for this task.

That is,  

[1.] when the darkness corresponding to 25% then one ofthe 4 led will be lit with 25% of duty cycleand  

[2.] when darkness is equivalent to 50% then the 2 of 4LEDs will be lit with 50% duty cycle and   

[3.] when when darkness corresponds 75% then3 of 4 LEDs will be lit with 75% duty cycle and   

[4.] with complete darkness all the 4 LEDs withbe lit with 100% duty cycle of brightness

Answer & Explanation Solved by verified expert
4.3 Ratings (718 Votes)

#include \"mbed.h\"

AnalogIn ADC(A5);

BusOut myLEDs(D10, D11, D12, D13); //Using Bus out instead of DigitalOut for Group of signals in order to control multiple outputs  

PwmOut Led1(D10);

PwmOut Led2(D11);

PwmOut Led3(D12);

PwmOut Led4(D13);

  

  

int main(void)

  

{

while(1)

{

float brightness;

//LEDs.period(1);

brightness= 1-ADC.read();

float I_val;

I_val =brightness*100;//Intensity value= I_val

printf(\"light Level =%3.1f%%\n\", I_val);//maximum of 5 significant figures and maximum of 2 decimal places.

wait(1);

Led1.period(1.0f);//period to be 1 second  

Led2.period(1.0f);

Led3.period(1.0f);

Led4.period(1.0f);

if(I_val<25)

{

Led1.write(0.0f); //duty cycle as 20% of 1 period (1 second)

Led2.write(0.0f);

Led3.write(0.0f);

Led4.write(0.0f);

break;

}  

else if(I_val >=25 && I_val <50)

{

Led1.write(0.25f);

Led2.write(0.25f);

Led3.write(0.25f);

Led4.write(0.25f);   

break;

}

else if(I_val >=50 && I_val <75)

{

Led1.write(0.50f);

Led2.write(0.50f);

Led3.write(0.50f);

Led4.write(0.50f);

break;

}

else if (I_val >=75 && I_val<100)

{

Led1.write(0.75f);

Led2.write(0.75f);

Led3.write(0.75f);

Led4.write(0.75f);

break;

}

else if (I_val == 100)   

{

Led1.write(1);

Led2.write(1);

Led3.write(1);

Led4.write(1);

break;

}

else if (I_val<0 && I_val >100)

{

printf(\"ERROR!\");

}

}

}


Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students