光センサーの値を読み取る
光センサーでLEDの点滅の間隔を制御する
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #define LED_PIN 3 #define CDS_PIN 0 int cdsVal = 0; void setup() { pinMode(LED_PIN, OUTPUT); } void loop() { cdsVal = analogRead(CDS_PIN); digitalWrite(LED_PIN, HIGH); delay(cdsVal/10); digitalWrite(LED_PIN, LOW); delay(cdsVal/10); } |
光センサーでLEDの明るさを制御する(PWM)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #define LED_PIN 3 #define CDS_PIN 0 int cdsVal = 0; void setup() { //pinMode(LED_PIN, OUTPUT); } void loop() { cdsVal = analogRead(CDS_PIN); analogWrite(LED_PIN, 255-cdsVal/4); } |