使用Arduino UNO和超声波传感器和温度传感器开水(入水壶)

时间:2016-10-17 23:38:49

标签: debugging arduino arduino-ultra-sonic

我正在使用超声波传感器来检测你是否接近它然后开始沸水(我为了这个功能入侵了这个水壶,并将它连接到继电器),这个机器(使用arduino)一旦温度达到一定程度(使用温度传感器),它就会停止继电器(控制水壶的功率),然后使用伺服电机将水壶倾斜到一个单独的杯子里。

到目前为止,当检测到水的温度不够热时,我的代码很容易打开继电器和水壶,但是在温度达到一定量后(我在这种情况下尝试使用35)伺服不会停止旋转。代码使它旋转三度,然后它应该停止(对吗?)但它继续旋转。有没有什么办法解决这一问题?另外,如何完成旋转部件并使程序继续使用超声波传感器并开始处理?

(仅供参考我使用DF机器人或Seeed Studio的超声波传感器和温度传感器以及5 kg伺服电机)

我使用arduino的库进行继电器控制,使用ping库进行超声波传感器和温度传感器

#include <Servo.h>
#include <SoftwareSerial.h>


int val; // 
int tempPin = 1;
int relaypin = 13;
Servo myservo; 
const int pingPin = 7;
int ledpin = 10; 


void setup()
{
  Serial.begin(9600);  
  pinMode(relaypin, OUTPUT);             // taking relay input 
 myservo.attach(2);
 myservo.write(90);                        // servo position 
  pinMode(ledpin, OUTPUT);


}
void loop()
{
  long duration, cm;                           //*following code is for ultrasonic sensor
  pinMode ( pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(1);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(2);
  digitalWrite(pingPin, LOW);

  pinMode (pingPin, INPUT);
  duration = pulseIn (pingPin, HIGH); 

  cm= microsecondsToCentimeters(duration);


val = analogRead(tempPin);
float mv = ( val/1024.0)*5000; 
float temp = mv/10;
//float farh = (temp*9)/5 + 32;                    *last line for ultrasonic sensor
//digitalWrite(relaypin, HIGH);           //start the boiling 
//delay (10);


if (cm <= 20)
{
if (temp <= 27)
  {
  digitalWrite(relaypin,  HIGH);          //start the machine 

//  myservo.write(75);                   //tilting the servo 
//   delay (2000);                    //pause for 2 seconds 
//   myservo.write(65);
//  delay (2000);                    //pause for 2 seconds 
//   myservo.write(45);
//   delay (2000);
//   myservo.write(35);
//   delay (2000);
// myservo.write(90);
//  delay(5000); 

  }
else if(temp >= 35)
  {
  digitalWrite(relaypin, LOW);         //stops the machine 
  delay(5000);
  myservo.write(75);                   //tilting the servo 
   delay (2000);                    //pause for 20 seconds 
   myservo.write(65);
  delay (2000);                    //pause for 20 seconds 
   myservo.write(45);
   delay (2000);
   myservo.write(35);
   delay (2000);
 myservo.write(90);
  delay(5000); 

  }

}

}

long microsecondsToCentimeters(long microseconds)
  {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
  }

2 个答案:

答案 0 :(得分:0)

伺服完成旋转后,温度是否仍然> = 35?在这种情况下,旋转代码将一次又一次地执行,直到temp低于35。

顺便说一句:在您的代码中,您有delay(2000),但评论显示为// pause for 20 seconds。事实上你在这里只延迟了2秒钟。也许这是你出乎意料的行为的另一个原因?如果您只是等待水壶关闭,温度会下降到不会再次触发旋转代码的水平。

关于再次开始这个过程:我不确定你是否知道loop()中的代码只是反复执行,直到你关掉Arduino。因此,一旦温度<= 27并且您足够接近超声波传感器,就可以再次执行用于启动机器的代码。

答案 1 :(得分:0)

修订代码:

(注意:感谢您的回复,Josef。所以我最终更改了代码,并在某种程度上让它以我想要的方式运行。我使用了一个计数器变量来结束循环,但我想知道如果有一种方法在退出循环后再次进入循环?理想情况下,我希望它在每次有人接近时运行,然后当有人再接近它时重复该过程。我意识到延迟并修复它,同样我知道loop()函数。)

#include <Servo.h>
#include <SoftwareSerial.h>
#define trigPin 8
#define echoPin 7


int val; 
int tempPin = A0;
int relaypin = 13;
Servo myservo; 
int ledpin = 10; 
boolean complete = false;

void setup()
{
  Serial.begin(9600);  
  pinMode(relaypin, OUTPUT);             // taking relay input 
  myservo.attach(12);
  myservo.write(90);                        // servo position 
  pinMode(ledpin, OUTPUT);
  SensorSetup();


}
void loop()
{
  int actualDistance = MeasureDistance();
  Serial.print(actualDistance);
  Serial.println(" cm");
  delay(500);


  val = analogRead(tempPin);
  float mv = ( val/1024.0)*5000; 
  float temp = mv/10;
  //float farh = (temp*9)/5 + 32;                    *last line for         ultrasonic sensor
  //digitalWrite(relaypin, HIGH);           //start the boiling 
  //delay (10);
  Serial.println(temp);

  if (actualDistance <= 25)
  {
    while (temp >= 20 && temp <=45)
    {
     digitalWrite(relaypin,  HIGH);          //start the machine 
      Serial.println(actualDistance);

    }
  }
  else if(actualDistance >= 20)
 {
    if (temp >= 55 && complete == false)
    {
      Serial.println(actualDistance);
      digitalWrite(relaypin, LOW);         //stops the machine 
      delay(5000);
       myservo.write(75);                   //tilting the servo 
         delay (2000);                    //pause for 2 seconds 
         myservo.write(65);
        delay (2000);                    //pause for 2 seconds 
         myservo.write(45);
         delay (2000);
         myservo.write(35);
         delay (2000);
       myservo.write(25);
        delay (2000);
       myservo.write(15);
        delay (2000);
       myservo.write(0);
        delay (2000);
       myservo.write(25);
        delay (2000);
       myservo.write(35);
        delay (2000);
       myservo.write(45);
        delay (2000);
       myservo.write(60);
        delay (2000);
       myservo.write(90);
       delay(5000); 
      complete = true;
    }

  }

}


void SensorSetup(){ 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

int MeasureDistance(){        // a low pull on pin COMP/TRIG  triggering a         sensor reading
  long duration;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  long distance = (duration / 2) / 29.1;
  return (int)distance;
}