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.
Select a file under Code in Explorer to open it in the Arduino Code editor. The markers correspond to the descriptions below.
Selecting a file displays its contents and updates the selected item in Explorer.
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.
| 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. |
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.
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.
int limitValue(int value) { return constrain(value, 0, 100); // Limit the value to the range 0 to 100. }
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..ino files in one project are built together as one sketch, so a function in one file can be called from another.MPINO-8A4R(T)-S project.
For the storage types of D0 and D1 and sharing PLC memory with Arduino Code, see Phase 2 · Understand Memory. To call an Arduino function from a ladder box function, see Calling an Arduino Code Function.
Caution: If ladderLoop() is removed from loop(), the ladder logic will not run.
As you type the beginning of a function or variable name, available items appear in the completion list.
Ctrl + Space.↑ or ↓.Enter or Tab.
In the preceding example, typing limitV lets you select limitValue() from the other code file.
| 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.
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.
F12.| 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.
Arduino Code and Ladder Logic use a find-and-replace interface suited to the active editor.
| 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.
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 |
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.
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 Phase 1 · Create Your First Project.
Shortcuts can be changed under File → Preferences → 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.
| 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. |