Saturday, April 30, 2016

Arduino Chapter 1 - language structure - setup() and loop()

 

In Arduino they use their programming language which is using OOP but they have customize some keywords or namespaces.

Today I am going to show you about the base structure of Arduino such as Setup() and Loop()


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

Example
int btnPin = 3;
void setup()
{
  Serial.begin(9600);
  pinMode(btnPin, INPUT);
}

void loop()
{
  // ...
}

Note
Serial.begin() //used to tell the processing speed be tween Arduino and computer
pinMode(pin,status) //used to assign status to a pin

loop()This function is used to loop the action repeatedly again and again.

Example
void loop()
{
  if (digitalRead(buttonPin) == HIGH)
    Serial.write('HIGH');
  else
    Serial.write('LOW');

  delay(1000);
}

Note
digitalRead() //used to read digital signal from a pin
Serial.write() //used to show the information back to the computer through Arduino



No comments:
Write comments

Visitors