반응형
블로그 이미지
개발자로서 현장에서 일하면서 새로 접하는 기술들이나 알게된 정보 등을 정리하기 위한 블로그입니다. 운 좋게 미국에서 큰 회사들의 프로젝트에서 컬설턴트로 일하고 있어서 새로운 기술들을 접할 기회가 많이 있습니다. 미국의 IT 프로젝트에서 사용되는 툴들에 대해 많은 분들과 정보를 공유하고 싶습니다.
솔웅

최근에 받은 트랙백

글 보관함



Arduino Basic


Open Source Hardware

- Arduino UNO : 8 bit CPU

- Arduino DUE

- Arduino TRE : Arduino + Beaglebone black

- Arduino Yun :(Built-in Wifi

- Arduino Mini Nano

- Netduino

- Raspberry Pi : 32 bit, Linux, 700 MB

- Beaglebone black : 32 bit, 1 GB, 512 MB Memory, Android O

- Intel Galileo : Pentium CPU,  can share Arduino shield

 

IDE

- Arduino Sketch : Language - C/C++

- Arduino Scratch : GNU Development Tool

- Arduino Processing : for Art

 

Arduino Shields


Sensor & Actuator

 

Breadboard

 

 



◎ Control Arduino Built-in LED


Source Code (Sketch Sample : Blink)


`/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
  it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
  the correct LED pin independent of which board is used.
  If you want to know what pin the on-board LED is connected to on your Arduino model, check
  the Technical Specs of your board  at https://www.arduino.cc/en/Main/Products
 
  This example code is in the public domain.

  modified 8 May 2014
  by Scott Fitzgerald
 
  modified 2 Sep 2016
  by Arturo Guadalupi
 
  modified 8 Sep 2016
  by Colby Newman
*/


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}


==> The built-in LED will blink every second.




◎ Control External LED


circuit diagram




Source Code : Same Source Code as above one


==> The External LED will blink every second.


Youtube Tutorial of above examples (in Korean) : https://www.youtube.com/watch?v=YcAdhH3Y-HU&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w&index=2




◎ Control multiple external LEDs


LED : Light-emitting diode 


circuit diagram




Source Code


#define DELAY_TIME 100

void setup() {
  // put your setup code here, to run once:
  pinMode(2. OUTPUT);
  pinMode(3. OUTPUT);
  pinMode(4. OUTPUT);
  pinMode(5. OUTPUT);
  pinMode(6. OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  digitalWrite(2, HIGH);
  delay(DELAY_TIME);
  digitalWrite(2, LOW);
  digitalWrite(3, HIGH);
  delay(DELAY_TIME);
  digitalWrite(3, LOW);
  digitalWrite(4, HIGH);
  delay(DELAY_TIME);
  digitalWrite(4, LOW);
  digitalWrite(5, HIGH);
  delay(DELAY_TIME);
  digitalWrite(5, LOW);
  digitalWrite(6, HIGH);
  delay(DELAY_TIME);
  digitalWrite(6, LOW);
  digitalWrite(5, HIGH);
  delay(DELAY_TIME);
  digitalWrite(5, LOW);
  digitalWrite(4, HIGH);
  delay(DELAY_TIME);
  digitalWrite(4, LOW);
  digitalWrite(3, HIGH);
  delay(DELAY_TIME);
  digitalWrite(3, LOW);
 
}


==> The LEDs will turn on and off sequentially.



Refactoring above code using FOR statement


#define DELAY_TIME 100

void setup() {
  // put your setup code here, to run once:
  for(int thisPin = 2; thisPin < 7; thisPin++) {
    pinMode(thisPin,OUTPUT);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  // loop from the lowest pin to the highest:
  for (int thisPin = 2; thisPin < 7; thisPin++) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(DELAY_TIME);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }

  // loop from the highest pin to the lowest:
  for (int thisPin = 6; thisPin >= 2; thisPin--) {
    // turn the pin on:
    digitalWrite(thisPin, HIGH);
    delay(DELAY_TIME);
    // turn the pin off:
    digitalWrite(thisPin, LOW);
  }
 
}



◎ Button Control


circuit diagram



Source Code


#define LED 12
#define BUTTON 7

void setup() {
  // put your setup code here, to run once:
  pinMode(LED, OUTPUT);
  pinMode(BUTTON, INPUT);

}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(BUTTON) == HIGH) {
    digitalWrite(LED, HIGH);
    delay(500);
    digitalWrite(LED,LOW);
  }
}


==> If press the button then turn on the LED for 0.5 second.


Youtube Tutorial of above examples (in Korean) : https://www.youtube.com/watch?v=wtbjzGIQe_I&index=3&list=PL0Vl139pNHbe-JlsydLg3NFRk6nC_cC7w





Application

I am going to combine both Button control and Multiple LEDs control source codes.



Source Code



#define BUTTON 8
#define DELAY_TIME 100

void setup() {
  // put your setup code here, to run once:
  for(int thisPin = 2; thisPin < 7; thisPin++) {
    pinMode(thisPin,OUTPUT);
  }
  pinMode(BUTTON, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(digitalRead(BUTTON) == HIGH) {
    // loop from the lowest pin to the highest:
     for (int thisPin = 2; thisPin < 7; thisPin++) {
      // turn the pin on:
       digitalWrite(thisPin, HIGH);
      delay(DELAY_TIME);
       // turn the pin off:
      digitalWrite(thisPin, LOW);
     }

    // loop from the highest pin to the lowest:
    for (int thisPin = 6; thisPin >= 2; thisPin--) {
      // turn the pin on:
      digitalWrite(thisPin, HIGH);
      delay(DELAY_TIME);
      // turn the pin off:
      digitalWrite(thisPin, LOW);
    }
  }
}


==> The LEDs will turn on and off sequentially when press button





반응형

Comment