Arduino basic code structure - ElectroEngineers

Destination to project makers.

Sunday, 20 August 2017

Arduino basic code structure

Arduino is one of the trend setting open source development board used by most of the Embedded developers. Arduino is popular among students, hobby project makers and professionals. It is known for its easiness. The code structure is easily understandable and it has got impressive support, discussion forum on its official website.

Arduino code structure.

As I said, arduino is easily to use and understandable comoare to Embedded C programming. It is known for its simplicity.
Arduino code consists of two parts
1. Setup                             2. Loop

The code we write in the arduino are called as SKETCHES.
Every sketch has two functions void setup() and void loop().
setup function is excecuted only once, when the arduino is powered up. Loop function is excecuted continuosly after setup.
Initialisations are made in tge setup function. The loop function consists of the commamds to be excecuted continuosly by the controller.
If you want to attach any headers, we can add it via tools or we can include the headers file as shown below.
#include header_name.h
Headers are to be placed before the setup function.

Pin mode declaration.

Arduino pins numbers can be declared before the setup as shown below. 
#define pin 2 or int pin=2;// declared before set up
void setup(){
}
void loop(){
}

pinMode- This is the keyword use to set up the declared pin as either input or output. 
Arduino digital pins are set to input by default. So there is no need to declare the input pins.
Pins configured as input are said to be in high imedence state and Output pibs are said to be in low impedence state. The pinMode is used inside the setup function as shown below.
digitalRead()- This function is used to read the digital value from an input pin. Its result will be either zero or one.
Usage: 
Int=digitalRead(11);
digitalWrite()- This function is used set or reset the digital pins of arduno. 
Usage:
digitalWrite(11, HIGH);
digitalWrite(6,LOW);
analogRead()- This function is used to read the values from analog input pin. It retuturns values ranging from 0 to 1023 where 0 is the least and 1023 is the highest value.
Usage: can be used to read potentiometer
int pot_val=analogRead(3);
analogWrite()- This function is used to write the values to the analog pin. The values must be between 0 and 255. 

No comments:

Post a Comment

Followers