Skip to content

Instantly share code, notes, and snippets.

@likersacademia
Last active November 25, 2019 10:20
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/6ab5676a602c47ab2de3a47ed366a2e3 to your computer and use it in GitHub Desktop.
Save likersacademia/6ab5676a602c47ab2de3a47ed366a2e3 to your computer and use it in GitHub Desktop.
//04_hikari_speaker
//暗くなったら、1回だけ鳴る
//SchooMyとかいてあるところまですすもう
#define cdsPin 5
#define SPEAKER 10
#define do 262
#define re 294
#define mi 330
#define fa 349
#define so 392
#define ra 440
#define shi 494
#define do2 523
int count = 0;
void setup() {
Serial.begin(9600);
pinMode(cdsPin, INPUT);
pinMode(SPEAKER, 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 ");
//SchooMy
//プログラムはここからかえます
if (lux < 10) { //このエリアになったら
count++; //カウントする
} else { //それ以外は
count = 0; //カウントをゼロにする
noTone(SPEAKER); //鳴らない
}
if (count > 0 && count <= 20) { //この時間だけ
tone(SPEAKER, do); //鳴る
} else if (count > 20) { //この時間は
noTone(SPEAKER); //鳴る
Serial.println(" Stay "); //表示する
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment