Microchip controlled servo

Hi All,

I am working on an automatic pet feeder addition project where I need to use a pet injectable microchip to activate a servo motor when the RFID chip reader reads the correct chip and does not open for any other chip. When the correct chip is read a flap will open for as long as the animal is still being picked up by the ultrasonic sensor. Once the animal moves away the flap closes after say 5-10 seconds. If another animal's chip is read in that countdown period, the flap will automatically default to closing.

The hardware I have for this is in the images below. I am using an Arduino Uno R3 for the controller.



The code found and been able to get working so far for the scanning is below. I am unsure how to incorporate the servo and the sensor into the mix.

If you have any suggestions that would be appreciated. Thanks!

/* Test scanner to activate servo

Test Tag = Cat 3 = TAG: 978 142000100827

*/

#include <SoftwareSerial.h>
#include <Rfid134.h>

// implement a notification class,
// its member methods will get called 
//
class RfidNotify
{
public:
  static void OnError(Rfid134_Error errorCode)
  {
    // see Rfid134_Error for code meaning
    Serial.println();
    Serial.print("Com Error ");
    Serial.println(errorCode);
  }

  static void OnPacketRead(const Rfid134Reading& reading)
  {

     digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000); 
    char temp[8];

    Serial.print("TAG: ");

    // since print doesn't support leading zero's, use sprintf
    sprintf(temp, "%03u", reading.country);
    Serial.print(temp);   

    Serial.print(" ");
    if (reading.id == 14200010827) {
      Serial.print("CAT!");

    };
    // since print doesn't support leading zero's, use sprintf
    // since sprintf with AVR doesn't support uint64_t (llu/lli), use /% trick to
    // break it up into equal sized leading zero pieces
    sprintf(temp, "%06lu", static_cast<uint32_t>(reading.id / 1000000));
    Serial.print(temp); 
    sprintf(temp, "%06lu", static_cast<uint32_t>(reading.id % 1000000));
    Serial.print(temp); 

    Serial.print(" ");
    if (reading.isData)
    {
        Serial.print("data");
    }
    if (reading.isAnimal)
    {
        Serial.print("animal");
    }
    Serial.println();
  }

};

// instance a Rfid134 object, 
// defined with the above notification class and the hardware serial class
//
//Rfid134<HardwareSerial, RfidNotify> rfid(Serial1);

// Some arduino boards only have one hardware serial port, so a software serial port is needed instead.
// comment out the above definition and uncomment these lines
SoftwareSerial secondarySerial(3, 2); // RX, TX
Rfid134<SoftwareSerial, RfidNotify> rfid(secondarySerial);

void setup() 
{
  Serial.begin(9600);

  Serial.println("initializing...");
   digitalWrite(LED_BUILTIN, HIGH);  // turn the LED on (HIGH is the voltage level)
  delay(1000);                      // wait for a second
  digitalWrite(LED_BUILTIN, LOW);   // turn the LED off by making the voltage LOW
  delay(1000); 
  // due to design differences in (Software)SerialConfig that make the serial.begin
  // method inconsistent between implemenations, it is required that the sketch
  // call serial.begin() that is specific to the platform
  // 
  // hardware
  //Serial1.begin(9600, SERIAL_8N2);
  // software ESP
  //secondarySerial.begin(9600, SWSERIAL_8N2);
  // software AVR
  secondarySerial.begin(9600);
  
  rfid.begin();
 
  Serial.println("starting...");
}

void loop() 
{
  rfid.loop();
  //look for motion
}

You have to use the servo.h library to add servos to the setup.

The three parts of your project are entirely separable.

You've said nothing except you found some code made it work. Please post a link to where the code you began with was found.

Assuming you have only a beginner's skill level, I would suggest to you that writing simple programs for the ultrasonic sensor and the servo would be useful.

A sketch that uses only the ultrasonic sensor and perhaps does nothing more than report the echo distance; probably an example from the library or seller.

Another sketch that does nothing but make a servo sweep back and forth. There is one in the IDE examples

Before you do either of those, learn how to count button presses. One button, sketch just prints to the serial monitor the increased number every time you press the button.

"Incoporating" these disparate functionalities is the program logic, which can be written so it has no idea of where a distance number is coming from, no need to know that a variable going high came from a cat ID sensor and no need to do anything but throw an angle at code that actually talks to the servo.

In fewer words, divide and conquer.

The only part not addressed here is the timing, but that can be the next thing after you are confidant about the separate parts and by which time you will necessarily have learned some things about coding, even if you are only borrowing code, as long as you read the borrowed code to see how it works. Read, study, google and ask questions about the things you can't figure out

If you try and fail at any of the small steps, come here with your attempts and explain the trouble, you will get help.

I just looked and see I have already suggested to you a series of steps that should increase your abilities if successfully taken. I did not look further to see if you did do anything like that, I'll hope you have and leave you to it.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.