简体   繁体   English

使用Arduino UNO和超声波传感器和温度传感器将水煮沸(侵入水壶)

[英]Boiling water (hacking into Kettle)using Arduino UNO and Ultrasonic sensor and Temperature sensor

I am making this machine thing(using arduino) that uses ultrasonic sensor to detect if you are close to it and then it starts boiling water (i hacked into this kettle for this function, and connected it to a relay), and once the temperature reaches a certain degree (using temperature sensor) it then stops the relay (that controls the power of the kettle), and tilts the kettle using the servo motor into a separate cup. 我正在使用超声波传感器制造这台机器的东西(使用arduino),该机器使用超声波传感器检测您是否靠近它,然后它开始沸腾水(我为此功能闯入了水壶,并将其连接到继电器),并且一旦温度达到一定程度(使用温度传感器)后,它将停止继电器(控制水壶的功率),并使用伺服电机将水壶倾斜到单独的杯子中。

As of now my code easily turns on the relay and the kettle when it detects that the temperature of water is not hot enough, but after the temperature has reached a certain amount (i used 35 in this case just as a try) the servo wouldn't stop doing the rotation. 到目前为止,当我的代码检测到水温不够高时,我的代码很容易打开继电器和水壶,但是当温度达到一定值后(本例中我尝试使用35),伺服器将不要停止旋转。 The code makes it rotate at the three degrees and then it should stop (right?) but then it keeps rotating. 该代码使它旋转三个角度,然后它应该停止(对吗?),但它继续旋转。 Is there any way to fix this? 有没有什么办法解决这一问题? Also, how do i finish the rotation part and make the program go on to use the ultrasonic sensor again and begin the process? 另外,如何完成旋转部分并使程序继续以再次使用超声波传感器并开始该过程?

(FYI i am using a DF Robot or Seeed Studio's Ultrasonic sensor and temperature sensor and a 5 kg servo motor) (仅供参考,我使用的是DF机器人或Seeed Studio的超声波传感器和温度传感器以及5公斤伺服电机)

I am using arduino's library for relay control, ping library for the ultrasonic sensor, and temperature sensor 我正在使用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;
  }

Could it be that after the servo did the rotation the temperature is still >= 35? 伺服旋转后,温度是否仍可能大于等于35? In that case the rotation code will be executed again and again until temp is falling below 35. 在这种情况下,将反复执行旋转代码,直到temp降至35以下。

BTW: in your code you have a delay(2000) but the comment says // pause for 20 seconds . 顺便说一句:在您的代码中您有一个delay(2000)但是注释说// pause for 20 seconds In fact you are only delaying for 2 seconds here. 实际上,您在这里仅延迟2秒。 Maybe that's another reason why you get unexpected behavior? 也许这是您出现意外行为的另一个原因? If you just wait long enough with the kettle being turned off the temperature is falling to a level which will not trigger the rotation code again. 如果您在水壶关闭的状态下等待了足够长的时间,温度将下降到不会再次触发旋转代码的水平。

Regarding beginning the process again: I'm not sure if you are aware that the code in loop() is just repeatedly executed until you turn off the Arduino. 关于再次开始该过程:我不确定您是否知道loop()中的代码只是重复执行,直到您关闭Arduino。 So as soon as the temperature is <= 27 and you are close enough to the ultrasonic sensor the code for starting the machine is just executed again. 因此,只要温度<= 27,并且您与超声波传感器的距离足够近,就会再次执行启动机器的代码。

Revised code: 修改后的代码:

(note: Thank you for your response, Josef. so i ended up changing the code and somehow made it run the way i wanted it to, to some extent. I used a counter variable to end the loop but I was wondering if there is a way to get into the loop again after exiting the loop? ideally i would want it to run everytime someone comes close and then repeat the process when someone comes close to it again. I realized the delay and fixed it, and same i know about the loop() function.) (注意:约瑟夫,感谢您的答复。因此,我最终更改了代码,并以某种方式使其按我想要的方式运行。我使用了计数器变量来结束循环,但我想知道是否存在一种退出循环后再次进入循环的方法?理想情况下,我希望它在有人接近时再次运行,然后在有人再次接近时重复该过程。我意识到延迟并已解决,我也知道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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM