Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] LED State machine to detect and indicate a fault

Status
Not open for further replies.
Joined
Sep 21, 2020
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
36
New to C, I have an int defined as LED_FLASH that has all bit's alternating by using LED_FLASH = ~LED_FLASH at a given rate coming from the timer ISR, what I need is to have three states of the LED. The circuit is 8 relays with a circuit breakers and a feedbacks to the uC in order to get the status of the output, LED needs to be ON if relay is ON and working normally, OFF if relay is OFF, and FLASH if no feedback is detected to indicate a fault condition (open breaker)... I am having trouble figuring out the logic of detecting a fault and get the led to change from ON to FLASH. I have all electronics working at the point where I can get relays to turn on/off, leds to turn on/off and read feedback status, registers are defined as int RELAY_DATA, int LED_DATA, and int FEEDBACK_DATA- How would one go about doing the logic? Thank you -Ryan
 

Hi,

a textual description isn´t useful.
--> show us the schematic and your code.

Klaus
 

Classic hand drawn flow diagram helps indicate where and when to do tests
and modify hardware.

1630427881970.png





Regards, Dana.
 

New to C, I have an int defined as LED_FLASH that has all bit's alternating by using LED_FLASH = ~LED_FLASH at a given rate coming from the timer ISR, what I need is to have three states of the LED. The circuit is 8 relays with a circuit breakers and a feedbacks to the uC in order to get the status of the output, LED needs to be ON if relay is ON and working normally, OFF if relay is OFF, and FLASH if no feedback is detected to indicate a fault condition (open breaker)... I am having trouble figuring out the logic of detecting a fault and get the led to change from ON to FLASH. I have all electronics working at the point where I can get relays to turn on/off, leds to turn on/off and read feedback status, registers are defined as int RELAY_DATA, int LED_DATA, and int FEEDBACK_DATA- How would one go about doing the logic? Thank you -Ryan
sounds like you need an if statement in your decision making
Code:
if((relay == on) && (feedbackstatus == ok)
{
ledon();
}
else if((relay == on) && (feedbackstatus == not_ok))
{
led flash()
}
else
{
ledoff();
}
excuse the syntax :)

To detect the flags, i'd use the BITWISE operators
lets assume you have a variables called LEDS, RELAYS, & FEEDBACKS with 8 Bits as flags
Code:
#define BITMASK 1
#define on 1
#define ok 1
#define notok 0


for (int x = 0; x < 8; x++)
{
relay = RELAYS & BITMASK; //Clears all flags except bit 0
led = LEDS & BITMASK; //Clears all flags except bit 0
feedbackstatus = FEEDBACK & BITMASK; //Clears all flags except bit 0

if((relay == on) && (feedbackstatus == ok)
{
ledon(x);
}
else if((relay == on) && (feedbackstatus == not_ok))
{
led flash(x)
}
else
{
ledoff(x);
}

RELAYS =  RELAYS >> 1; //shift flags by 1 so on next iteration you check BIT1 and so on
LEDS = LEDS >> 1; //shift flags by 1 so on next iteration you check BIT1 and so on
FEEDBACKS = FEEDBACKS >> 1; //shift flags by 1 so on next iteration you check BIT1 and so on
}
Hopefully theres enough there for you to get started or at least get you moving in the right direction.
 
Last edited:

Code:
#define OUTPUT_LED_1_ON             OUTPUT_LED_DATA_A0 |= 1 << 0;
#define OUTPUT_LED_1_OFF            OUTPUT_LED_DATA_A0 &= ~(1 << 0);

void Check_Output_Status(void)
{
    Read_Output_Feedback();                                                     //Read Output Feedback's

    if ( (OUTPUT_RLY_DATA_A0 == 0b00000001) && (OUTPUT_FB_DATA_A == 0b00000001)){
    OUTPUT_LED_1_ON;
    }
    else{
    OUTPUT_LED_1_OFF; 
    }

    Send_LED_Data();                                                            //Send to LED Driver
}

I can't get the if statement to work, I can turn led's on or off by calling OUTPUT_LED_1_ON; or OUTPUT_LED_1_OFF; The LED's go to I/O expander IC and Feedback is coming from I/O expander via I2C am I missing something?
if will work if I do:
Code:
if ( OUTPUT_RLY_DATA_A0 == OUTPUT_FB_DATA_A ){
OUTPUT_LED_1_ON;
}
else {
OUTPUT_LED_1_OFF;
}

but I would like to test each bit since I have 8 I/O's
 
Last edited:

Code:
#define OUTPUT_LED_1_ON             OUTPUT_LED_DATA_A0 |= 1 << 0;
#define OUTPUT_LED_1_OFF            OUTPUT_LED_DATA_A0 &= ~(1 << 0);

void Check_Output_Status(void)
{
    Read_Output_Feedback();                                                     //Read Output Feedback's

    if ( (OUTPUT_RLY_DATA_A0 == 0b00000001) && (OUTPUT_FB_DATA_A == 0b00000001)){
    OUTPUT_LED_1_ON;
    }
    else{
    OUTPUT_LED_1_OFF; 
    }

    Send_LED_Data();                                                            //Send to LED Driver
}

I can't get the if statement to work, I can turn led's on or off by calling OUTPUT_LED_1_ON; or OUTPUT_LED_1_OFF; The LED's go to I/O expander IC and Feedback is coming from I/O expander via I2C am I missing something?
if will work if I do:
if ( OUTPUT_RLY_DATA_A0 == OUTPUT_FB_DATA_A ){
OUTPUT_LED_1_ON;
}
else {
OUTPUT_LED_1_OFF;
}

but I would like to test each bit since I have 8 I/O's
From your code is looks like your expecting OUTPUT_RLY_DATA_A0 AND OUTPUT_FB_DATA_A to be a value of 1 so if the IF statement isn't turning the LEDON its because the value of the registers arn't 1.

By doing OUTPUT_RLY_DATA_A0 == OUTPUT_FB_DATA_A you are compararing two registers, which to turn the LED on would need to be the same.

If you look at the code i suggested you need to have a FOR LOOP to shift through the register to look at one BIT at a time

Code:
#define BITMASK 1
#define on 1
#define ok 1
#define notok 0

for (int x = 0; x < 8; x++) //Shift throught the Register 8 Times
{

//you need to mask the bits your not interested in
relay = RELAYS & BITMASK; //Clears all flags except bit 0
led = LEDS & BITMASK; //Clears all flags except bit 0
feedbackstatus = FEEDBACK & BITMASK; //Clears all flags except bit 0

if((relay == on) && (feedbackstatus == ok)
{
ledon(x);
}
else if((relay == on) && (feedbackstatus == not_ok))
{
led flash(x)
}
else
{
ledoff(x);
}

//You need to shift the register so you can look at the next BIT

RELAYS =  RELAYS >> 1; //shift flags by 1 so on next iteration you check BIT1 and so on
LEDS = LEDS >> 1; //shift flags by 1 so on next iteration you check BIT1 and so on
FEEDBACKS = FEEDBACKS >> 1; //shift flags by 1 so on next iteration you check BIT1 and so on
}
 

Scopeprobe, thank you for your ideas, I tried to get your example to work but I still cannot get it to work the way I have in mind.... I am able to get it to detect a fault by doing-

Code:
void Check_Output_Status(void)
{
    Read_Output_Feedback();                                                     //Read Output Feedback's
    if ( OUTPUT_RLY_DATA_A0 == OUTPUT_FB_DATA_A ){
         OUTPUT_LED_DATA_A0 = OUTPUT_FB_DATA_A;
  }
    else{
    OUTPUT_LED_DATA_A0 = LED_FLASH;
    }

    Send_LED_Data();                                                            //Send to LED Driver
}

If Feedback is not the same as Relay data then a fault is present, if a relay is on and feedback is okay it will light output led and if a relay is off output led will be off, where I am stuck now is when a fault is detected ALL led's flash because right now I have OUTPUT_LED_DATA_A0 matching LED_FLASH, I need to somehow make a bit mask to mask out "GOOD" outputs and only flash output's with a fault condition.... Sorry if I don't make much since, it's hard for me to explain as I'm new to "C" I have code working in assembly but would like to learn in C -Ryan
 
Last edited by a moderator:

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top