简体   繁体   中英

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.

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. 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)

I am using arduino's library for relay control, ping library for the ultrasonic sensor, and temperature sensor

#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? In that case the rotation code will be executed again and again until temp is falling below 35.

BTW: in your code you have a delay(2000) but the comment says // pause for 20 seconds . In fact you are only delaying for 2 seconds here. 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. 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.

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.)

#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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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