私のグループと私は、定期的に水を飲むように思い出させる、プロジェクト用のスマートなArduino給水ボトルを作ろうとしています。Arduino給水ボトルコードが期待通りに機能しない
私がしようとしているのは、超音波センサーと水位の距離を確認することです。距離が1時間を超えて同じになると、シリアルモニタに「bad」という単語が表示されます。それ以外の場合は「良い」という単語を返します。しかし、このコードを実行すると、距離が5000ミリ秒以上同じ場合でも「良い」という単語が出力されます。
注:コードでは、迅速な結果を得るために1時間ではなく、間隔変数に5000ミリ秒を割り当てました。
注2:これはテストコードであり、完全ではありません。私はこの仕事をする必要があります。
// defines pins numbers
const int trigPin = 8;
const int echoPin = 7;
// defines variables
int maxrange = 20;
int minrange = 0;
long duration, distance;
long previousmillis = 0;
long interval = 5000; //1 hour = 3600000ms;
unsigned long currentmillis = millis();
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
//Converting the distance to cm
distance = duration/29/2;
if (currentmillis > interval) {
if (distance == distance) {
Serial.println("Bad");
Serial.println(distance);
}
} else {
Serial.println("Good");
Serial.println(distance);
}
}
このコードで何が問題になっていますか?私にお知らせください。
いつ 'distance == distance'が間違っていると思いますか? – jonrsharpe
私は間違いをしたと確信しています。あなたが気にしないなら、あなたは訂正で私を助けてくれますか?ありがとうございました。 –
https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – jonrsharpe