/ src / chapter_2.md
chapter_2.md
  1  # Chapter 2 (How to use `aci` PART - 1)
  2  
  3  By the end of this chapter , you will be able to do the following :
  4  
  5  1. [Select a Board to use](#select-a-board)
  6  2. [Create a New sketch using `aci`](#create-a-new-sketch)
  7  3. [Edit the created sketch](#edit-a-sketch)
  8  <br><br>
  9  ## Select a Board 
 10  
 11  Open your terminal and type `aci`. 
 12  You will be greeted with this page 
 13  
 14  ![Homepage](images/homepage.png)
 15  
 16  <br><br>
 17  
 18  Press `Enter` to enter the Board Selection Menu : 
 19  
 20  ![Board Selection](images/board_selection.png)
 21  
 22  <br><br>
 23  
 24  Type the Board Name you want to use and press `Enter`. 
 25  
 26  Then a confirmation dialog box will open asking you to confirm the board you chose. 
 27  
 28  Check the following GIF to see it in action. 
 29  
 30  For example , let's use an Arduino Nano that is connected to my system , 
 31  
 32  ![board_selection_gif](images/recordings/board_selection.gif)
 33  
 34  <br><br>
 35  
 36  Once you have selected a board, you can see the Board name and FQBN (Full Qualified Board Name) just
 37  below the "Welcome to arduino-cli-interactive" sign like so 
 38  
 39  ![board name](images/board_name.png)
 40  
 41  <br><br>
 42  ## Create a New Sketch 
 43  
 44  A sketch is a file with the `.ino` extension in which you will write the code to upload
 45  to the board. 
 46  
 47  To create a new sketch , first navigate to the directory you want the sketch to be in , in the terminal , run the `aci` command , select a board and then use the 
 48  Down arrow key to move down to the next option which **Create a New Sketch** and press **Enter**. 
 49  
 50  This will then bring up an Input box where you can enter the name of your sketch. 
 51  
 52  ![new_sketch_gif](images/recordings/new_sketch.gif)
 53  
 54  Congrats , you have created a new sketch using the command line. 
 55  
 56  <br><br>
 57  ## Edit a Sketch 
 58  
 59  After creating a new sketch , let us add the code to be uploaded to the board. 
 60  
 61  We will create a basic LED Blinking program to blink the inbuilt LED of the board every 1 second. 
 62  
 63  Before we get into editing the sketch , copy the below program :
 64  
 65  ```c 
 66  const int ledPin = 13;    //Depends on the board , for Uno and Nano it is pin 13
 67  
 68  void setup() {
 69    //This function will run only once when the board boots up 
 70    pinMode(ledPin , OUTPUT);  //Setting the ledPin as OUTPUT
 71  }
 72  
 73  void loop() {
 74    //This function runs infinitely until it is stopped
 75    digitalWrite(ledPin , HIGH);   //Turn ON the LED
 76    delay(1000);     //1 second delay
 77    digitalWrite(ledPin , LOW) ;  //Turn OFF the LED 
 78    delay(1000);     //1 second delay
 79  }
 80  ```
 81  
 82  Now , let us go to the terminal where `aci` is already running and then open the sketch `MyFirstSketch` , 
 83  we created previously. 
 84  
 85  Make sure you have selected your [board](#select-a-board) and the [sketch](#create-a-new-sketch) 
 86  has been created.
 87  
 88  Now navigate to the **Edit the Sketch** option using the Down arrow key and press **Enter**. 
 89  
 90  This will bring up a file picker throuh which you can navigate using the arrow keys to find the folder
 91  in which the sketch is present. 
 92  
 93  Once you find the folder , press **Enter** , you can then see the `.ino` file in the file picker. 
 94  
 95  Press **Enter** again and you will see a choice picker to pick your preferred editor. I will be using
 96  **NeoVim** as my main editor. You can choose whatever editor feels comfortable to you. 
 97  
 98  #### **__NOTE__** :
 99  Make sure you have any one of the following editors installed : 
100  
101  1. [NeoVim](./chapter_1.md#neovim)
102  2. [Vim](./chapter_1.md#vim)
103  3. Vi
104  4. Nano  
105  5. [Micro](./chapter_1.md#micro)
106  6. [Visual Studio Code](./chapter_1.md#vscode)
107  7. [VSCodium](./chapter_1.md#vscodium)
108  8. [Helix](./chapter_1.md#hx)
109  
110  Select the editor of your choice. 
111  
112  You will now be able to edit the file. 
113  If you are opening the file for the first time , you will be greeted with this code 
114  
115  ```c
116  void setup(){
117    //Run only once when the board boots up
118  }
119  
120  void loop(){
121    //Run infinitely until stopped
122  }
123  ```
124  
125  This is the default boilerplate code of any Arduino file. 
126  
127  There are 2 functions `setup()` and `loop()`. 
128  
129  `setup()` runs only once. It runs when the board is switched ON.
130  `loop()` run infnitely until it is stopped by switching OFF the board or by an interrupt. 
131  
132  In the [example code](#edit-a-sketch) that was given , we can see that we have defined the constant
133  `ledPin` as an Output in the `setup()` function and toggled the LED in the `loop()` function.
134  
135  You can paste the [example code](#edit-a-sketch) by deleting the default contents of the file and then 
136  pasting the given code. 
137  
138  Then you can save the code and exit the editor.
139  
140  You will then be asked if you want to preview the contentns of your file to check for any kind of mistakes. 
141  
142  Choose **YES** or **NO** by navigating between them using arrow keys. 
143  
144  To quit the preview , press `q` on your keyboard. 
145  
146  ![edit_sketch_gif](images/recordings/edit_sketch.gif)
147  
148  <br><br>
149  #### **__NOTE__** :
150  
151  Each editor has different shortcuts to save and exit a file 
152  
153  1. Vi/Vim/NeoVim/Helix : `:wq`
154  2. Nano : `Ctrl + S , Ctrl + X`
155  3. Micro : `Ctrl + S , Ctrl + Q`
156  4. Visual Studio Code/VSCodium : `Ctrl + S , Alt + F4`
157  
158