본문 바로가기
Programming/아두이노

아두이노 : LED, 점점 밝게 하기

by 공부합시다홍아 2020. 7. 1.
const int buttonPin = 2;
const int ledPin = 3;

void setup() {
  // put your setup code here, to run once:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  int buttonInput = digitalRead(buttonPin);
  if(buttonInput = HIGH){
    for(int t_high = 0; t_high<=255; t_high++)
    {
      analogWrite(ledPin, t_high);
      delay(4);
    }
  }
  else
  {
    analogWrite(ledPin, 0);
  }
}

for문을 사용하여 t_high 변수 값을 0부터 255까지, 즉 어둠에서 가장 밝은 단계까지 4밀리 초 간격으로 주기적으로 반복한다.

 

 

2. 버튼 클릭시 LED가 천천히 증가

 

728x90

'Programming > 아두이노' 카테고리의 다른 글

아두이노 : 버튼 제어, LED 불켜기  (0) 2020.07.01