Difference between revisions of "Exercise - Using Macros"

From Flowcode Help
Jump to navigationJump to search
Line 81: Line 81:
 
::* Click on 'OK'.
 
::* Click on 'OK'.
 
:: The 'Switch-case' dialogue box is shown opposite.
 
:: The 'Switch-case' dialogue box is shown opposite.
:* Inside each of the three branches ('=1', '=2' and '=3'), click and drag a macro icon, and a calculation icon.  
+
:* Inside each of the three branches ('=1', '=2' and '=3'), click and drag a macro icon, [[File:Btn Macro.gif|30px]] and a calculation icon, [[File:Btn Calculation.gif|30px]].
 +
::* In the '=1' branch, double click on the 'Calculation' icon, and rename it "One hertz delay".
 +
::* In the 'Calculations:' box, type "delay = 500".
 +
::* In the '=2' branch, double click on the 'Calculation' icon, and rename it "Two hertz delay".
 +
::* In the 'Calculations:' box, type "delay = 250".
 +
::* In the '=4' branch, double click on the 'Calculation' icon, and rename it "Four hertz delay".
 +
::* In the 'Calculations:' box, type "delay = 125".
 +
The three 'Calculation' dialogue boxes are shown below.
 +
<Gallery widths=120px>
 +
File:gen_exerciseUsingMacros_delay1_01.png
 +
File:gen_exerciseUsingMacros_delay2_01.png
 +
File:gen_exerciseUsingMacros_delay4_01.png
 +
</gallery>
  
  

Revision as of 18:31, 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.

Linking frequency and delay

The flowchart controlling the LED flashes is shown opposite.
The table that follows shows the link between length of delay in each 'Delay' icon, and the frequency of flashing. The LED is switched on for one delay period, and then off for one delay period.


LED frequency and delay
Delay in milliseconds Frequency
1000 0.5
500 1
250 2
125 4




Create the variables

The program will use two variables, 'selection', to store the state of the switches used to select frequency, and 'delay', used to determine the frequency at which the LED flashes.

  • In the 'Edit' menu, on the main toolbar, click on 'Variables...' to open the Variable Manager dialogue box.
  • Hover just to the left of the 'Variables' label, and click on the down arrow that appears.
  • Select 'Add new' and the 'Create a New Variable' dialogue box opens.
  • Name the first new variable "selection".
  • Leave the variable type as 'Byte'.
  • Click on 'OK'.
  • Now do the same thing again to add the second variable "delay".

Set up the 'main' flowchart

  • Click and drag an infinite loop, Btn Loop.gif between the 'BEGIN' and 'END' icons.
Inside the loop:
  • Click and drag an input icon Btn Input.gif.
  • Double click on it to open the dialogue box.
  • Rename it "Read the switches" and set it up to input from the entire Port B.
  • In the 'Variable:' box, type "selection"
The three switches will be connected to bits 0, 1 and 2 of Port B.
The state of these switches will be stored in the variable 'selection'.
  • Click on 'OK'.
The input dialogue box is shown opposite.
  • Click and drag a 'Switch-case' icon, Btn Switch.gif after the 'Input' icon.
  • Double click on it to open the dialogue box.
  • Leave the 'Display name:' as 'Switch'.
  • In the 'Switch:' box, type the name of the variable that will trigger the branching, "selection".
  • Tick the first three 'Case:' boxes, and change the content of the third one from '3' to '4'.
  • Click on 'OK'.
The 'Switch-case' dialogue box is shown opposite.
  • Inside each of the three branches ('=1', '=2' and '=3'), click and drag a macro icon, Btn Macro.gif and a calculation icon, Btn Calculation.gif.
  • In the '=1' branch, double click on the 'Calculation' icon, and rename it "One hertz delay".
  • In the 'Calculations:' box, type "delay = 500".
  • In the '=2' branch, double click on the 'Calculation' icon, and rename it "Two hertz delay".
  • In the 'Calculations:' box, type "delay = 250".
  • In the '=4' branch, double click on the 'Calculation' icon, and rename it "Four hertz delay".
  • In the 'Calculations:' box, type "delay = 125".

The three 'Calculation' dialogue boxes are shown below.


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.