Skip to content

Instantly share code, notes, and snippets.

@likersacademia
Last active November 25, 2019 10:26
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/3600cc627b1ba5725ddb71ecb578558c to your computer and use it in GitHub Desktop.
Save likersacademia/3600cc627b1ba5725ddb71ecb578558c to your computer and use it in GitHub Desktop.
//05_hikari_led
//暗くなった回数を数えて、5回目で光る
//SchooMyとかいてあるところまですすもう
#define ledPin 10
#define cdsPin 5
#define ON LOW
#define OFF HIGH
int count = 0;
float lux;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(cdsPin, INPUT);
}
long get_lux() {
// R1のAD値を取得
float cds_ad = analogRead(cdsPin);
// AD値を電圧値に変換
float cds_v = cds_ad * 5 / 1023;
lux = 10000 * cds_v / (5 - cds_v) / 1000;
//Serial.print(lux);
//Serial.println(" Lux ");
delay(100);
}
void loop() {
get_lux();
//SchooMy
//プログラムはここからかえます
if (lux < 20) { //このエリアになったら
count++; //カウントする
Serial.print(count); //表示する
Serial.println("カウント"); //表示する
if (count == 5) { //この数になったら
digitalWrite(ledPin, ON); //光る
count = 0; //カウントをゼロにもどす
}
while (lux < 20) {
get_lux();
}
} else { //それ以外は
digitalWrite(ledPin, OFF); //光らない
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment