Exercise - Inserting Code Into Flowcode
<sidebar>Sidebar: What Is a Macro?</sidebar>
C code is a high-level programming language widely used in industry.
It has spawned a number of other programming languages, such as C#, C++, Java and Python.
Flowcode programs are first compiled into C before being crunched down eventually into hex code.
There are two mechanisms for adding your own C code to a Flowcode flowchart:
- using the 'C Code' icon ;
- using the 'Use supplementary code' facility within 'Project Options', shown opposite:
Contents
Why would you want to?
- Flowcode programs are compiled to 'C', but produce many lines of code.
- They do so because they must be able to cope with any function or structure that the designer wishes to use.
- Experienced C programmers may wish to use code more efficiently, and so insert a few lines of code to replace many lines which Flowcode would generate. (This usually applies to the 'Adding supplementary code' option.)
- Experienced programmers can insert a short section of C code in order to reduce the length, or complexity, of the Flowcode program.
- Students learning to program in C can test short sections of code by inserting them into a Flowcode program. This avoids having to write the full program in C.
Adding supplementary code
This feature is used when you have blocks of code containing routines, definitions, lookup tables, etc.
Definitions and function declarations:
This is the first section. It allows users to initialise the program, by adding structures such as 'defines', 'includes', and other function declarations. This section must be placed at the beginning of the C file so that the code is 'visible' to all parts of the program.
Function implementations:
This second section allows for the addition of the main function code.
Structured thus, the code is accessible to any Flowcode macro, and vice-versa.
Warning
Be sure that the added C code is correct!
Flowcode does not simulate C Code, so programs that make use of supplementary code may not simulate correctly. Where Flowcode fails to compile, it may be that the supplementary code contains errors.
Using the 'Code' icon
- Open Flowcode.
- On the Startup screen, click on 'New project' to create a new flowchart.
- Add the icons shown in the flowchart:
Heading
Heading
The flowchart sequence
The flowchart sequence:
- Check if switch 1 is pressed.
- If it isn't, make the red LED flash slowly.
The main program
- Start a new Flowcode flowchart, using the default microcontroller.
- Make sure that the System Panel is visible. If necessary, click on View and then select 'System Panel' a check-box will appear next to the option when enabled.
- Drag and drop a Loop icon between the BEGIN and END icons.
- Inside the loop, drag and drop an Input icon from the Icons toolbar.
- Drag and drop a Decision icon after the 'Input' icon.
Next, add icons to control what happens when switch is not pressed, (and so the program follows the 'No' branch