I am working on an electronic dog bowl project that opens when the dog comes near (with an RFID tag on his collar) and closes once he leaves. The problem with my code is that I want the lid to stay open while the tag is still detected (dog still eating). At the moment, it opens when it detects the tag (which is good), however, doesn't remain open while the dog is standing at the bowl. Lid is controlled by a servo that opens when 'raise_lid' function is called, and closes when 'lower_lid' function is called. Both of these functions work correctly.
Any ideas on how to fix my code would be greatly appreciated.
Sorry, I am a beginner and don't know my way around Arduino very well.
Regards
Boyd
#include <SoftwareSerial.h>
SoftwareSerial id20(3,2); // virtual serial port
char i;
int pos = 80;
int counter = 0;
boolean open_door = false;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
void setup()
{
Serial.begin(9600);
id20.begin(9600);
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(80);
}
void loop ()
{
if(id20.available()) {
Serial.println("tag detected");
while (counter < 16){
i = id20.read(); // receive character from ID20
Serial.println(counter);
counter ++;
}
counter = 0;
open_door = true;
}
if (open_door == true){
raise_lid();
open_door = false;
delay(1000);
}
else (open_door == false);{
lower_lid();
}
delay (2000);
}
void lower_lid()
{
Serial.println(pos);
if (pos <= 30){
Serial.println("lower lid");
for(pos = 30; pos<=80; pos++){ // goes from 90 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.print(pos);
Serial.print(",");
delay(15); // waits 15ms for the servo to reach the position
}
Serial.println("");
}
}
void raise_lid()
{
Serial.println(pos);
if (pos >= 80){
Serial.println("raise lid");
for(pos = 80; pos>=30; pos-=1) // goes from 90 degrees to 0 degrees
{
myservo.write(pos); // tell servo to go to position in variable 'pos'
Serial.print(pos);
Serial.print(",");
delay(15); // waits 15ms for the servo to reach the position
}
Serial.println("");
}
}