Skip to content

Instantly share code, notes, and snippets.

@likersacademia
Last active November 25, 2019 10:27
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/37fcfdec5e1af711e3553fb587a985ad to your computer and use it in GitHub Desktop.
Save likersacademia/37fcfdec5e1af711e3553fb587a985ad to your computer and use it in GitHub Desktop.
//05_hikari_speaker
//暗くなった回数を数えて、5回目で光る
//SchooMyとかいてあるところまですすもう
#define SPEAKER 10
#define cdsPin 5
#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;
float lux;
void setup() {
Serial.begin(9600);
pinMode(SPEAKER, 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) { //この数になったら
tone(SPEAKER, do) ; //鳴る
count = 0; //カウントをゼロにする
}
while (lux < 20) {
get_lux();
}
} else {
noTone(SPEAKER); //鳴らない
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment