Jump to content

Exercise - Using Analogue Input Devices: Difference between revisions

From Flowcode Help
No edit summary
Replaced content with "For an excellent introduction guide, we recommend [https://www.flowcode.co.uk/education/ Introduction to microcontroller programming]"
Tag: Replaced
 
(18 intermediate revisions by 4 users not shown)
Line 1: Line 1:
The aim of this exercise is to demonstrate the use of analogue input devices within a Flowcode flowchart.
For an excellent introduction guide, we recommend [https://www.flowcode.co.uk/education/ Introduction to microcontroller programming]
 
 
Digital inputs are simpler to deal with, as they have a finite range of possible values.<br />
For example, a two bit digital input can have one of only four possible values - 00, 01, 10 or 11.<br />
Flowcode uses an 'Input' icon [[File:Btn Input.gif|30px]] to deal with digital inputs.
 
 
An analogue input, on the other hand can have any of an infinite number of possible values.<br />
As a result, it is more difficult to handle in Flowcode.
[[File:gen_exerciseUsingAnInp_ansyspan_01.png|300px|right]]
A component macro [[File:Btn Component Macro.gif|30px]] is used to input data from an analogue sensor.<br />
The data is then stored in a variable.
 
 
Component macros are sections of code that have been written to support the components included with Flowcode 6.<br />
They take all the effort out of using these components.
 
 
In this exercise, a warning lamp lights when the signal from an ADC dial device rises too far.
 
 
 
__TOC__
 
 
==The flowchart sequence==
The flowchart will:
::::* Read the value set on the input device, the ADC dial.
::::* Compare this with a set value, and:
::::: if greater than the set value, turn on a lamp;
::::: if less, then make sure that the lamp is off.
::::* Loop back to the beginning, and repeat the process.
 
 
==Create the 'input' variable==
:* 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 new variable "input".
::* Leave the variable type as 'Byte'.
::* Click on 'OK'.
::* The dialogue box is shown opposite.
 
 
==Create the flowchart==
* Click and drag an infinite loop, [[File:Btn Loop.gif|30px]] between the 'BEGIN' and 'END' icons.
 
 
[[File:gen_exerciseUsingAnInp_ancompmacprop_01.png|300px|right]]
* Inside the loop:
:* Click and drag a 'Component macro' icon.[[File:Btn_Component_Macro.gif|30px]]
::* Double click on it to open the dialogue box, so that you can configure it.
::: The program 'knows' which components you have added to the System or Dashboard panel, and modifies the list of available commands accordingly.
::: Under 'Components', the ADC dial will be listed, and underneath it the list of commands.
::* Scroll down the list and click on the 'GetByte' command.
::: This reads the output of the analogue input device - the ADC dial in this case, and stores it in the byte variable called 'input'.
::* Rename the 'Display name' as "Read the input".
::* Click on 'OK'.
::* Again, the dialogue box is shown opposite.
 
 
* Next, drag and drop a 'Decision' box, [[File:Btn Decision.gif|30px]] and double click on it to open the dialogue box.
[[File:gen_exerciseUsingAnInp_andecprop_01.png|300px|right]]
:* Rename it "Compare it !"
:* In the condition 'If' box, type "input>128".
:: The '128' is totally arbitrary in this program. A byte variable can store any value from 0 to 255, so 128 is roughly half way.
:: In this case, the program decides whether the value stored in the variable 'input' is greater or smaller than 128, and reacts differently depending on that decision.
:* Click on 'OK'.
::* Again, the resulting dialogue box is shown opposite.
 
 
[[File:gen_exerciseUsingAnInp_anoutprop0_01.png|300px|right]]
* In the 'Yes' branch, drag and drop an 'Output' icon, [[File:Btn Output.gif|30px]].
:* Double click on it to open the dialogue box.
:* Change the 'Display name:' to "Switch on the lamp".
:* In the 'Variable or value:' box, type "1".
:* Configure the rest of the box to output this to 'PORT A' 'Single Bit:' 0.
:* Click on 'OK'.
 
 
 
[[File:gen_exerciseUsingAnInp_anoutprop1_01.png|300px|right]]
* In the 'No' branch, drag and drop a second 'Output' icon.
:* Double click on it to open the dialogue box.
:* Change the 'Display name:' to "Switch off the lamp".
:* In the 'Variable or value:' box, type "0".
:* Configure the rest of the box to output this to 'PORT A' 'Single Bit:' 0.
:* Click on 'OK'.
::* Again, the dialogue boxes for the 'Output' icons are shown opposite.
 
 
 
 
The flowchart should now resemble the one shown below.
[[File:gen_exerciseUsingAnInp_anprog_01.png|300px|center]]
 
 
 
==Add the LED==
* Locate the 'Single LED' in the 'Outputs' toolbox in the [[Tools and Views#1) Icons Toolbar|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.
 
 
==Add the ADC dial==
The ADC dial represents a number of input devices.
* Locate the 'ADC dial' in the 'Inputs' toolbox in the [[Tools and Views#1) Icons Toolbar|Icons toolbar]].
:* Hover over the image to the left of the label, and click on the down-arrow that appears.
:* Click on the 'Add to system panel' option to select it.
:* Select the ADC dial on the System panel and drag it to a suitable position.
:* Look at the Panel Properties, and locate the 'Connection' property. Just below it, the 'Channel' property shows that, by default, it is connected to 'AN0' - the first bit of the microcontroller capable of accepting an analogue input. Click on 'AN0' to open a pinout of the microcontroller.
::* Click on the rectangle representing input 'AN1' to change the connection to this channel.
:: Leave it like this.
 
 
==Final testing==
* Save the flowchart as 'Analogue,fcf'.
* Click on the simulation button [[File:Btn Run.png|30px]].
* Use the mouse to turn the pointer on the ADC dial, and notice what happens when you pass the half-way mark.
* Click on the 'Stop simulation' button, [[File:Btn Stop.png|30px]] when you are happy with the program's behavior.

Latest revision as of 13:30, 25 April 2023

For an excellent introduction guide, we recommend Introduction to microcontroller programming