Skip to content

Instantly share code, notes, and snippets.

@likersacademia
Created October 19, 2019 09:54
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/8a76a20b25e3fc67b38dbc322fb5cfd6 to your computer and use it in GitHub Desktop.
Save likersacademia/8a76a20b25e3fc67b38dbc322fb5cfd6 to your computer and use it in GitHub Desktop.
//設定した時間内に行われた動作をカウントし、条件を満たしていると鳴る
//30秒以内に人が5回以上暗くなったら、鳴る
#define cdsPin 5
#define SPEAKER 10
int count=0;
int stay=0;
int start_time=0;
int current_time=0;
float lux;
#define do 262
#define re 294
#define mi 330
#define fa 349
#define so 392
#define ra 440
#define si 494
#define do2 523
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計算
lux = 10000 * cds_v / (5 - cds_v) / 1000;
//Serial.print(lux);
//Serial.println(" Lux ");
delay(100);
}
long get_time(){
if(stay==0){
int num = millis();
start_time = num / 1000;
stay++;
}
int now = millis();
current_time = (now / 1000) - start_time;
}
void loop() {
get_lux();
get_time();
if(current_time<=20){
if(lux<20){
noTone(SPEAKER);
delay(200);
count++;
Serial.print(count);
Serial.println("回");
while(lux<20){
get_lux();
}
}
}else{
Serial.println("結果");
if(count>5){
Serial.println("OK");
tone(SPEAKER,do) ;
delay(200);
noTone(SPEAKER); //音が鳴らない
delay(200); //鳴らない時間を数字を変えて設定しよう
count=0;
stay=0;
}else{ //そうでない場合
Serial.println("NO");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment