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