Hi, I'm new to the coding community and through internet examples I've been able to compile a code to make a food container for my cats that opens and closes when they are near, but each one will have a different ID tag, so that they can only access their own food. I have 6 cats with different diets.
I have an arduino uno with a motor shield (L293D), a Pn532 rfid reader and an Ir sensor so it stays open while the cat is there. For the motor I'm using the DC motor within the CD-ROM tray as the lid for the container.
My problem is the motor only runs in one direction and the arduino seems to draw too much power and I need to connect both the arduino and the shield to draw enough power for the motor to move. Please help me with what could be wrong in the code. Since I have already tried a simpler code for the motor and it runs fine in both directions, only with my code it doesn't. Also the BREAK and RELEASE commands, don't seem to work either.
The simple code I used and works fine is this one:
#include <Adafruit_MotorShield.h>
#include <AFMotor.h>
AF_DCMotor motor1(1);
void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps
}
void loop() {
motor1.setSpeed(255);
motor1.run(FORWARD);
delay(5000);//wait for 2 seconds
motor1.run(BRAKE);
delay(100);
motor1.setSpeed(255);
motor1.run(BACKWARD);
delay(4500);
motor1.run(RELEASE);
delay(1000);
}
And my code for the feeder is this one:
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_PN532.h>
#include <Adafruit_MotorShield.h>
#include <AFMotor.h>
#define PN532_SCK (13)
#define PN532_MOSI (11)
#define PN532_SS (10)
#define PN532_MISO (12)
#define PN532_IRQ (2)
#define PN532_RESET (3)
const int irPin = 9;
Adafruit_PN532 nfc(PN532_SCK, PN532_MISO, PN532_MOSI, PN532_SS);
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
AF_DCMotor motor1(4);
uint8_t expectedUID[] = {0xE3, 0xC9, 0x53, 0x14}; // Define the expected RFID card UID
void setup(void) {
Serial.begin(9600);
Serial.println("Hello!");
AFMS.begin(); // start motor shield
nfc.begin(); // start PN532 reader
pinMode(irPin, INPUT);
uint32_t versiondata = nfc.getFirmwareVersion();
if (! versiondata) {
Serial.print("Didn't find PN53x board");
while (1); // halt
}
// Got ok data, print it out!
Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);
Serial.println("Waiting for an ISO14443A Card ...");
}
void loop(void) {
uint8_t success;
uint8_t uid[] = { 0, 0, 0, 0, 0, 0, 0 }; // Buffer to store the returned UID
uint8_t uidLength; // Length of the UID (4 or 7 bytes depending on card type)
success = nfc.readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success) {
// Display the UID
Serial.print("Found an RFID card UID: ");
for (uint8_t i = 0; i < uidLength; i++) {
Serial.print(" 0x"); Serial.print(uid[i], HEX);
}
Serial.println("");
// Check if the UID matches the expected UID
if (uidLength == 4 && memcmp(uid, expectedUID, 4) == 0) {
Serial.println("Correct tag! Opening door");
// Check if the IR sensor detects an object
if (digitalRead(irPin) == LOW) {
Serial.println("cat is there");
// Move the motor for 5 seconds
motor1.setSpeed(255); // Set the speed to maximum
motor1.run(FORWARD); // Rotate the motor forward
delay(5000); // Wait for 5 seconds
motor1.setSpeed(0);
delay(1000);
while(digitalRead(irPin) == LOW){
motor1.setSpeed(0);
Serial.println("cat is there");
}
Serial.println("cat is not there");
Serial.println("Closing door");
motor1.setSpeed(255); // Set the speed to maximum
motor1.run(BACKWARD);
delay(5000);
motor1.setSpeed(0);
} else {
Serial.println("Incorrect tag!");
}
}
}
}
The wiring IĀ“m using is this one
Thanks for any help in advance, my cat's and I are going through a very stressfull moment with the food issue