flow chart for different conditions

Thread Starter

Parth786

Joined Jun 19, 2017
642
I am trying to write c program for LED with two switches. one LED and two switches are connected to micro controller.
I want to develop flow chart for below conditions
Code:
SW1    SW2     LED
OFF     OFF      OFF
OFF     ON       OFF
ON      OFF      OFF
ON       ON      ON
Here is my flow chart
upload_2017-7-3_20-22-10.png
I am not sure this flow chart are correct or not , If its correct Than I will show you my code ?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Good attempt. However, see if you can simplify it to a single test.
sorry I am not getting your point
my approach ( work on paper)
Code:
main ()
{
  Led off
while (1)
if (SW1==1&& SW2==1)
     {
      LED ON
      }
else if (SW0==0&& SW2==1)
     {
      LED OFF
      }
else if  (SW1==1&& SW2==0)
      {
      LED OFF
      }
else
   {
      LED OFF
      }
 

WBahn

Joined Mar 31, 2012
30,052
sorry I am not getting your point
my approach ( work on paper)
Code:
main ()
{
  Led off
while (1)
if (SW1==1&& SW2==1)
     {
      LED ON
      }
else if (SW0==0&& SW2==1)
     {
      LED OFF
      }
else if  (SW1==1&& SW2==0)
      {
      LED OFF
      }
else
   {
      LED OFF
      }
Your code has a syntax error since your main is not closed. Also, although your while() statement only includes a single statement, it is long enough that it is better to enclose it in braces. Also try to use consistent case, consistent spacing, and proper indenting.

For instance:

Code:
main ()
{
   LED OFF;
   while (1)
   {
      if (SW1 == 1 && SW2 == 1)
      {
         LED ON;
      }
      else if (SW0 == 0 && SW2 == 1)
      {
         LED OFF;
      }
      else if (SW1 == 1 && SW2 == 0)
      {
         LED OFF;
      }
      else
      {
         LED OFF;
      }
   }
}
This code, while functionally equivalent to your flowchart, does not match.

Your flowchart says that the first thing you ask is if both switches are off.

Consider your code here. If the first test fails, then EVERY path after that does the same thing -- turns the LED off. So what is gained by making the subsequent tests.

Look at your original truth table and try to summarize it in the simplest English sentence.

IF both switches are 1, turn on the LED else turn it off.

Does that description give a strong hint as to how to write the code for it.

Most programming languages are designed so that natural ways of expressing logical statements translate very directly into source code.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Your code has a syntax error since your main is not closed. Also, although your while() statement only includes a single statement, it is long enough that it is better to enclose it in braces. Also try to use consistent case, consistent spacing, and proper indenting.
I didn't compiled that code. I mentioned that this is only paper work. that was just hand made code on paper for general understanding

Does your code match the flow chart?
this code match with flow chart
Code:
#include<REGX51.h>

#define LED_ON     (1)
#define LED_OFF    (0)
#define SW1_ON     (1)
#define SW1_OFF    (0)
#define SW2_ON     (1)
#define SW2_OFF    (0)

/*set bit P1^7 to LED*/
sbit LED = P1^7;

/*set bit P2^0 to Switch*/
sbit  SW1 = P0^0;
sbit  SW2 = P2^0;

void main ()
{
   LED = LED_OFF;
   while (1)
   {
      if (SW1 == 0 && SW2 == 0)
      {
         LED = LED_OFF;
      }
      else if (SW1 == 0 && SW2 == 1)
      {
         LED = LED_OFF;
      }
      else if (SW1 == 1 && SW2 == 0)
      {
         LED = LED_OFF;
      }
            else if (SW1 == 1 && SW2 == 1)
      {
         LED = LED_ON;
      }
      else
      {
         LED = LED_OFF;
      }
   }
}
hardware design
upload_2017-7-4_15-54-58.png

I think there is problem in hardware design. I am using simulator software (proetus) to test my code I have tested my code but in third condition when SW1 =1 and SW2= 0 the LED should be turn OFF. but its not happening in this conditions. LED is ON
when SW1 =1 and SW2= 0 , LED is ON
I don't think that there is problem in my program. what's the problem? why its happening ?
 
Last edited:

MrChips

Joined Oct 2, 2009
30,802
1. Your code does not match the flow chart.
2. Your flow logic is too complicated.
3, Your circuit shows an input pin going to logic high when the switch is closed. What makes the input pin go to logic low?
4. 1nF on C1 and C2 is 100 times too large.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Your code has a syntax error since your main is not closed. Also, although your while() statement only includes a single statement, it is long enough that it is better to enclose it in braces. Also try to use consistent case, consistent spacing, and proper indenting.

For instance:

Code:
main ()
{
   LED OFF;
   while (1)
   {
      if (SW1 == 1 && SW2 == 1)
      {
         LED ON;
      }
      else if (SW0 == 0 && SW2 == 1)
      {
         LED OFF;
      }
      else if (SW1 == 1 && SW2 == 0)
      {
         LED OFF;
      }
      else
      {
         LED OFF;
      }
   }
}
This code, while functionally equivalent to your flowchart, does not match.

Your flowchart says that the first thing you ask is if both switches are off.

Consider your code here. If the first test fails, then EVERY path after that does the same thing -- turns the LED off. So what is gained by making the subsequent tests.

Look at your original truth table and try to summarize it in the simplest English sentence.

IF both switches are 1, turn on the LED else turn it off.

Does that description give a strong hint as to how to write the code for it.

Most programming languages are designed so that natural ways of expressing logical statements translate very directly into source code.
1. Your code does not match the flow chart.
2. Your flow logic is too complicated.
3, Your circuit shows an input pin going to logic high when the switch is closed. What makes the input pin go to logic low?
4. 1nF on C1 and C2 is 100 times too large.
Your code does not match the flow chart
what's the wrong flow chart or code
Your flow logic is too complicated
all expert member suggest me first make flow chart, so I made flow chart first
Your circuit shows an input pin going to logic high when the switch is closed. What makes the input pin go to logic low?
I think switch is open means there is 0v on MCU pin
and if switch is closed means , there is 5v on mcu pin
things are going really complicated for me
 

blocco a spirale

Joined Jun 18, 2008
1,546
The point is, you only need to test for one condition to turn the LED on; SW1 AND SW2 = True, anything else and the LED is off so there is no need to test for every LED OFF condition.
 

MrChips

Joined Oct 2, 2009
30,802
what's the wrong flow chart or code

all expert member suggest me first make flow chart, so I made flowchart first
Yes, you are correct. Draw flowchart first. Then write code.
If the code does not match the flowchart then there is no point in drawing the flowchart.

It is like drawing a blueprint before building a house. What is the point of the blueprint if the house is then built differently?

2) Your code does not match your flow chart.

1) Your flowchart is too complicated. Redraw the flowchart first then fix the code second.
 

WBahn

Joined Mar 31, 2012
30,052
I didn't compiled that code. I mentioned that this is only paper work. that was just hand made code on paper for general understanding
Oh, I figured that the code was essentially pseudocode. That doesn't change any of my comments. Your pseudocode is meant to communicate the logical structure of your code in an unambiguous manner. To do that it needs to be formatted properly and consistently.

this code match with flow chart
Look at your flowchart. What does it show that you do immediately after you execute LED ON at the bottom of it?

Is this what you want to do?

Is this what your code does?

I think there is problem in hardware design. I am using simulator software (proetus) to test my code I have tested my code but in third condition when SW1 =1 and SW2= 0 the LED should be turn OFF. but its not happening in this conditions. LED is ON
when SW1 =1 and SW2= 0 , LED is ON
I don't think that there is problem in my program. what's the problem? why its happening ?
Add two more LEDs that do nothing except indicate the state of SW1 and SW2.

Do they represent things as you expect?

What is the state of an input pin that is not connected to anything? You seem to be assuming that it is a 0, but is this guaranteed to be the case?

Try putting pulldown resistors on those inputs.

I don't know about this particular micro, but it is generally a bad idea to leave unused inputs flowing unless the part is specifically designed to allow it.

Do you need to do anything to establish P0 as an input port?
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
Ok , here is my another approach with simple flow chart
upload_2017-7-4_22-16-9.png
program code
Code:
#include<REGX51.h>

#define LED_ON     (1)
#define LED_OFF    (0)
#define SW1_ON     (1)
#define SW1_OFF    (0)
#define SW2_ON     (1)
#define SW2_OFF    (0)

/*set bit P1^7 to LED*/
sbit LED = P1^7;

/*set bit P2^0 to Switch*/
sbit  SW1 = P0^0;
sbit  SW2 = P2^0;

void main ()
{
   LED = LED_OFF;
   while (1)
   {
      if (SW1 == 1 && SW2 == 1)
      {
         LED = LED_ON;
      }
   
      else
      {
         LED = LED_OFF;
      }
   }
}
hardware design
upload_2017-7-4_22-18-59.png
another end of resistors connected to ground
Now Its all that I wanted to do, I have tested every condition, its work fine for me. so If do you have any suggestion let me know ?
 
Last edited:

JohnInTX

Joined Jun 26, 2012
4,787
I think you need to express your test as
if ((SW1 == 1) && (SW2 == 1))
to account for C's order of evaluation and operator precedence. With that change and the way it is currently wired, the LED will be ON until you push either or both of the switches - an open switch is a logic '1'.

As I mentioned in the other thread, I don't think that P1.7 will source enough current to turn on the LED.
Your switches aren't debounced but it doesn't mater here.
Have you tried it?

I like your use of #defines for the IO.
Making progress!
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
I think you need to express your test as
if ((SW1 == 1) && (SW2 == 1))
.
its look good,
As I mentioned in the other thread, I don't think that P1.7 will source enough current to turn on the LED.
Your switches aren't debounced but it doesn't mater here.
Have you tried it?
I am working on simulator, so there is no problem. I have not tested on real hardware
This could be simplified to:
if (SW1 && SW2)
yahhh its work
so thank to all of you for your help
 

dl324

Joined Mar 30, 2015
16,916
i found ternary operator
That would have been the next step:
C:
while (1)
   {
      if (SW1 == 1 && SW2 == 1)
      {
         LED = LED_ON;
      }
 
      else
      {
         LED = LED_OFF;
      }
   }
becomes
C:
while (1) ((SW1 && SW2) ? LED = LED_ON : LED = LED_OFF);
The outer () are for readability.
 

WBahn

Joined Mar 31, 2012
30,052
Ok , here is my another approach with simple flow chart
View attachment 130332
Think about this flowchart. It has a number of issues.

Once you turn on the LED, you are done. You are out of the loop. It doesn't matter what you do with the switches after that, the LED is on and will stay on. Is that the behavior you want?

Your code still does not match your flowchart.

Your flowchart shows the LED OFF block INSIDE the loop! It also happens BEFORE the test, so that is NOT a while() loop, but rather a do/while() loop.

The test condition you show in your decision block doesn't indicate how SW1 and SW2 are combined. If you want it to be (SW1 == 1) and (SW2 == 1), then you NEED to include the AND, because it could easily be OR or even XOR and make perfect sense.

This is the code that matches this flowchart

Code:
void main(void)
{
   do
   {
      LED = LED_OFF;
   } while ( (SW1 == 1) && (SW2 == 1) );

   LED = LED_ON;
}
I think you would agree that this is not what you want -- but this IS the flowchart you have.

so If do you have any suggestion let me know ?
Recall the English description I gave back in Post #5:

IF both switches are 1, turn on the LED else turn it off.

Turn this into a program statement

Code:
if ( (SW1 == 1) && (SW2 == 1) )
   LED = LED_ON;
else
   LED = LED_OFF;
You then want to do this repeatedly in a loop. So your main is just

Code:
void main(void)
{
   while (1)
   {
      if ( (SW1 == 1) && (SW2 == 1) )
         LED = LED_ON;
      else
         LED = LED_OFF;
   }
}
As pointed out previously, C defined any nonzero value to be a logical TRUE, so you could write your test as

Code:
      if ( SW1 && SW2 )
and this is very commonly done and considered acceptable style.

You could also leverage this defined behavior to write your test as either

Code:
      if ( SW1 * SW2 )
But this is poor style and will likely execute slower.

However, another alternative is

Code:
      if ( SW1 & SW2 )
This uses a bitwise AND and may execute faster than the logical AND. It is still not great style, but if the logical AND doesn't execute fast enough and this one does, sometimes ya gotta do what ya gotta do.

You can also use the ternary operator (aka, the conditional operator) and tighten things up even more.

Code:
void main(void)
{
   while (1)
      LED = (SW1 && SW2)? LED_ON : LED_OFF;
}
 

WBahn

Joined Mar 31, 2012
30,052
I think you need to express your test as
if ((SW1 == 1) && (SW2 == 1))
to account for C's order of evaluation and operator precedence. With that change and the way it is currently wired, the LED will be ON until you push either or both of the switches - an open switch is a logic '1'.

As I mentioned in the other thread, I don't think that P1.7 will source enough current to turn on the LED.
Your switches aren't debounced but it doesn't mater here.
Have you tried it?

I like your use of #defines for the IO.
Making progress!
While I also recommend (if you aren't going to just use (SW1 && SW2), that is)

if ((SW1 == 1) && (SW2 == 1))

for readability and so that you aren't giving the compiler the choice of how to evaluate things, the inner parens are not needed because the C order of precedence places relational operators before logical operators.
 
Top