In: Electrical Engineering
The code below takes in values from LDR but only one LED is lit. The other 3 LEDs are always off. What can I do to correct this ?
#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!");
}
}
}
The task is to use mbed library with Nucleo 64 bits STM32f303RE ARM board and the four red leds as lighting devices. There are five required levels of lighting, 1, 2, 3, 4 and off. The lighting level is controlled by the lightness/darkness detected by the LDR sensor, for example when it is very light, all the leds will be off, and when it is very dark all the four leds will be on. All the 5 levels of lighting 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 of the 4 led will be lit with 25% of duty cycle and
[2.] when darkness is equivalent to 50% then the 2 of 4 LEDs will be lit with 50% duty cycle and
[3.] when when darkness corresponds 75% then 3 of 4 LEDs will be lit with 75% duty cycle and
[4.] with complete darkness all the 4 LEDs with be lit with 100% duty cycle of brightness
#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!");
}
}
}