Last active
November 25, 2019 10:07
-
-
Save likersacademia/5fa9f693407256ab859c6fb969bcfe10 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//02_hikari_speaker | |
//明るさによって違う音が鳴る | |
//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 | |
void setup() { | |
Serial.begin(9600); | |
pinMode(SPEAKER, OUTPUT); | |
pinMode(cdsPin, INPUT); | |
} | |
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) { //このエリアになったら | |
tone(SPEAKER, do); //鳴る | |
delay(20); //待つ | |
Serial.println("do"); //表示する | |
} else if (lux < 20) { //このエリアになったら | |
tone(SPEAKER, re); //鳴る | |
delay(20); //待つ | |
Serial.println("re"); //表示する | |
} else if (lux < 30) { //このエリアになったら | |
tone(SPEAKER, mi); //鳴る | |
delay(20); //待つ | |
Serial.println("mi"); //表示する | |
} else { //それ以外は | |
noTone(SPEAKER); //鳴らない | |
delay(200); //待つ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment