본문으로 바로가기

[네퓨즈수업 - 아두이노] Fade 예제

category 네퓨즈 수업 2016. 5. 19. 17:57

// 예제

/*

 Fade


 This example shows how to fade an LED on pin 9

 using the analogWrite() function.


 The analogWrite() function uses PWM, so if

 you want to change the pin you're using, be

 sure to use another PWM capable pin. On most

 Arduino, the PWM pins are identified with 

 a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.


 This example code is in the public domain.

 */


int led = 9;           // the PWM pin the LED is attached to

int brightness = 0;    // how bright the LED is

int fadeAmount = 5;    // how many points to fade the LED by


// the setup routine runs once when you press reset:

void setup() {

  // declare pin 9 to be an output:

  pinMode(led, OUTPUT);

}


// the loop routine runs over and over again forever:

void loop() {

  // set the brightness of pin 9:

  analogWrite(led, brightness);


  // change the brightness for next time through the loop:

  brightness = brightness + fadeAmount;


  // reverse the direction of the fading at the ends of the fade:

  if (brightness == 0 || brightness == 255) {

    fadeAmount = -fadeAmount ;

  }

  // wait for 30 milliseconds to see the dimming effect

  delay(30);

 // 반복문이 짧은편이라 우리가 눈으로 체크할수 있을정도의 시간을주기위해

}

//구현

void setup() {


pinmode(11,OUTPUT); // 12번핀에 출력

analogWrite(11,0); // 꺼진상태

}

int bright = 0; // 밝기

int v = 5;

void loop() {

     bright = bright + 5; // 계속해서 밝아짐

if(bright >= 254 || bright <= 0) { // 밝아지는것에 한계를둠

v = -v;

}

analogWrite(11,bright);

}

// 아날로그지만 디지털핀을 꽂는곳인 11번에다꽂아도 가능하다. 그이유는 PWM때문에이다.

// pinmode를 쓰는것은 디지털핀을 꽂을때만 사용한다.


-----PWM 설명--------

*11,*10,*9,*6,*5,*3 (포트번호)

디지털핀은 0,1만 줄수있다.

디지털핀에서 *PWM이란 펄스의 폭을 변조해서 표현. (단순히 밝기를 조정하는것 뿐만아니라 신호를 줄 수 있다.)

핀 하나가지고 PWM을 이용해서 신호를 줄수있다. (디지털로 아날로그를 표현하거나 , 아날로그를 디지털로 받을수있다.)

-----digitalRead-------

//버튼을 13번포트에

void setup() {

 pinMode(13, INPUT);

 Serial.begin(9600);

}

void loop() {

  int input = digitalRead(13);

  Serial.println(input);

  delay(30);

}

-----analogRead-------

// 아날로그 돌리는

void setup() {

 pinMode(13, INPUT);

 Serial.begin(9600);

}

void loop() {  

  int value = analogRead(A0); // 아날로그는 A를 붙여서 포트를 표시한다.

  Serial.println(value); // value에 저장해서 출력

  delay(30);

}



과제 ) 돌리면 밝기가 변하게 어떻게할 수 있을까?

- 아날로그는 0~1023까지

- 디지털은 0~254까지

이것을 조절해주는 함수는 map()

map()예제-------------------------

void setup() {

 Serial.begin(9600);

}

void loop() {

  int analogVal = analogRead(A0);

  int digitalVal = map(analogVal , 0 , 1023 , 0 , 255);

// (어떤것에대해 할지,들어오는 최솟값, 들어오는 최댓값,들어오는 최솟값, 들어오는 최댓값)

  Serial.println(digitalVal);

  delay(30);

}

-------------------------------

해결 -------------------

void setup() {

  pinMode(9,OUTPUT);

  Serial.begin(9600); // 포트 9600

}

void loop() {

  int analogVal = analogRead(A0);

  int digitalVal = map(analogVal , 0 , 1023 , 0 , 255);

  analogWrite(9,digitalVal);

}

----------------------


: 5V에서 전기가계속나오고

GND에서는 전기가 계속 들어옴


전기는 저항이 적은쪽으로 가려함


'네퓨즈 수업' 카테고리의 다른 글

[네퓨즈 수업] 소켓통신  (1) 2016.04.12
[네퓨즈 수업 - C] 포인터 정리  (0) 2016.04.12
[네퓨즈 수업 - C] 함수 정리  (1) 2016.04.07
[네퓨즈 수업 - C] 배열 정리  (0) 2016.04.05