====== MPINO STUDIO 2 Phase 5 · Editing Arduino Code ======
This page explains how to create and manage Arduino Code files, write code, use completion and Go to Definition, find and replace text, and locate code errors.
{{ :en-us_products:mpino_studio2:code-editor-learning-path.svg?1000 |Arduino Code workflow from choosing a file to checking problems }}
===== 1. Arduino Code Editor =====
Select a file under **Code** in Explorer to open it in the Arduino Code editor. The markers correspond to the descriptions below.
{{ :en-us_products:mpino_studio2:code-editor-marked.svg?1000 |Arduino Code editor marked with the Code group, line numbers and folding, editing area, and minimap }}
* **1** Code group: Lists the Arduino Code files in the project.
* **2** Line numbers and folding: Shows code locations and folds or expands functions and blocks.
* **3** Editing area: Used to write and edit Arduino C/C++ code.
* **4** Minimap: Shows the file structure and the currently visible area.
Selecting a file displays its contents and updates the selected item in Explorer.
===== 2. Create and Manage Code Files =====
==== Primary Code File ====
The first code file in a new project is named ''main''. When the project is saved for the first time, its name changes automatically to match the project file name.
The first code file is the project entry file. It stays at the top of the Code list and cannot be renamed, deleted, or moved directly.
==== Code File Operations ====
^ Action ^ Mouse ^ Keyboard ^ Result ^
| Add | Use ''+'' next to **Code**, or **Add** on the context menu for the Code group or a file | - | Adds and opens a new ''.ino'' file at the end of the list. |
| Rename | Double-click a file, or choose **Rename** from its context menu | ''F2'' or ''Enter'' | Renames an additional code file. |
| Delete | Use the file's ''×'', or choose **Delete** from its context menu | ''Delete'' | Deletes the file after confirmation. |
| Move in list | Drag a file up or down | ''Ctrl + ↑'' or ''Ctrl + ↓'' | Moves the selected additional file one position in Explorer. |
{{ :en-us_products:mpino_studio2:code-files-marked.svg?1000 |Code file controls marked with add, primary file, additional file, and rename or delete menu }}
Do not type the extension in a code file name. A name cannot be empty, start with a period, contain ''/''ㆍ''\''ㆍ''..'', or differ from an existing name only by letter case.
Moving an additional file updates the current working list. When the project is opened again, additional files are sorted by name.
===== 3. Write Code and Use Functions Across Files =====
Separating code by function makes a project easier to navigate and maintain. In this example, a function that limits a value is written in ''control'' and called from the primary code file.
==== ''control'' file ====
int limitValue(int value) {
return constrain(value, 0, 100); // Limit the value to the range 0 to 100.
}
==== Primary code file ====
void setup() {
}
void loop() {
D0 = limitValue(D1); // Limit D1 to 0 through 100 and store it in D0.
ladderLoop(); // Run the ladder logic.
}
* ''limitValue()'' uses the Arduino function ''constrain()'' to return its argument within the range 0 through 100.
* Multiple ''.ino'' files in one project are built together as one sketch, so a function in one file can be called from another.
* This example was compiled successfully in an ''MPINO-8A4R(T)-S'' project.
{{ :en-us_products:mpino_studio2:code-files-share.svg?1000 |limitValue function defined in control and called from the primary code file }}
For the storage types of ''D0'' and ''D1'' and sharing PLC memory with Arduino Code, see [[en-us_products:mpino_studio2:memory|Phase 2 · Understand Memory]]. To call an Arduino function from a ladder box function, see [[en-us_products:mpino_studio2:box_functions:free_input|Calling an Arduino Code Function]].
**Caution:** If ''ladderLoop()'' is removed from ''loop()'', the ladder logic will not run.
===== 4. Use Code Completion =====
As you type the beginning of a function or variable name, available items appear in the completion list.
- Type the beginning of a function or variable name in the code editor.
- If the list does not open, press ''Ctrl + Space''.
- Select an item with ''↑'' or ''↓''.
- Insert the selected item with ''Enter'' or ''Tab''.
In the preceding example, typing ''limitV'' lets you select ''limitValue()'' from the other code file.
{{ :en-us_products:mpino_studio2:code-autocomplete.svg?1000 |limitV in the editor and the limitValue completion item }}
^ Completion source ^ Example ^ What it contains ^
| Project functions and variables | ''limitValue()'' | Names written in the project's code files |
| Arduino functions and constants | ''constrain()'' and ''HIGH'' | Functions and constants provided by Arduino |
| Board core and libraries | Analyzed functions and constants | Items from the selected board core and project libraries |
Board-core and library items may appear after code analysis finishes. A completed name still needs the argument count and types required by its definition.
===== 5. Go to Definition =====
Place the cursor on a function or variable name and press ''F12'' to open its definition.
^ Definition location ^ Result of ''F12'' ^
| Current code file | Moves to the line containing the definition. |
| Another code file | Opens that file and moves to the definition. |
| Arduino core or library | Opens a read-only definition view. |
Pressing ''F12'' on ''limitValue()'' in the primary code file opens its definition in ''control''. Arduino-core functions such as ''constrain()'' open in a read-only definition view. Close that view with its close button or ''Esc''.
{{ :en-us_products:mpino_studio2:code-definition-marked.svg?1000 |Function call, definition opened with F12, and read-only state }}
* **1** Call site: The function whose definition you want to inspect.
* **2** Function definition: The declaration or implementation opened by ''F12''.
* **3** Read-only: Arduino-core and library definitions cannot be edited here.
^ Focused editor ^ ''F12'' action ^
| Arduino Code | Go to Definition |
| Ladder Logic | Create a falling-edge contact |
The same shortcut performs a different action according to the focused editor.
===== 6. Find and Replace =====
Arduino Code and Ladder Logic use a find-and-replace interface suited to the active editor.
==== Find and Replace in Arduino Code ====
^ Task ^ Control ^ Result ^
| Open Find | ''Ctrl + F'' | Finds text in the current code file. |
| Open Replace | ''Ctrl + H'' | Shows fields for the search and replacement text. |
| Previous or next result | ''Shift + Enter'' or ''Enter'', or the on-screen buttons | Moves among matches. |
| Replace | **Replace** or **Replace All** | Changes the selected match or all matches in the current file. |
Match Case, Match Whole Word, and Regular Expression options are available.
==== Find and Replace in Ladder Logic ====
Focus the ladder grid and press ''Ctrl + F''. Search the current ladder or all ladders for contact and coil addresses and code text in cells.
^ Mode ^ Search target ^ Replace ^
| General search | Addresses and cell code | Replace and Replace All are available |
| Whole word | Searching ''M1'' excludes ''M10'' | Finds complete addresses |
| Box-function search | Box-function command names | Finds exact command names; replacement is disabled |
{{ :en-us_products:mpino_studio2:find-replace-comparison.svg?1000 |Comparison of find and replace in Arduino Code and Ladder Logic }}
===== 7. Check Code Errors =====
When a new error or warning is added, you can inspect its message and location in the bottom **Problems** panel. If the panel is hidden, press ''Ctrl + J'' and select **Problems**.
- Read the badge on the Problems tab for the combined error and warning count.
- Read the severity and diagnostic message.
- Check the displayed file name and line and column numbers.
- Selecting a problem in the primary code file moves to that code location.
{{ :en-us_products:mpino_studio2:code-problems-marked.svg?1000 |Problems tab, error message, file and line location, and erroneous code line }}
* **1** Problems and count: The combined number of errors and warnings.
* **2** Error message: The diagnostic text used to identify the cause.
* **3** File, line, and column: The location where the problem was found.
* **4** Code location: The line opened by selecting the problem.
For an error in an additional code file, read its file name and line number in Problems, then open that file directly from Explorer.
For the full build output and rebuilding a project, see [[en-us_products:mpino_studio2:first_project|Phase 1 · Create Your First Project]].
===== 8. Arduino Code Editing Shortcuts =====
Shortcuts can be changed under **File → Preferences → Shortcuts**.
{{ :en-us_products:mpino_studio2:code-editor-shortcuts.svg?1000 |Main Arduino Code editing shortcuts }}
^ Task ^ Default shortcut ^ Where it applies ^
| Open completion | ''Ctrl + Space'' | Code editor |
| Go to definition | ''F12'' | Function or variable in code |
| Find | ''Ctrl + F'' | Code or ladder editor |
| Replace | ''Ctrl + H'' | Arduino Code editor |
| Show or hide bottom panel | ''Ctrl + J'' | Workspace |
| Rename code file | ''F2'' or ''Enter'' | Additional code file in Explorer |
| Delete code file | ''Delete'' | Additional code file in Explorer |
| Move code file | ''Ctrl + ↑'' or ''Ctrl + ↓'' | Additional code file in Explorer |
''F2'', ''F12'', ''Delete'', and ''Ctrl + ↑'' or ''Ctrl + ↓'' can perform other actions when focus is elsewhere. Select the code area or file in Explorer before using them.
===== 9. Troubleshooting =====
^ Symptom ^ What to check ^
| The completion list does not open. | Select the code editor, press ''Ctrl + Space'', and check the spelling of the name. |
| A function from another code file is absent from completion. | Check the function name and braces, make sure both files belong to the same project, and wait for code analysis to finish. |
| ''F12'' does not open a definition. | Place the cursor on the name, focus Arduino Code, and check the code for syntax errors. |
| The definition view cannot be edited. | Arduino-core and library definitions open read-only. |
| The find interface is not the one expected. | Check whether Arduino Code or Ladder Logic is currently focused. |
| The first code file cannot be changed. | The project manages the first code file, so it cannot be renamed, deleted, or moved. |
| A problem in an additional code file does not open its line. | Read the file name and line in Problems and open the file directly from Explorer. |
===== Related Documentation =====
* [[en-us_products:mpino_studio2:activity_bar|Phase 8 · Activity Bar and Side Panels]]
* [[en-us_products:mpino_studio2:menus_settings|Phase 7 · Menus and Preferences]]
* [[en-us_products:mpino_studio2:monitoring|Phase 6 · Serial and Ladder Monitoring]]
* [[en-us_products:mpino_studio2:first_project|Phase 1 · Create Your First Project]] — Build, upload, and build output
* [[en-us_products:mpino_studio2:memory|Phase 2 · Understand Memory]] — Share PLC memory with Arduino Code
* [[en-us_products:mpino_studio2:ladder_editing|Phase 3 · Ladder Editing Basics]] — Edit ladder cells, contacts, and coils
* [[en-us_products:mpino_studio2:box_functions|Phase 4 · Box Functions]] — Box functions and Arduino function calls
* [[en-us_products:mpino_studio2|MPINO STUDIO 2 User Manual]]