exercise
빛을 감지하여 LED 밝기 조절, 켜진 LED 개수 조절해 봅시다
material
Photocell
LED(any color) 6개
220 Ω resistor 6개 / 10K Ω resistor
circuit
code
int sensorpin = 2;
int ledpins[] = {9,10,11,12,13};
int numpin = 5;
int pwmpin = 6;
void setup() {
Serial.begin(9600);
for(int i=0; i<numpin; i++) {
pinMode(ledpins[i], OUTPUT);
}
}
void loop() {
int val = analogRead(sensorpin);
Serial.println(val); //20~220
int numleds = map(val, 0, 1023, 0, 5);
numleds = constrain(numleds, 0, 5);
for(int i=0; i<numleds; i++){
digitalWrite(ledpins[i], HIGH);
}
for(int j=numleds; j<numpin; j++) {
digitalWrite(ledpins[j], LOW);
}
int brightness = map(val, 0, 1023, 0, 255);
brightness = constrain(brightness, 0, 255);
analogWrite(pwmpin, brightness);
}
further exercise
[초급] 반대로 손으로 센서를 가릴 때 켜지는 LED 개수가 줄어들도록 해봅시다
[중급] RGB LED를 써서 센서를 가릴 때 LED 색깔이 변하도록 해봅시다
[고급] 손으로 센서를 가리는 정도에 따라 Kit 애니메이션하는 LED 개수를 조절해 봅시다