레이블이 Coding인 게시물을 표시합니다. 모든 게시물 표시
레이블이 Coding인 게시물을 표시합니다. 모든 게시물 표시

2012년 5월 3일 목요일

아두이노 코딩 기초

1. 기본 구조는 setup() 함수와 loop() 함수 각각 하나로 구성되어 있다.

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly: 
  
}


2. setup 함수는 pin 설정을 하는 곳이고, loop 함수는 반복될 로직이 들어가는 곳이다.


3. 문법은 C언어에 충실한 것 같다. 기본적인 함수는 http://arduino.cc/en/Reference/HomePage 여기를 참조하고 특이하면서 자주 쓰는 함수를 보면..
    pinMode(pin, mode) : pin - 핀번호, mode - INPUT 또는 OUTPUT 으로 setup함수안에 설정
  digitalWrite(pin, value) : pin - 핀번호, valude - HIGH 또는 LOW로 loop함수안에서 사용
  digitalRead(pin) : pin - 핀번호로 loop함수안에서 사용


예제)

int ledPin = 13; // LED connected to digital pin 13
int inPin = 7;   // pushbutton connected to digital pin 7
int val = 0;     // variable to store the read value

void setup()
{
  pinMode(ledPin, OUTPUT);      // sets the digital pin 13 as output
  pinMode(inPin, INPUT);      // sets the digital pin 7 as input
}

void loop()
{
  val = digitalRead(inPin);   // read the input pin
  digitalWrite(ledPin, val);    // sets the LED to the button's value
}