Switch flowchart & coding [SOLVED]

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
I need expert's suggestion's to translate flowchart into pseudo code. When a flowchart is ready how do you write pseudo code for it?

I would show you my work I have created flowchart and written code for it.

Project_Flowchart_Switch.jpg

I have written below code for flowchart

C:
//Program in c language
#include<header.h>   

#define Power_Button  Port1_Pin1

#define SW1           Port1_Pin2
#define SW2           Port1_Pin3
#define SW3           Port1_Pin4

#define Light1        Port2_Pin1
#define Light2        Port2_Pin2
#define Light3        Port2_Pin3
#define Green_Light   Port2_Pin4

#define  True         1
#define  On           1

void main ()

{
    while ( True )
    {
            while( Power_Button == True )  // check power button
                  Green_Light = On;
      
            if ( SW1 == True )       // if SW1 button pressed, Turn on Light1
                Light1 = On
          
            if ( SW2 == True )      // if SW2 button pressed, Turn on Light2
                Light2 = On
          
            if ( SW3 == True )      //// if SW3 button pressed, Turn on Light3
                Light3 = On
        }
    }
      
}
Does the code I have written below match with the flowchart. I would highly apricate any suggestion's to translate flowchart into pseudo code ?
 

click_here

Joined Sep 22, 2020
548
This...
Code:
while( Power_Button == True )  // check power button
    Green_Light = On;
Should be...
Code:
while( Power_Button == False )  // check power button
    continue;

Green_Light = On;
i.e. The Green Light does not turn on until power button is not false
 

click_here

Joined Sep 22, 2020
548
To do the pseudocode you would just write out line by line what the code is doing. However unless someone has specifically requested you to do it, it is redundant now that you have written the actual code.
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
To do the pseudocode you would just write out line by line what the code is doing. However unless someone has specifically requested you to do it, it is redundant now that you have written the actual code.
My main objective is that a when flowchart is ready so how to convert it into code as I already shown you. forget about the logic problem of the flowchart for a while. For now just forget when to turn off the light. I have a clear idea of when to turn off light. For this I will make another flowchart.

But for now my question is does code in #1 match with flowchart?

Should only one button (one LED) be active at a time?
Each button turns on its relative light. As in our homes, the button for the bed room light is fixed, the button for the hall light is fixed.
 
Last edited:

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
hi Pushkar,
Again there are lots of good sites via Google
Thank you for listing from google. My problem is not finding tool to draw flow chart. I use online tools to create flowcharts. https://app.diagrams.net/

I see mostly tools is available for 30 days free trial. After that we have to purchase

The last one in this list is a free chart maker program
yes i have used it before but as far as i remember it was also free for 30 days. That's why I use online tools to draw flowchart
 
Last edited:

MrChips

Joined Oct 2, 2009
30,720
Flowchart and pseudocode serve the same purpose. One usually does not do both. One does one or the other.
If you want to practise by doing both the pseudocode should be an exact transcription of what is in the flowchart.
 

BobaMosfet

Joined Jul 1, 2009
2,110
I need expert's suggestion's to translate flowchart into pseudo code. When a flowchart is ready how do you write pseudo code for it?

I would show you my work I have created flowchart and written code for it.

View attachment 246354

I have written below code for flowchart

C:
//Program in c language
#include<header.h>

#define Power_Button  Port1_Pin1

#define SW1           Port1_Pin2
#define SW2           Port1_Pin3
#define SW3           Port1_Pin4

#define Light1        Port2_Pin1
#define Light2        Port2_Pin2
#define Light3        Port2_Pin3
#define Green_Light   Port2_Pin4

#define  True         1
#define  On           1

void main ()

{
    while ( True )
    {
            while( Power_Button == True )  // check power button
                  Green_Light = On;
   
            if ( SW1 == True )       // if SW1 button pressed, Turn on Light1
                Light1 = On
       
            if ( SW2 == True )      // if SW2 button pressed, Turn on Light2
                Light2 = On
       
            if ( SW3 == True )      //// if SW3 button pressed, Turn on Light3
                Light3 = On
        }
    }
   
}
Does the code I have written below match with the flowchart. I would highly apricate any suggestion's to translate flowchart into pseudo code ?
Be aware, that a flowchart does not always exactly match code- A flow-chart is a LOGIC diagram to help you understand and test what the code DOES at a high level. It is also a form of documentation and reference. Many companies require programmers to create flow-charts for every function they write so the company has documentation for a variety of reasons about the code. However, the flow-chart doesn't necessarily exactly/literally match the code's functionality at a line-for-line basis (that can be too tedious sometimes for what you need).

Think about how you throw a ball- If you create a flow-chart about how you grab a tennis ball off a table, raise it, and throw it at a target 20 feet away from you, there is a LOGICAL process that you go through. That is what the flowchart contains. How your brain controls your nerves, your muscles, how your joints work, and all of that- may not be in that flow-chart because that is not the process you're trying to retain/test. But it can be- it's a matter of gauging how much detail you want in your flow-chart.

Unless it's really complicated, or required for legal, I generally work with what is reasonable, rather than minutae. IMHO
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
Flowchart and pseudocode serve the same purpose. One usually does not do both. One does one or the other.
If you want to practise by doing both the pseudocode should be an exact transcription of what is in the flowchart.
Why do you think they both have same purpose?

I don't think they both have the same purpose. I think pseudo contains more information. For example look at my code for flowchart. I am defining variables name and using operators in pseudo code.

I think for any beginner they should first make flowchart and then try to write the pseudo code and then try to write complete code. After that complete code should be test by debugging
 

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
The code and flow chart never turn off any lights.
Just wait for a while, I will soon make a new flowchart for you in which the light will be off

Edit - @MrSalts Does it make sense? This flowchart is different then previous.

Project_Flowchart_Switch2.jpg

Edit 2
The code and flow chart never turn off any lights.
The flow chart doesn't turn off the lights so there's no code to turn off lightt.
 
Last edited:

click_here

Joined Sep 22, 2020
548
But for now my question is does code in #1 match with flowchart?
It does not.

And the reason is what I posted in #3 (I'm guessing that you accidently missed that post?)
The Green Light does not turn on until power button is not false
If you look at the flow diagram, the power light does not turn on until the power button is no longer false. However, your code does not do that.

i.e. Your question had already been answered.
 
Last edited:

Thread Starter

Pushkar1

Joined Apr 5, 2021
416
@click_here

I have uploaded revised flowchart in #12. Does the code given below follow logic in flowchart in #12
C:
#include<header.h>

#define Hall_Switch          Port1_Pin1
#define Kitchen_Switch       Port1_Pin2
#define Bedroom_Switch       Port1_Pin3

#define Hall_Light      Port2_Pin1
#define Kitchen_Light   Port2_Pin2
#define Bedroom_Light  Port2_Pin3

#define Green_Light         Port2_Pin4

#define  Open          0
#define  Close         1

#define   OFF          0
#define   ON           1

void main ()

{
     Hall_Switch     = Open ;
     Kitchen_Switch  = Open ;
     Bedroom_Switch  = Open;

     Hall_Light     =  OFF ;
     Kitchen_Light  =  OFF ;
     Bedroom_Light  =  OFF;

     Green_Light = ON

     while ( 1 )
    {

            if ( Hall_Switch == Close )
                 Hall_Light = ON;
            else
                  Hall_Light = OFF;
                 
            if ( Kitchen_Switch == Close)
                Kitchen_Light = ON;
            else
                Kitchen_Light = OFF;
   
            if (Bedroom_Switch == Close)
                Bedroom_Light = ON;
            else
                Bedroom_Light = OFF;
    }

}
Edit : correction done
 
Last edited:

click_here

Joined Sep 22, 2020
548
@click_here

I have uploaded revised flowchart in #12. Does the code given below follow logic in flowchart in #12
C:
#include<header.h>

#define Hall_Switch          Port1_Pin1
#define Kitchen_Switch       Port1_Pin2
#define Bedroom_Switch       Port1_Pin3

#define Hall_Light      Port2_Pin1
#define Kitchen_Light   Port2_Pin2
#define Bedroom_Light  Port2_Pin3

#define Green_Light         Port2_Pin4

#define  Open          0
#define  Close         1

#define   OFF          0
#define   ON           1

void main ()

{
     Hall_Switch     = Open ;
     Kitchen_Switch  = Open ;
     Bedroom_Switch  = Open;

     Hall_Light     =  OFF ;
     Kitchen_Light  =  OFF ;
     Bedroom_Light  =  OFF;

     Green_Light = ON

     while ( 1 )
    {

            if ( Hall_Switch == Close )
                 Hall_Light = ON;
            else
                  Hall_Light = OFF;
                  
            if ( Kitchen_Switch == Close)
                Kitchen_Light = ON;
            else
                Kitchen_Light = OFF;
    
            if (Bedroom_Switch == Close)
                Bedroom_Light = ON;
            else
                Bedroom_Light = OFF;
        }
    }

}
Assuming that all the defines are correct, it does.

However, you do have too many closing parenthesis at the end, so it won't compile.
 
Top