analogin creating LED flicker

02 Nov 2012

I'm trying to take an analogin signal (0 to 3.3V) and turn a digitalout and an LED on or off based on the analogin signal.

If the analogin signal is below 0.9V,(0.9/3.3 = 0.27) I want led4 to turn on and I want pin 24 to go high. If the analog signal is above 3V, (3/3.3 = 0.92) then I want led3 to turn on and pin 25 to go high. If the voltage is between 0.9 and 3V, then I want neither the leds nor pins 24 and 25 on.

The code I've written is below. The final product will be more complicated and have more function than this, but the problem I'm having at this point is that the leds and the outputs are flickering. My mbed is powered by an external 5V power supply with a 0.1uF tantalum capacitor and a 2200uF capacitor across the terminals to smooth out the power supply. Pins 24 and 25 are connected to 1200 ohm resistors going to the base leg of a 2N2222A transistor to switch the voltage higher for input to a motor controller, but the results are unpredictable based on the flickering LEDs and output on pins 24 and 25.

I can capture the analogin signal. It's a nice smooth sinusoidal-esque signal. It does not fluctutate rapidly above the thresholds set for the output pins and leds. What can I do to stop the erratic flickering?

//EBRGT9
#include "mbed.h"

AnalogIn loadright(p16);
DigitalOut right1(p25);
DigitalOut right2(p24);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

int main() {
    led1=led2=led3=led4=0;
    right1=right2=0;
    wait(0.5);
    wait (1);
    led1=1;
    wait(0.1);
    led2=1;
    wait(0.1);
    led1=0;;
    led3=1;
    wait(0.1);
    led4=1;
    led2=0;
    wait(0.1);
    led3=0;
    wait(0.1);
    led4=0;
    
    if (loadright <0.27){
        right2=led4=1;
        right1=led3=0;
        wait(0.1);
        
    }    
    else if (loadright >0.92){
        right1=led3=1;
        right2=led4=0;        
    }
    
    else led1=led2=led3=led4=left1=left2=right1=right2=0;     
    }    
}
03 Nov 2012

Hi Jeff,

I may have misunderstood the exact requirement, but perhaps something like this is what you are after:

Import program

00001 #include "mbed.h"
00002  
00003 AnalogIn input(p16);
00004 
00005 DigitalOut pin_low(p24);
00006 DigitalOut pin_high(p25);
00007 DigitalOut led_low(LED4);
00008 DigitalOut led_high(LED3);
00009  
00010 #define THRESHOLD_LOW (0.9 / 3.3)
00011 #define THRESHOLD_HIGH (3.0 / 3.3)
00012 
00013 int main() {
00014     while(1) {
00015         pin_low = led_low = (input < THRESHOLD_LOW);
00016         pin_high = led_high = (input > THRESHOLD_HIGH);
00017     }
00018 }

Hope that helps,

Simon

03 Nov 2012

Hi Simon,

That works. Thank you. It eliminated the flicker. I understand why it works. I don't understand why my way doesn't work though. Perhaps it's not important.

For the sake of clarity, I left out some of the details of my program. I will need the pins to turn on and stay on for 0.1 second. With a if-then loop, this was easy. I just put in a wait(0.1) statement after the "then" statement. I'm not so sure how to do it with this way.

Any ideas?