0

我正在制作这台机器(使用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;
  }
4

2 回答 2

0

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

顺便说一句:在您的代码中,您有一个delay(2000)但评论说// pause for 20 seconds. 实际上,您在这里只延迟了 2 秒。也许这是您出现意外行为的另一个原因?如果您在关闭水壶的情况下等待足够长的时间,温度将下降到不会再次触发旋转代码的水平。

关于再次开始该过程:我不确定您是否知道代码loop()只是重复执行,直到您关闭 Arduino。因此,一旦温度 <= 27 并且您离超声波传感器足够近,启动机器的代码就会再次执行。

于 2016-10-18T05:54:48.717 回答
0

修改后的代码:

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

#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;
}
于 2016-11-03T02:35:41.847 回答