Question

In: Electrical Engineering

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

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

Solutions

Expert Solution

#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!");

}

}

}


Related Solutions

Using the code below from “LStack.h” file, write the code for a main program that takes...
Using the code below from “LStack.h” file, write the code for a main program that takes as input an arithmetic expression. The program outputs whether the expression contains matching grouping symbols. For example, the arithmetic expressions { 25 + ( 3 – 6 ) * 8 } and 7 + 8 * 2 contains matching grouping symbols. However, the expression 5 + { ( 13 + 7 ) / 8 - 2 * 9 does not contain matching grouping symbols....
Using Arduino to blink an LED in morse code. Morse code is used as one of...
Using Arduino to blink an LED in morse code. Morse code is used as one of the methods for communications. It consists of a series of “dit” and “dah” symbols. (i) Develop an Arduino program to produce a Morse code to express the phrase “We are students” using Pin #13 (connect an LED that is in series with a 220-Ω resistor to Pins #13 to view the information sent via the Morse code). Express one “dit” with LED on for...
(a) Let X be a continuous random variable which only takes on positive values on the...
(a) Let X be a continuous random variable which only takes on positive values on the interval [1, 4]. If P(X) = (√ x + √ 1 x )C 2 for all x in this interval, compute the value of C. (b) Let X be a random variable with normal distribution. Let z represent the z-score for X, and let a be a positive number. Prove that P(z < |a|) = P(z < a) + P(z > −a) − 1.
The variable x takes the values ​​from 1 to 100, with these values, create two lists,...
The variable x takes the values ​​from 1 to 100, with these values, create two lists, one where the prime numbers are stored in one, and another where the non-prime numbers are stored, indicate and print the result
One feature that separates Linux from other operating systems is that the source code, along with...
One feature that separates Linux from other operating systems is that the source code, along with any changes, remains public.   Closed source operating systems, where developers refuse to share some or all of the code, may hinder third-party software developers who create programs and apps for the operating system. Supporters of open source maintain that open source software enables developers to examine, correct and enhance code to create better programs. Communities of open source programmers can make changes immediately, which...
(C++ only please) Write a function called maximum that takes an array of double values as...
(C++ only please) Write a function called maximum that takes an array of double values as a parameter and returns the largest value in the array. The length of the array will also be passed as a parameter. (Note that the contents of the array should NOT be modified.) Write a function called printReverse that takes an array of characters and the length of the array as parameters. It should print the elements of the array in reverse order. The...
FOR ARDUINO PROGRAMMING; WRITE CODE TO FIT THE BELOW REQUIREMENTS; 1. LED 1 TURNS ON AND...
FOR ARDUINO PROGRAMMING; WRITE CODE TO FIT THE BELOW REQUIREMENTS; 1. LED 1 TURNS ON AND STAYS ON THE ENTIRE TIME THE BOARD IS RUNNING EXCEPT AT 30 SECOND INTERVALS THEN LED 1 TURNS OFF AND BACK ON 2. LED 2 TURNS ON IN LIGHT CONDITIONS AND OFF IN DARK CONDITION THANK YOU!
FOR ARDUINO PROGRAMMING; WRITE CODE TO FIT THE BELOW REQUIREMENTS 1. LED 3 TURNS ON IN...
FOR ARDUINO PROGRAMMING; WRITE CODE TO FIT THE BELOW REQUIREMENTS 1. LED 3 TURNS ON IN DARK CONDITIONS AND OFF IN LIGHT CONDITIONS 2. LED 4 TURNS ON WITH FIRST BUTTON PRESS AND STAYS ON UNTIL A SECOND BUTTON PRESS
(Python) a) Using the the code below write a function that takes the list xs as...
(Python) a) Using the the code below write a function that takes the list xs as input, divides it into nss = ns/nrs chunks (where nrs is an integer input parameter), computes the mean and standard deviation s (square root of the variance) of the numbers in each chunk and saves them in two lists of length nss and return these upon finishing. Hint: list slicing capabilities can be useful in implementing this function. from random import random as rnd...
1. Consider the following question: “A headlamp has one LED, takes three batteries, and has a...
1. Consider the following question: “A headlamp has one LED, takes three batteries, and has a switch that toggles between different circuit configurations. What circuit configuration will get the brightest light from the LED, and what configuration will provide the longest battery life? A different lamp has two LEDs, one battery, and a switch. Answer the same questions for this lamp.” Attempt to answer this question and make note of what concepts are involved. 2. Using this question and the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT