Skip to content

Instantly share code, notes, and snippets.

@likersacademia
Last active August 29, 2018 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save likersacademia/1bd731990ef210c07928eb34da009b25 to your computer and use it in GitHub Desktop.
Save likersacademia/1bd731990ef210c07928eb34da009b25 to your computer and use it in GitHub Desktop.
// 電気が消え、かつスイッチが押されるとLEDがつく
#define cdsPin 1 //光センサーが1番ピン
#define buttonPin 12 //スイッチが12番ピン
#define ledPin 2 //LEDが2番ピン
bool buttonState = 0;
void setup() {
Serial.begin(9600);
pinMode(cdsPin, INPUT);
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
// R1のAD値を取得
float cds_ad = analogRead(cdsPin);
// AD値を電圧値に変換
float cds_v = cds_ad * 5 / 1023;
// 電圧値より、Lux計算
float lux = 10000 * cds_v / (5 - cds_v) / 1000;
Serial.print(lux);
Serial.println(" Lux ");
if (lux > 20) {
Serial.println("High lux");
delay(2000);
} else {
Serial.println("Low lux");
delay(2000);
}
buttonState = digitalRead(buttonPin);
if (lux < 20 && buttonState == HIGH) { //lux < 20(20luxより小さい)→<、>と数値の変更ができます。buttonState == HIGH(スイッチが押される)
digitalWrite(ledPin, HIGH); //HIGH:光る LOW:光らない
delay(200);
} else { //上以外のときは、下のことが起こる
digitalWrite(ledPin, LOW); //HIGH:光る LOW:光らない
delay(200);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment