Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//06_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
double luxnew = 0; //新しい光の値
double luxold = 0; //古い光の値
double ans = 0; //差
void setup() {
Serial.begin(9600);
pinMode(cdsPin, INPUT);
pinMode(SPEAKER, OUTPUT);
}
void loop() {
float cds_ad = analogRead(cdsPin);
// AD値を電圧値に変換
float cds_v = cds_ad * 5 / 1023;
// 電圧値より、Lux計算
luxnew = 10000 * cds_v / (5 - cds_v) / 1000;
Serial.print(luxnew);
Serial.println(" Lux ");
ans = luxnew - luxold;
//SchooMy
//プログラムはここからかえます
if (abs(ans) > 10) { //この速さの間の時は
tone(SPEAKER, do); //鳴る
} else { //それ以外は
noTone(SPEAKER); //鳴らない
}
luxold = luxnew;
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment