SETUP() & LOOP() Function

In an Arduino Program/Sketch you will see two main functions, setup() & other is loop ().

We firstly describe setup() function....

setup()

The setup() function is called when a sketch/program of Arduino starts. We use it to initialize variables, pin modes(Input/Output), start using libraries, etc. The setup function will only run once, after each powerup or reset of the Arduino board.

Example

 
int buttonPin = 3;

void setup()
{
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}

void loop()
{
  // ...
} 
 

loop()

After creating a setup() function, which initializes and sets the initial values, the loop() function does precisely what its name suggests, and loops consecutively, allowing your program to change and respond. We use it to actively control the Arduino board.


Example

 
const int buttonPin = 3;

// setup initializes serial and the button pin
void setup()
{
  Serial.begin(9600);
  pinMode(buttonPin, INPUT);
}



void loop()// loop checks the button pin each time,
{
  if (digitalRead(buttonPin) == HIGH)// and will send serial if it is pressed
    Serial.write('H');
  else
    Serial.write('L');

  delay(1000);
} 
 
Digital iVision Labs!