Does my Flow chart match with program

Thread Starter

Parth786

Joined Jun 19, 2017
642
This is a big step back. Your flowchart doesn't match your code and neither is what you want.
Whats wrong program. or flow chart

Does it not do following things ?

while switch is closed , turn ON LED ) (check only one condition switch is closed again and again repeatedly and if true turn ON LED

while switch is open , turn OFF LED (check only one condition switch is open again and again repeatedly and if true turn of LED)

Your post #19 is very informative for me . What is next Exercise for me ?
 
Last edited:

WBahn

Joined Mar 31, 2012
32,882
Whats wrong program. or flow chart

Does it not do following things ?

while switch is closed , turn ON LED ) (check only one condition switch is closed again and again repeatedly and if true turn ON LED

while switch is open , turn OFF LED (check only one condition switch is open again and again repeatedly and if true turn of LED)

Your post #19 is very informative for me . What is next Exercise for me ?
Look at the flowchart. After entering the main loop it goes into a loop that sits there doing nothing as long as the check for the switch being closed is false. Now look at your code. After entering the main loop it goes into loop that continually sets the LED to ON as long as the check for the switch being closed is true. In the flow chart, your LED = xxx; statements are not in either of the inner loops, while that is exactly where they are in the code.

In either case, you are really limiting your self. Your primary loop (the while(1) loop) need to be able to service all of the program needs quickly on each pass. For instance, let's say you had two switches, each controlling an LED. How would you modify this code to service both switches? Your present code will stall at each inner while() loop until the first switch changes state. In your outermost loop, you want to check the switch once and service its needs. Then you move on to the next item, such as checking another switch, and do that once. You service everything that needs servicing once and then go back to the top and start over. You do NOT want your code stalling within the inner loop waiting for some event to happen.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
let's say you had two switches, each controlling an LED. How would you modify this code to service both switches?.
Your code is more efficient then my code . I need to study more
Does this what you point for two switches.
So I need to test one condition to turn the LED ON, Switch_1 AND Switch2 = True,
C:
void main(void)
{
     while (FOREVER)

     LED  = (Swithch_1  &&  Switch_2) ?  LED_ON  :  LED_OFF;
}
 

WBahn

Joined Mar 31, 2012
32,882
Your code is more efficient then my code . I need to study more
Does this what you point for two switches.
So I need to test one condition to turn the LED ON, Switch_1 AND Switch2 = True,
C:
void main(void)
{
     while (FOREVER)

     LED  = (Swithch_1  &&  Switch_2) ?  LED_ON  :  LED_OFF;
}

You STILL need to pay attention to indenting.

It is actually quite inconsiderate to continually expect others to jump through hoops to follow your code just because you insist on being too lazy to make it easily readable. If it's not too much trouble for the people trying to help you to properly format their code so that you can read it more easily, why is it so unreasonable to expect a comparable amount of courtesy in return?

It appears the only way that I might be able to get you to stop being sloppy is to stop providing feedback on your code unless it is properly formatted.

So here is as good a place to start as any. Properly format this code and then I will provide feedback.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
You STILL need to pay attention to indenting.

Properly format this code and then I will provide feedback.
Does it look proper code
C:
#include<REGX51.h>

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

/*set bits to Switch*/
sbit  Switch1  =   P2^0;
sbit  Switch2  =   P2^1;

#define TRUE          (1)
#define FALSE         (0)
#define FOREVER       (TRUE)

#define LED_ON         (1)
#define LED_OFF        (0)

#define Switch1_ON     (1)
#define Switch2_ON     (1)

void main(void)

{
        while (FOREVER)
         {
             LED  =  (Switch1  ==  Switch1_ON  &&  Switch2  ==  Switch2_ON)  ?  LED_ON  :  LED_OFF;
         }
}
 

MrChips

Joined Oct 2, 2009
34,829
What is the point of the following?

LED = (boolean expression) ? 1 : 0;

It is the same as

LED = (boolean expression);

Do you understand the following?

LED = Switch;
 

WBahn

Joined Mar 31, 2012
32,882
What is the point of the following?

LED = (boolean expression) ? 1 : 0;

It is the same as

LED = (boolean expression);

Do you understand the following?

LED = Switch;
The difference is in readability and maintainability.

If you have:

LED = switch;

you really have no idea what it is doing other than the state of the LED being related, somehow, to the state of the switch.

Even if you do know (and adequately document it for other readers) that a 0 is open for a switch and off for an LED and 1 otherwise, you have left yourself with a maintenance nightmare.

There is nothing that says that a switch being on will always be a 1 or that a 1 will always be what is needed to turn an LED on. If the hardware changes to invert one or the other, then the person that is maintaining the code has to sift through the entire code base looking for all of the effects -- and Murphy pretty much guarantees that at least one will get missed. That is why the code is better presented as:

LED = (SWITCH_CLOSED == switch)? LED_ON : LED_OFF;

Now if the LED circuit is changed so that a 0 turns it on, all that is needed is to change a single #define statement at the top of the code to update the entire code base.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
C:
#include<REGX51.h>
#define TRUE (1)
#define FALSE (0)
#define FOREVER (TRUE)
#define LED_ON        (1)
SWITCH_CLOSED    (1)

#define LED_OFF       (!LED_OFF)
#define SWITCH_OPEN (!SWITCH_CLOSED)


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


      led = switch;
   }
}
.
What is the meaning of this line SWITCH_CLOSED (1)
are you defining or are you declaring variable . what are you doing. because when I tried to run program it give error
 

WBahn

Joined Mar 31, 2012
32,882
What is the meaning of this line SWITCH_CLOSED (1)
are you defining or are you declaring variable . what are you doing. because when I tried to run program it give error
It's a typo -- sorry for the confusion. It's supposed to be a #define directive.

The idea is that you define what SWITCH_CLOSED and LED_ON are and then, further down, you define their opposites in terms of these definitions. That way, if later you change the circuitry so that it requires a 0 to turn on the LED, you only change the #define for LED_ON to be (0) and the definition of LED_OFF automatically changes to be (1) as a consequence.
 

Thread Starter

Parth786

Joined Jun 19, 2017
642
It's a typo -- sorry for the confusion. It's supposed to be a #define directive.

The idea is that you define what SWITCH_CLOSED and LED_ON are and then, further down, you define their opposites in terms of these definitions. That way, if later you change the circuitry so that it requires a 0 to turn on the LED, you only change the #define for LED_ON to be (0) and the definition of LED_OFF automatically changes to be (1) as a consequence.
OK Now I understood. can you suggest me some further Exercise.
 

MrChips

Joined Oct 2, 2009
34,829
Here is your next exercise.

You have one push-button and one LED.
When you press the push-button, the LED turns ON. Release the push-button and there is no change to the status of the LED.
Press the push-button again and the LED turns OFF.

Repeat forever.

I suggest that you start a new thread with the title:

Toggle LED with push-button
 

WBahn

Joined Mar 31, 2012
32,882
I second both of MrChips' suggestions. Either that or do the problem with two switches each controlling a separate LED. In either case, the exercise after that might be two switches/LEDs behaving as MrChips described.
 
Top