Synthesis of useful commands to code the Arduino

If you are new to Arduino, you may be interested in the list of commands that are useful for the most important operations. You need them to control the Arduino board and define simple logic maths.

The good thing about Arduino is that you don't really have to learn too many programming realities. Just invest in an Arduino board and install the IDE.

Most of the command examples are available in the Examples library. If you want to follow the existing reference examples, all you have to do is modify the code a bit and create your own sketches.

Synthesis of useful commands to code the Arduino Picture 1Synthesis of useful commands to code the Arduino Picture 1

The goal of this article is not to present a complete list of Arduino tricks, but to understand the logic behind the code. It is important to know how to apply them in all situations.

Arduino IDE toolbar 

Verify Scan code and report any errors Upload Compile the code and upload it to Arduino board via USB New Open a blank Arduino sketch (program written by Arduino IDE) Open Open list of saved sketches in file browser Save the current sketch. Serial Monitor Open Serial Monitor in a new window

Basic Arduino IDE commands

As soon as you open the Arduino IDE, you will be greeted by the command setup () and loop (). These are the only two sketch examples that you will find in almost all Arduino code.

Synthesis of useful commands to code the Arduino Picture 2Synthesis of useful commands to code the Arduino Picture 2

  1. Setup (): Every time the sketch starts, the setup command will help you initialize the variables and start using libraries. Refer to an example of a digital LED that blinks with no lag in the Arduino IDE installation.
void setup () {// set digital pin as output: pinMode (ledPin, OUTPUT); }
  1. Loop (): A loop follows setup and is truly the heart of the program, making it respond infinitely to any logic. For example, the code above is an example of an LED that blinks infinitely, without delay.
void loop () {// this is where you put the code to run at all times. // check if it's time to blink the LED .}

After familiarizing yourself with sketches, you need to know the control commands. The most important commands are:

  1. Break: If you want to exit a command, you need to press break.
if (sens> threshold) {// bail out on sensor detect x = 0; break; }
  1. If or else: Logical statements initiate an action each time a condition is satisfied. Again, let's go back to the example of a flashing digital LED. Remember the loop () command in which the code must run indefinitely.

Other useful control commands can provide certain logic. Commands you can use include:

  1. return: Returns a certain value.
  2. while: Another loop that occurs continuously under a certain condition. For example:
while (dist 
  1. goto: As the name implies, this command allows you to go to a certain line of code.

Boolean and arithmetic operators

Besides sketch and control commands, you must know some Booleans and arithmetic operators to command programs.

  1. Operands: Equal (=), addition (+), subtraction (-), multiplication (*), and division (/).
  2. Advanced operands: Not equal (! =), Less than or equal (<=), greater than or equal to (> =), remainder (%).

Important variables

In some cases, you need to give several variables to handle different logic operations. The important variables are:

  1. HIGH | LOW: This gives the high and low final values ​​for the constants.
// The variables will change: int ledState = LOW;
  1. LED_BUILTIN: Indicates the number of LED pins (LED pin). In the above example of LED flashing on Uno, the number of LED pins is 13.

Other important variables to remember include True / false, sizeof (), void, int, and string. They are just like any other regular program including Python, C ++, etc .

High-level functions

Finally, you need to know a few advanced functions to control the Arduino board. Advanced functions include:

Synthesis of useful commands to code the Arduino Picture 3Synthesis of useful commands to code the Arduino Picture 3

Built-in Arduino functions 

  1. digitalRead (): Reads the value from a certain digital pin. There is also digitalWrite ().
  2. pinMode (PIN_NUMBER, INPUT / OUTPUT): Sets the pin at position PIN_NUMBER to INPUT or OUTPUT.
  3. pinMode (PIN_NUMBER, INPUT_PULLUP): Sets the pin at position PIN_NUMBER to be input using the built-in pull-up resistor of the Arduino board.
  4. analogRead (PIN_NUMBER): Reads the analog pin number PIN_NUMBER and returns an integer between 0 and 1023.
  5. analogWrite (PIN_NUMBER, VALUE): Emulate analog output VALUE using PWM on PIN_NUMBER (Note: Only available on pins 3, 5, 6, 9, 10, and 11).
  6. analogReference (DEFAULT): Uses default reference voltage (5V or 3.3V depending on board voltage).
  7. analogReference (INTERNAL): Use internal reference voltage (1.1v for ATmega168 / 328p, 2.56 for ATmega 32U4 / 8).
  8. analogReference (EXTERNAL): Uses voltage applied to AREF pin as reference voltage (Note: 0-5V only).

Time-related functions

  1. Delay (): Recall the example of flashing LEDs. You can add latency to that.
  2. millis (): Returns the time in milliseconds since the Arduino sketch started running as an unsigned long integer.
  3. micros (): Returns the time in microseconds since the Arduino sketch started running as an unsigned long integer.
  4. delayMicroseconds (integer): Delays program execution in (integer) microseconds.

Mathematical functions

  1. min (i, j): Returns the lowest value of the two values ​​i and j
  2. max (i, j): Returns the highest of the two values ​​i and j
  3. abs (i): Returns the absolute value of i
  4. sin (angle): Returns the sine of an angle in radians
  5. cos (angle): Returns the cosine of an angle in radians
  6. tan (angle): Returns the tangent of an angle in radians
  7. sqrt (i): Returns the square root of i
  8. pow (base, exponent): Calculates the power of the order (exponent) of (base). For example: pow (2, 3) == 8
  9. constrain (i, minval, maxval): Constrain the i-value between minval (minimum value) and maxval (maximum value)
  10. map (val, fromL, fromH, toL, toH): Convert val from one range to another
  11. random (i): Returns a long random integer less than i
  12. random (i, j): Returns a long random integer between i and j
  13. randomSeed (k): Use the value k to seed the random () function

Serial communication

  1. Serial.begin (speed): Starts serial communication at the specified speed
  2. Serial.end (): Serial communication connection
  3. Serial.print (DATA): Export DATA to the serial port. DATA can be characters, strings, integers, and floating point numbers.
  4. Serial.available (): Returns the number of characters available to read in the serial buffer
  5. Serial.read (): Reads first character in serial buffer (returns -1 if no data is available)
  6. Serial.write (DATA): Writes DATA to the serial buffer. DATA can be a character, an integer, or an array.
  7. Serial.flush (): Clear the serial buffer after outbound communication is completed

Servo

  1. Servo myServo: Create variable myServo of Servo type
  2. myServo.attach (PIN_NUMBER): myServo is associated with the pin at position PIN_NUMBER
  3. myServo.write (angle): Write an angle from 0 to 180 for the servo attached to myServo
  4. myServo.writeMicroseconds (uS): Writes a value in microseconds to the servo attached to myServo (typically 1000 to 2000 with 1500 as the midpoint)
  5. myServo.read (): Returns an integer containing the current angle of the servo in the range 0 - 180
  6. myServo.attached (): Returns true if the servo is attached to the pin
  7. myServo.detach (): Separate myServo with an attached pin

There are also:

  1. random (): This function helps to generate random numbers.
  2. Tone () and notone (): Do you want a sound to appear in your battery? The tone () function takes care of that, while notone () keeps everything quiet.

The above commands are some of the most useful in handling Arduino boards on IDE. This list is very limited and not exhaustive, but it can help get you started on your projects.

For the details, you need to research the sketches of other Arduino enthusiasts and find out what you can learn from them.

Good luck.

4 ★ | 1 Vote