温度センサーと超音波センサーを使い正確な距離を割り出したあとにその距離に応じてbuzzerを鳴るようにしたいんですけど、なりません。シリアルモニターを見る限りでは距離の割り出しはうまく行っているようです。回路図の問題ですかね?どの様に直したら上手くいきますか?
int sensor = 0; int BEAT = 300; int SPK = 13; double Duration = 0; //受信した間隔 double Distance = 0; //距離 void setup() { Serial.begin( 9600 ); pinMode( 3, INPUT ); pinMode( 2, OUTPUT ); } void loop() { int input = analogRead(sensor); float voltaje = 5.0 / 1024 * input; float temp = voltaje * 100 - 50; Serial.print("temp:"); Serial.print(temp); //温度を表示 Serial.print("c"); digitalWrite( 2, LOW ); delayMicroseconds( 2 ); digitalWrite( 2, HIGH ); //超音波を出力 delayMicroseconds( 10 ); // digitalWrite( 2, LOW ); Duration = pulseIn( 3, HIGH ); if (Duration > 0) { Distance = Duration / 2; float sspeed = 331.5 + 0.6 * temp; Serial.print("\tspeed:"); Serial.print(sspeed); Serial.print("m/sec"); Distance = Distance * sspeed * 100 / 1000000; // Serial.print("\tDistance:"); Serial.print(Distance); Serial.print("cm"); } if (Distance < 4) { tone(SPK, 523, BEAT); //C Serial.println(" [C] "); } else if (Distance >= 4 && Distance < 7) { tone (SPK, 494, BEAT); //B Serial.println(" [B] "); } else if (Distance >= 7 && Distance < 10) { tone(SPK, 440, BEAT); Serial.println(" [A]"); } else if (Distance >= 10 && Distance < 13) { tone(SPK, 392, BEAT); //G Serial.println(" [G] "); } else if (Distance >= 13 && Distance < 16) { tone(SPK, 349, BEAT); //F Serial.println(" [F]"); } else if (Distance >= 16 && Distance < 19) { tone(SPK, 330, BEAT); //E Serial.println(" [E] "); } else if (Distance >= 22 && Distance < 25) { tone(SPK, 294, BEAT); //D Serial.println(" [D]"); } else if (Distance >= 25) { tone(SPK, 262, BEAT); //C Serial.println("[c]"); } Serial.println(""); delay(500); }
あなたの回答
tips
プレビュー