Introduction: Starting Programming With a Flow Chart
Starting to use PIC micro controllers for your projects? they are very useful but very frustrating when your programme just doesn't work.
This is one way to sort out your ideas by drawing a flow chart. This is the way professional programmers often create their masterpieces.
Starting with a simple pencil and paper to get their ideas into some form of order.
This is particularly valuable when the system you are defining is a process that moves step by step.
A good example of the would be programming an automatic washing machine or a robot. Of course for very simple programme you wouldn't need to do this.
Step 1: The Symbols
For simple flow charting you only need to use 2 symbols.
A rectangle shows an ACTION example - turn say motor on or off, turn an LED on or off.
A diamond shows a DECISION - example - is the switch on, is the lid closed, has the robot touched anything.
Step 2: Using the Symbols
Your process should fit into a step by step series of actions, Do this, Then do that, Has this happened?
Is the washing machine lid closed?
Start filling the machine
Is the machine full
Stop filling the machine
This can be represented by using the symbol boxes and writing in them what the action or decision is.
You may need to swap things around or re order them until you can see that everything is in the right order and right place so it happens at the right time.
Step 3: More Complicated Things
A more complicated programme controlling say a robot or a washing machine will have many more steps.
Step 4: Turning the Flowchart Into a Programme
This is where the flow chart scores.
It is now possible to write the necessary programming commands next to each flowchart box.
First you need to sort out what input and output is connected to what real life item.
ALL systems can be described as the following 3 sections
INPUT - PROCESS - OUTPUT
The input section deals with the sensors e.g. switches, ultrasonic sensors, microphones etc.
The process section is the part that makes the decisions depending on what the input sensors say.
The output section translated the small electronic signals into larger voltages and currents to drive output devices e.g. Motors, LEDs, Lamps, Speakers, etc.
This Input output table (I/O table) has 4 outputs and 1 input and would be used to control a small robot. So turning output 0 on will make the right motor go forward, turning output 0 off will stop the right motor.
Step 5: Adding to the Flow Chart
This table is easily applied to the flow chart.
Where there is an action this will normally be turn something on or off or wait for a while to allow the action to complete.
Where there is a decision normally you will be checking the inputs for any activity.
In most forms of PIC programming this will be by asking "If input x is on then do this.."
These command can be applied to the flow chart using the I/O table as below
Step 6: Turning It Into the Programme
Now we are in a good position to get a programme that will work mostly as we intend it to.
The commands can now be written into a suitable form for the programming language you are using.
I generally us the PICAXE microprocessor system http://www.picaxe.com this is programmed in a form of BASIC which I find easier to use for most applications.
The programme would now be written like this - I have commented this so you can see what each line does.and how it related to the flow chart.:
Start: ' this is a label so we can jump around the programme if we need to.
High 0 ' turns output 0 on
high 1 ' turns output 1 on
check: another label
If pin 3=1 then turn when input 3 is on jump to label turn
goto check ' if input 3 is not on then keep checking until it is.
low 0 ' turn output 0 off
low 1 ' turn output 1 off
high 2 turn output 2 on
high 4 ' turn output 4 on
wait 2 ' wait for 2 seconds whilst the robot backs up a bit.
goto start ' return to the start to move forward again.
Step 7: A Real PAIN
This all looks very long winded when all you want to do is get your robot/washing machine/wigit working. I agree, although this has taken me much longer to write than actually it would take to do and it is worth the effort.
1. you will find with complex programmes it is hard to get things in the right order.
2. You miss things (it's complicated)
3. Paper is cheap and your time may not be - believe me this is quicker in the long run for anything more complicated than turning an LED on and off.
4. Frustration is the killer for learning new skills, Nothing is worse than building something with electronics and it won't work, you don't know why or where to start. MUCH better to have a good chance to say well the programme should be working it must be the hardware.
Give it a try you may be surprised with the clarity of thought it give you.
We have a be nice policy.
Please be positive and constructive.