Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//スクーミーボードのデフォルトプログラム(2/15更新内容:スイッチ:INA 明るさセンサー:INA距離センサー:INBに入れる)
#define echoPin 15
#define trigPin 14
#define cdsPin A5
#define SPEAKERA 10
#define ledPinA 10
#define SPEAKERB 5
#define ledPinB 5
#define ON LOW
#define OFF HIGH
double Duration = 0; //受信した間隔
double Distance = 0; //距離
void setup() {
Serial.begin(9600);
pinMode(echoPin,INPUT);
pinMode(trigPin,OUTPUT);
pinMode(cdsPin, INPUT);
pinMode(SPEAKERA, OUTPUT);
pinMode(ledPinA, OUTPUT);
pinMode(SPEAKERB, OUTPUT);
pinMode(ledPinB, OUTPUT);
}
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH); //超音波を出力
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
Duration = pulseIn(echoPin, HIGH); //センサからの入力
// 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 ");
if (Duration > 0) {
Duration = Duration/2; //往復距離を半分
Distance = Duration*340*100/1000000; //音速を340m/sに設定
Serial.print("Distance:");
Serial.print(Distance);
Serial.println("cm");
}
if (Distance < 10 && lux < 30) {
noTone(SPEAKERB);
digitalWrite(ledPinB, OFF);
tone(SPEAKERA,330) ;
tone(SPEAKERA,569) ;
} else if (Distance > 10 && lux < 30){
noTone(SPEAKERA);
digitalWrite(ledPinA, OFF);
tone(SPEAKERB,689) ;
tone(SPEAKERB,890) ;
} else if (Distance < 10 && lux > 30){
noTone(SPEAKERA);
digitalWrite(ledPinA, OFF);
tone(SPEAKERB,689) ;
tone(SPEAKERB,890) ;
}else{
noTone(SPEAKERA);
noTone(SPEAKERB);
digitalWrite(ledPinA, OFF);
digitalWrite(ledPinB, OFF);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment