﻿ Variable Block

# 變數指令Variable Block

Think of a variable as a place to store a value in the NXT’s built-in memory. Other programming blocks can read the variable’s current value (and even change it) by connecting to the block with data wires.

## 建立變數Creating a Variable

1. 首先要從編輯選單中,使用"定義變數"功能,在對話框中,取一個簡短易懂的名稱並設定變數的資料型態(也就是文字,數字,或是邏輯).
To create a variable, you must first choose the Define Variable command in the Edit menu of the software. In the dialog window, give your new variable a short, easy-to-understand name and set the type of data that the variable will hold (i.e., text, number, or logic).
2. 接著拖曳變數指令到程式中,並從列表中選取變數.
Then drag a Variable block into your program and select the variable’s name from the list.
3. 針對變數指定"讀取"或是"寫入"功能,若要寫入變數時,就需要使用輸入資料線來傳遞新的數字,並覆寫該變數的舊數值.
Choose to either “read” from or “write” to the variable. Choosing to write to a variable will let you change the variable’s value over time by overwriting its current value with new information. To do this, you must wire an input data wire to the block’s data hub that will deliver the new information.
4. 在輸入區中可以設定變數的初始值,若是邏輯資料型態可用單選按鈕來指定.
You can give the variable a starting constant value by typing in the input box (or by using the radio buttons if your variable’s data type is set to logic).

Note: if you need to change the data type or name of your variable, go back to the Define Variable command of the Edit menu and change the setting there.

## 使用變數Using a Variable

A variable’s value can change over time. You might create a variable called “Recorded Light Level” and give it an initial value of 50. Then as your robot (fitted with a light sensor) drives to a certain location, you can use a Variable block (set to “write”) to record the current light level at the location. To do this, you would connect an output data wire from the Light Sensor block to the input plug of the Variable block.

Later in your program you can use a Variable block (set to “read”) to read the light value recorded at the location. Other blocks connected to the block’s output plug by data wire would be able to access the information saved in the variable (i.e., the recorded light level).

1. 當變數指令放在工作區上時,資料集線器會自動打開.要讀取變數值時,則要從輸出埠連接資料線,若是更改變數值時,就要從輸入端連接資料線.(參考下列資料集線器說明).
The block’s data hub will open automatically when the block is placed in the work area. A data wire must be connected to this block’s output plug for other blocks to read the variable’s current value. To write new information to the variable, you must also connect an input data wire to the block’s data hub. (See the Data Hub section below for more information.)

## 設定變數指令Configuring the Variable Block

1. 從編輯選單的"定義變數"功能中,產生的變數都會列在本表.
In the list, select the variable that you created using the Define Variable command of the Edit menu.
2. 選擇讀取或是寫入變數.
Choose to either read the variable’s current value or write new information to the variable.
3. 給予變數特定值,可以輸入文字或數字,或是用單選按鍵指定邏輯資料.記住,若有資料線連接到變數指令,且該變數指令為讀取時,從資料線傳來的新資料會覆蓋之前給予的數值.
To give your variable a constant value, either type a number or text into the input box, or use the radio buttons if the variable is of the logic data type. Remember that if an input data wire is connected to the block and the Variable block is set to “read,” new information coming in dynamically through the data wire will take precedence over any constant value.

Note: if you need to change the data type or name of your variable, go back to the Define Variable command of the Edit menu and change the setting there.

## 程式與指令的資料共享Sharing Information between a Program and a My Block

In order to share data between your main program and a My Block (i.e., between two .rbt files), do these two things:

1. 在主程式跟我的指令中,定義相同名稱與資料型態的變數.
Define a variable with the same name and data type in both the main program and in the My Block.
2. 在主程式跟我的指令中使用變數指令.
Use that variable in a Variable block in the main program and in the My Block.

Any place that the variable is used will read/write to the same memory location on the NXT.

## 設定變數指令的資料集線器Configuring the Variable block’s Data Hub

You can control the Variable block dynamically by connecting data wires (from other blocks’ data hubs) to the Variable block’s data hub.

Open a block’s data hub by clicking the tab at the lower left edge of the block after it has been placed on the work area.

Data wires carrying input information to a block are connected to the plugs on the left side of its data hub. Data wires carrying output information are connected to the plugs on the right side.

[A]輸入連接埠 [A] Input plug
[B]輸出連結部 [B] Output plug
[C]數字資料線(黃) [C] Number data wire (yellow)
[D]邏輯資料線(綠) [D]Logic data wire (green)
[E]文字資料線(橘) [E]Text data wire (orange)
[F]無效資料線(灰) [F]Broken data wire (gray)

### 資料由輸入端傳至傳出端Passing from the input plug to the output plug

If an input plug has a corresponding output plug (see A above), the input data will pass through from the input plug to the output plug without being changed. In this case, you can only use the output plug if the input plug is connected to an input data wire; connecting an output data wire to such an output plug without a connected input data wire will cause the output data wire to be “broken” (and colored gray).

### 特定的資料型態Data wire colors carry specific types of data

Each data wire carries a specific type of data between blocks. For example, if a data wire is dragged from a logic plug on a block’s data hub, it can only be connected to a logic plug on another block’s data hub. The chart below shows what kind of data each plug can accept or send out.

### 資料線顏色Data wire colors

Data wires are identified with specific colors: wires carrying number data are colored yellow, wires carrying logic data are colored green, and wires carrying text data are colored orange.

### 無效的資料線“Broken” data wires

If you try to connect a data wire to a plug of the wrong data type, the data wire will be broken (and colored gray). You will not be able to download your program if a data wire is broken.

If you click a broken wire you can read why it is broken in the small help window in the lower right corner of the work area.

### 資料必須在連接埠的限制範圍內Data must bewithin the possible range of the plug

If an input data wire transmits a value outside the possible range of the plug it is connected to, the block will either ignore the value or change it to a value within its range. For plugs that allow just a few input values (example: just 0, 1, or 2), the plug will ignore the input if a value arrives outside its range.

>而對那些可以接收大量範圍輸入值(例如: 0~100)的連接埠來說,該連接埠就會將數值轉成可接受的範圍. 例如, 移動指令的動力連接埠接收到150的輸入值時,那麼移動指令就會轉成100(也就是動力連接埠的範圍內).
For plugs that accept larger input ranges (example: 0 – 100), the plug will force any input outside its range to fit. For example, if a Move block’s Power plug receives an input value of 150, the block will change the input value to 100 (i.e., a number within the Power plug’s range).

### 下表列出本資料集線器中各連接埠的特性This chart shows the different characteristics of the plugs on the Variable block’s data hub:

 連結埠Plug 資料型態Date Type 資料範圍Possible Range 資料意義What the Values Mean 連接埠例外說明This Plug is Ignored When... 數值*Value* 任意Any 讀取或寫入的數值Value to read or write

*此資料連接埠會隨著變數的資料型態而改變外觀.
*This data plug will change appearance based on the variable's data type.