Understanding the Arduino Code

Structure


An Arduino sketch runs in two parts:

  • void setup()

This is where you place the initialization code that is the instructions that sets up the board before the main loop of the sketch starts.

  • void loop()


This contains the main code of your sketch. It contains a set of instructions that get repeated over and over until the board is switched off.

Special symbols

Arduino includes a number of symbols to delineate lines of code, comments,
and blocks of code.

  • ; (semicolon)

Every instruction (line of code) is terminated by a semicolon. This syntax
lets you format the code freely. You could even put two instructions on
the same line, as long as you separate them with a semicolon. 

  • comments(// or /*........*/)

These are portions of text ignored by the Arduino processor, but are extremely useful to remind yourself (or others) of what a piece of code does. A comment can consist of one line or multi

Constants

Arduino includes a set of predefined keywords with special values. HIGH and LOW are used, for example, when you want to turn on or off an Arduino pin. INPUT and OUTPUT are used to set a specific pin to be either and input or an output of a condition or expression.

Variables

Variables are named areas of the Arduino’s memory where you can store data that you can use and manipulate in your sketch. As the name suggests, they can be changed as many times as you like. Because Arduino is a very simple processor, when you declare a variable
you have to specify its type. This means telling the processor the size of the value you want to store. Here are the datatypes that are available:

  • boolean


Can have one of two values: true or false.

  • char


Holds a single character it may be a number or a letter

  • byte

Holds a number between 0 and 255. As with chars, bytes use only one byte of memory.

  • int


Uses 2 bytes of memory to represent a number between –32,768 and 32,767; it’s the most common data type used in Arduino.

Loop functions

  • if . . . else

if the expression is true, whatever follows will be executed. If it is false, the block of code following else will be executed. It is possible to use just if without providing a clause.
NOTE:if there are no {}(brackets) after the if command only the folowwing line will be executed under the if command.
Example:
if (val==1){
 digitalWrite(LED,HIGH)
}
else{
 digitalWrite(LED,LOW)
}

  • for

This command lets you repeat a block of code a specified number of times.
Example:
for (int i = 0; i < 10; i++){
 serial.print("hello world")
}

  • do.......while

Similar to if, this executes a block of code when a certain condition is true
Example:
//blink LED when sensor reads a value of 245
do{
digitalWrite(13,HIGH);
}
while(sensorValue = 255)
NOTE:we can also use only a "while " without a do command
Example:
while(sensorValue = 255){
 digitalWrite(13,HIGH);
}

Other important commands

  • break

this command lets you leave the current loop and execute the next set of code
Example:
do {
// Leaves the loop if a button is pressed
if (digitalRead(7) == HIGH)
break;
digitalWrite(13,HIGH);
delay(100);
digitalWrite(13,LOW);
delay(100);
sensorValue = analogRead(1);
} while (sensorValue < 512);

  • continue

When used inside a loop, continue lets you skip the rest of the code inside
it and force the condition to be tested again.
Example:
for (light = 0; light < 255; light++)
{
// skip intensities between 140 and 200
if ((x > 140) && (x < 200))
continue;
analogWrite(PWMpin, light);
delay(10);
}

Compound Operators

These are special operators used to make code more concise for some
very common operations like incrementing a value.

  • increment and decrement (–– and ++)

These increment or decrement a value by 1. Be careful: if you write i++
this increments i by 1 and evaluates to the equivalent of i+1; ++i evaluates
to the value of i and then increments i. The same applies to ––.

  • += , –=, *= and /=

These make it shorter to write certain expressions. The following two
expressions are equivalent:
a = a + 5;
a += 5;

Input and output functions

  • pinMode(pin, mode)

Reconfigures a digital pin to behave either as an input or an output.
Example:
pinMode(7,INPUT);

  • digitalWrite(pin, value)

Turns a digital pin either on or off. Pins must be explicitly made into an
output using pinMode before digitalWrite will have any effect.
Example:
digitalWrite(8,HIGH); // turns on digital pin 8

  • int digitalRead(pin)

Reads the state of an input pin, returns HIGH if the pin senses some
voltage or LOW if there is no voltage applied.
Example:
digitalRead(7);

  • int analogRead(pin)

Reads the voltage applied to an analog input pin and returns a number
between 0 and 1023 that represents the voltages between 0 and 5 V.
Example:
analogRead(0);

  • analogWrite(pin, value)

Changes the PWM rate on one of the pins marked PWM. pin may be 11,10,
9, 6, 5, 3. value may be a number between 0 and 255 .
Example:
analogWrite(9,128);

Time functions

  • delay(ms)

Pauses the program for the amount of milliseconds specified.
Example:
delay(500);

  • delayMicroseconds(us)

Pauses the program for the given amount of microseconds.
Example:
delayMicroseconds(1000);

Serial communication

  • Serial.begin(speed)

Prepares Arduino to begin sending and receiving serial data. You’ll
generally use 9600 bits per second (bps) with the Arduino IDE serial monitor,
but other speeds are available, usually no more than 115,200 bps.
Example:
Serial.begin(9600);

  • Serial.print(data)

Sends some data to the serial port. .
Examples:
Serial.print(75); 
Serial.println(data)

  • int Serial.read()


Fetches one byte of incoming serial data.

Comments