Difference between revisions of "Exercise - Using Macros"

From Flowcode Help
Jump to navigationJump to search
Line 7: Line 7:
  
  
The first section looks at using 'Component Macros' to create the core timing sequence. The second section looks at using it to turn on a lamp for ten seconds once a switch is pressed.
+
The first section looks at creating the core program, with the menu. The second section shows how to create the macro.
  
  
 
__TOC__
 
__TOC__
  
==The core timing sequence==
+
==The main sequence==
At the core of the program is a section that sets up the LCD to display the time.<br />
+
This part of the program uses three switches to select the frequency for the LED.<br />
The flowchart sequence for displaying the elapsed time will be:
+
The flowchart sequence will be:
::::* Initialize the LCD using the 'Start' macro.
+
::::* Read the state of the switches.
::::* Set the cursor position in the LCD, using the 'Cursor' macro.
+
::::* Use this state to determine the correct branch to take.
 +
::::* Set up the appropriate time delay for the chosen frequency.
 +
::::* Run the macro with that delay setting.
 +
::::* Loop back and check the switches again.
  
  
 
+
===Set up the 'main' flowchart===
 
 
===Set up the flowchart===
 
 
* Open Flowcode, and set up the new flowchart as described in [[Exercise - Creating Your First Flowchart|Creating Your First Flowchart]].
 
* Open Flowcode, and set up the new flowchart as described in [[Exercise - Creating Your First Flowchart|Creating Your First Flowchart]].
  

Revision as of 10:59, 25 June 2013

Macros are sections of code that are repeated a number of times within a program.
It is more efficient to use a macro than to create the code repeatedly every time it is needed.
For more information about macros, see What Is a Macro?.

This exercise shows how to use macros with a menu to create a simple LED flasher. The LED can be made to flash at different frequencies by pressing different switches.


The first section looks at creating the core program, with the menu. The second section shows how to create the macro.


The main sequence

This part of the program uses three switches to select the frequency for the LED.
The flowchart sequence will be:

  • Read the state of the switches.
  • Use this state to determine the correct branch to take.
  • Set up the appropriate time delay for the chosen frequency.
  • Run the macro with that delay setting.
  • Loop back and check the switches again.


Set up the 'main' flowchart


Create the core sequence

Add the LCD

  • Locate the 'LCD' inside the 'Outputs' toolbox in the Icons toolbar.
  • Hover over the image to the left of the 'LCD' label, and click on the down-arrow that appears.



Add the switch

  • Locate the 'Push Round Panel' switch in the 'Inputs' toolbox in the Icons toolbar.
  • Hover over the image to the left of the switch label, and click on the down-arrow that appears.
  • Click on the 'Add to system panel' option to select it.
  • Click on the image of the switch on the System panel to select it and drag it to a suitable position.
  • On the Panel Properties, click on the 'Unconnected' label, next to 'Connection'.
A pinout of the microcontroller appears.
  • Click on the rectangle that represents the pin RA1/AN1, to connect the switch to Port A, bit 1 of the microcontroller.

Add the LED

  • Locate the 'Single LED' in the 'Outputs' toolbox in the Icons toolbar.
  • Hover over the image to the left of the 'Single LED' label, and click on the down-arrow that appears.
  • Click on the 'Add to system panel' option to select it.
  • Select the LED on the System panel and drag it to a suitable position.
  • Look at the Panel Properties, the 'Connection' property shows that, by default, it is connected to Port A, bit 0.
Leave it like this.