I am attempting to create an RFID activated cat door. I have code working to recognize the cat. I also have code driving a stepper motor to open, pause, then close the door. I need help with making the RFID code call the motor driver code. Both have functions within so I am unable to convert the motor driver code to a function. I am pretty much a beginner.
Please post the sketches that work and each do half the job.
It can be easy or challenging to combine sketches, but conceptually at least this shoukd be a straight ahead matter.
Use the <CODE/> tool int the message composition window for each sketch.
If you haven't, it's a good idea to use the IDE Autoformat tool before copying the code there to paste here.
Unless you are happy with the formattting you've found or used yourself.
a7
Can you post the code you´ll need to access with this function?
Thank your for your help.
Here is the first part:
// Program to recognize RFID tag on cat's collar
#include <SPI.h>
#include <RFID.h>
// D10:pin of card reader SDA. D9:pin of card reader RST
RFID rfid(10, 9);
// 4-byte card serial number, the fifth byte is check byte
unsigned char serNum[5];
unsigned char status;
unsigned char str[MAX_LEN]; // MAX_LEN is 16: size of the array
unsigned char blockAddr; // select the operation block address: 0 to 63
// Write card data you want (within 16 bytes)
// unsigned char writeData[16] = "Forbes Pusscat";
// the A password of original sector: 16 sectors; the length of password in each sector is 6 bytes
unsigned char sectorKeyA[16][16] = {
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
};
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.init(); // initialization
Serial.println("Please place the card near the induction area ...");
}
void loop() {
// find the card
rfid.findCard(PICC_REQIDL, str);
// Anti-collision detection, read card serial number
if (rfid.anticoll(str) == MI_OK) {
Serial.print("The card's number is : ");
//Display card serial number
for (int i = 0; i < 4; i++) {
Serial.print(0x0F & (str[i] >> 4), HEX);
Serial.print(0x0F & str[i], HEX);
}
Serial.println("");
memcpy(rfid.serNum, str, 5);
}
// select card and return card capacity (lock card to prevent multiple read, and writing)
rfid.selectTag(rfid.serNum);
//first read the data of data block 4
//readCard(4);
rfid.halt();
//read the card
void readCard(int blockAddr) {
if (rfid.auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[blockAddr / 4], rfid.serNum) == MI_OK) //authenticate
{
//select a block of the sector to read its data
// Recognize the correct RFID tag
Serial.print("Read from the blockAddr of the card : ");
Serial.println(blockAddr, DEC);
if (rfid.read(blockAddr, str) == MI_OK) {
Serial.print("The data is (char type display): ");
Serial.println((char *)str);
Serial.println("space");
Serial.println((char *)str);
if (strcmp(((char *)str), "Forbes Pusscat") == 0) // Compare data read to correct data
{
Serial.println("This is our puss cat!");
motorDrive();
}
}
}
}
}
This is the motor drive part:
void motorDrive() {
int Ts_over4_ms = 20; //ms
const int T2A = 2;
const int T4A = 3;
const int T2B = 4;
const int T4B = 5;
void setup() {
Serial.begin(9600);
pinMode(T2A, OUTPUT);
pinMode(T4A, OUTPUT);
pinMode(T2B, OUTPUT);
pinMode(T4B, OUTPUT);
}
int count = 0; //Initialize count to zero
int endCount = 150;
void loop() {
if (count < endCount) // Drive motor revolutions determined by endCount to open cat door
{
//Mode 1
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms); //delay in ms
//Mode 2
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms);
//Mode 3
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
//Mode 4
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
count = count + 1;
Serial.println(count);
}
else if (count == endCount) // Pause before closing door
{
delay(6000);
count = 151;
Serial.println(count);
}
else if (count < 2 * endCount) // Drive motor revolutions determined by endCount to close cat door
{
//Mode 1R
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms);
//Mode 2R
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms); //delay in ms
//Mode 3R
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
//Mode 4R
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
count = count + 1;
Serial.println(count);
}
}
}
}
Please edit your post. Remove the code and try again to paste it as I described doing
Copy the code from the IDE, and paste it where it tells you to when you use the <CODE>/ tool.
It should end up in one shaded block, looking like this shorter code I just grabbed from elsewhere.
#define BLYNK_TEMPLATE_NAME "{redacted}"
#define BLYNK_AUTH_TOKEN "{redacted}"
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <WiFiClientSecure.h>
char ssid[] = "{redacted}"
char pass[] = "{redacted}"
#include <ESP32Servo.h>
#include <time.h>
#include "ACS712.h"
#include "DHT.h"
#define DHTTYPE DHT22
#define DHTPIN 4 // Temparture and Humididty Sensor of Boost Converter
DHT dht(DHTPIN, DHTTYPE);
BlynkTimer timer;
unsigned long previousMillis = 0;
unsigned long interval = 30000;
const char* host = "script.google.com"; // Host & httpsPort
const int httpsPort = 443;
WiFiClientSecure Client;
const char* fingerprint = "{redacted}";
String GAS_ID = "{redacted}"; //--> spreadsheet script ID
int solarvoltage = 34; // Solar Voltage Sensor
int batteryvoltage = 39; // Battery Voltage Sensor
float correctionfactor = 1.1;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000 + 150000;
float R2 = 7500;
int value = 0.0;
int solarcurrent = 35; // Solar Current Sensor
int batterycurrent = 36; // Battery Current Sensor
ACS712 ACS(35, 5.0, 4095, 66);
double voltage = 0.0;
double current = 0.0;
Serial.print("Average Value Top : ");
Serial.println(avt);
Serial.print("Average Value Down : ");
Serial.println(avd);
Serial.print("Average Value Left : ");
Serial.println(avl);
Serial.print("Average Value Right : ");
Serial.println(avr);
//Serial.print("time diffirence ");
//Serial.println(dtime);
Serial.print("Vertical Servo Position : ");
Serial.println(servov);
Serial.print("Horizontal Servo Position : ");
Serial.println(servoh);
//Serial.print("LDR Value tolerans ");
//Serial.println(tol);
//delay(dtime);
}
void setup()
{
Serial.begin(9600);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
WiFi.begin(ssid, pass);
initWiFi();
dht.begin(); // Initialize the DHT sensor
horizontal.attach(18);
horizontal.attach(16);
horizontal.write(90);
vertical.attach(17);
vertical.attach(5);
vertical.write(90);
pinMode(23,OUTPUT); // Solar Power On Off
pinMode(22,OUTPUT); // Battery Charging Power On Off
}
void loop()
{
timer.run(),
Blynk.run();
}
Also it does not appear as if you either applied the Autoformat tool available in the IDE before you copied it.
Doing both those with both parts will increase attention to your matter.
a7
@sebrof I don´t understand why you´re using a counter and a delay? Why not only a counter or a delay? And using functions like openCatDoor(), pauzeCatDoor() and closeCatDoor(). Much easier to reopen and understand it a few time later.
You can pull up a counter also this way:
count += 1;
Serial.println(count);
// or simply use a delay instead
And why using the if, else if, else if... statement and not using eg:
int i = 0;
do(i++) // or a function and i++
{
// openCatDoor() f.e. as 1st function
while( counter < 150);
}
do(i++){
// wait before closing
while(counter > 149 && counter < 300);
}
do(i++) {
// close door
while(counter > 299 && counter < 450);
}
if(i <= 449){
i = 0;
}
// Or use a switch/case statement?
// Program to recognize RFID tag on cat's collar
#include <SPI.h>
#include <RFID.h>
// D10:pin of card reader SDA. D9:pin of card reader RST
RFID rfid(10, 9);
// 4-byte card serial number, the fifth byte is check byte
unsigned char serNum[5];
unsigned char status;
unsigned char str[MAX_LEN]; // MAX_LEN is 16: size of the array
unsigned char blockAddr; // select the operation block address: 0 to 63
// Write card data you want (within 16 bytes)
// unsigned char writeData[16] = "Forbes Pusscat";
// the A password of original sector: 16 sectors; the length of password in each sector is 6 bytes
unsigned char sectorKeyA[16][16] = {
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
};
void setup() {
Serial.begin(9600);
SPI.begin();
rfid.init(); // initialization
Serial.println("Please place the card near the induction area ...");
}
void loop() {
// find the card
rfid.findCard(PICC_REQIDL, str);
// Anti-collision detection, read card serial number
if (rfid.anticoll(str) == MI_OK) {
Serial.print("The card's number is : ");
//Display card serial number
for (int i = 0; i < 4; i++) {
Serial.print(0x0F & (str[i] >> 4), HEX);
Serial.print(0x0F & str[i], HEX);
}
Serial.println("");
memcpy(rfid.serNum, str, 5);
}
// select card and return card capacity (lock card to prevent multiple read, and writing)
rfid.selectTag(rfid.serNum);
//first read the data of data block 4
//readCard(4);
rfid.halt();
//read the card
void readCard(int blockAddr) {
if (rfid.auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[blockAddr / 4], rfid.serNum) == MI_OK) //authenticate
{
//select a block of the sector to read its data
// Recognize the correct RFID tag
Serial.print("Read from the blockAddr of the card : ");
Serial.println(blockAddr, DEC);
if (rfid.read(blockAddr, str) == MI_OK) {
Serial.print("The data is (char type display): ");
Serial.println((char *)str);
Serial.println("space");
Serial.println((char *)str);
if (strcmp(((char *)str), "Forbes Pusscat") == 0) // Compare data read to correct data
{
Serial.println("This is our puss cat!");
motorDrive();
}
}
}
}
}
This is the motor code. It is basically code from a
"Motor Drive Systems" course I took last semester at the university.
void motorDrive() {
int Ts_over4_ms = 20; //ms
const int T2A = 2;
const int T4A = 3;
const int T2B = 4;
const int T4B = 5;
void setup() {
Serial.begin(9600);
pinMode(T2A, OUTPUT);
pinMode(T4A, OUTPUT);
pinMode(T2B, OUTPUT);
pinMode(T4B, OUTPUT);
}
int count = 0; //Initialize count to zero
int endCount = 150;
void loop() {
if (count < endCount) // Drive motor revolutions determined by endCount to open cat door
{
//Mode 1
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms); //delay in ms
//Mode 2
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms);
//Mode 3
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
//Mode 4
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
count = count + 1;
Serial.println(count);
}
else if (count == endCount) // Pause before closing door
{
delay(6000);
count = 151;
Serial.println(count);
}
else if (count < 2 * endCount) // Drive motor revolutions determined by endCount to close cat door
{
//Mode 1R
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms);
//Mode 2R
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms); //delay in ms
//Mode 3R
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
//Mode 4R
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
count = count + 1;
Serial.println(count);
}
}
}
}
I understand that my code probably is not optimized and I am happy to learn how to do it better, but my real need is to learn how to call one section from the other. Both of these do what I need to get done. Thanks for the help.```
Alto777,
Actually I did use the Auto Format tool when you suggested it. I was not aware of it before. If it did not work as you hoped it may be I used it incorrectly.
And yes, I did also past into the again as you pointed out. Again, I am new at this and haven’t done this before.
Also, I am aware that my code is probably not optimum. The RFID code is from the tutorial with the RFID kit I bought. And the motor code is basically from an university course lab in a Motor Drive Systems course I took recently. In its current form it drives the stepper 3 turns CW, then pauses for 6 seconds to let the cat in, and finally drives the stepper 3 turns in the CCW direction.
What I am really struggling with is how to format the code so that one section can call the other. Once I understand that I can massage the code to make it more elegant.
Thanks for being so helpful.
Murray D. Forbes
ASc, Avionics Engineer
Sebrof Aviation Services Ltd
250-470-8291
Hi @sebrof Welcome to the forum..
having some problems getting that code into them tags i see..
here, formatted it up a bit..
looks like you were trying to combine two sketches..
helped a little there too, didn't try to compile though, don't have the RFID lib..
// Program to recognize RFID tag on cat's collar
#include <SPI.h>
#include <RFID.h>
// D10:pin of card reader SDA. D9:pin of card reader RST
RFID rfid(10, 9);
// 4-byte card serial number, the fifth byte is check byte
unsigned char serNum[5];
unsigned char status;
unsigned char str[MAX_LEN]; // MAX_LEN is 16: size of the array
unsigned char blockAddr; // select the operation block address: 0 to 63
// Write card data you want (within 16 bytes)
// unsigned char writeData[16] = "Forbes Pusscat";
// the A password of original sector: 16 sectors; the length of password in each sector is 6 bytes
unsigned char sectorKeyA[16][16] = {
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
};
int count = 0; //Initialize count to zero
int endCount = 150;
int Ts_over4_ms = 20; //ms
const int T2A = 2;
const int T4A = 3;
const int T2B = 4;
const int T4B = 5;
void setup() {
Serial.begin(9600);
SPI.begin();
pinMode(T2A, OUTPUT);
pinMode(T4A, OUTPUT);
pinMode(T2B, OUTPUT);
pinMode(T4B, OUTPUT);
rfid.init(); // initialization
Serial.println("Please place the card near the induction area ...");
}
void loop() {
// find the card
rfid.findCard(PICC_REQIDL, str);
// Anti-collision detection, read card serial number
if (rfid.anticoll(str) == MI_OK) {
Serial.print("The card's number is : ");
//Display card serial number
for (int i = 0; i < 4; i++) {
Serial.print(0x0F & (str[i] >> 4), HEX);
Serial.print(0x0F & str[i], HEX);
}
Serial.println("");
memcpy(rfid.serNum, str, 5);
}
// select card and return card capacity (lock card to prevent multiple read, and writing)
rfid.selectTag(rfid.serNum);
//first read the data of data block 4
//readCard(4);
rfid.halt();
}
//read the card
void readCard(int blockAddr) {
if (rfid.auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[blockAddr / 4], rfid.serNum) == MI_OK) //authenticate
{
//select a block of the sector to read its data
// Recognize the correct RFID tag
Serial.print("Read from the blockAddr of the card : ");
Serial.println(blockAddr, DEC);
if (rfid.read(blockAddr, str) == MI_OK) {
Serial.print("The data is (char type display): ");
Serial.println((char *)str);
Serial.println("space");
Serial.println((char *)str);
if (strcmp(((char *)str), "Forbes Pusscat") == 0) // Compare data read to correct data
{
Serial.println("This is our puss cat!");
motorDrive();
}
}
}
}
void motorDrive() {
if (count < endCount) // Drive motor revolutions determined by endCount to open cat door
{
//Mode 1
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms); //delay in ms
//Mode 2
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms);
//Mode 3
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
//Mode 4
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
count = count + 1;
Serial.println(count);
}
else if (count == endCount) // Pause before closing door
{
delay(6000);
count = 151;
Serial.println(count);
}
else if (count < 2 * endCount) // Drive motor revolutions determined by endCount to close cat door
{
//Mode 1R
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms);
//Mode 2R
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms); //delay in ms
//Mode 3R
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
//Mode 4R
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
count = count + 1;
Serial.println(count);
}
}
good luck.. ~q
Q, thank you for the welcome and your help.
Actually, I've been able to get the code into the tag, and also to recognize the tag. What I am having trouble doing is then calling the motor drive part of the code in response to a correct reading of the tag.
Unfortunately, we lost the loop needed to run the motor steps.
Your code runs only as far as “Serial.println(count);” after the first “// Mode 4”
The loop was a function “void loop() “ which was what was causing me a problem because I couldn’t figure out how to put this loop into a motor drive function.
Murray
@sebrof you're welcome..
yeah, well you had 2 loops() and 2 setups() and a function declaration inside one of the loops().. again looks like two sketches were tossed together..
no worries, i tried to clean up..
looking at motor drive I'm thinking you want this to loop until count is twice the end count, and no it doesn't do that now, it does exactly as you describe..
I'm not the biggest fan of blocking code, don't like to use delay, but then again this is not my code, so I will leave it and present what I believe will accomplish what you desire, actually very simple change..
// Program to recognize RFID tag on cat's collar
#include <SPI.h>
#include <RFID.h>
// D10:pin of card reader SDA. D9:pin of card reader RST
RFID rfid(10, 9);
// 4-byte card serial number, the fifth byte is check byte
unsigned char serNum[5];
unsigned char status;
unsigned char str[MAX_LEN]; // MAX_LEN is 16: size of the array
unsigned char blockAddr; // select the operation block address: 0 to 63
// Write card data you want (within 16 bytes)
// unsigned char writeData[16] = "Forbes Pusscat";
// the A password of original sector: 16 sectors; the length of password in each sector is 6 bytes
unsigned char sectorKeyA[16][16] = {
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
{ 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF },
};
int count = 0; //Initialize count to zero
int endCount = 150;
int Ts_over4_ms = 20; //ms
const int T2A = 2;
const int T4A = 3;
const int T2B = 4;
const int T4B = 5;
void setup() {
Serial.begin(9600);
SPI.begin();
pinMode(T2A, OUTPUT);
pinMode(T4A, OUTPUT);
pinMode(T2B, OUTPUT);
pinMode(T4B, OUTPUT);
rfid.init(); // initialization
Serial.println("Please place the card near the induction area ...");
}
void loop() {
// find the card
rfid.findCard(PICC_REQIDL, str);
// Anti-collision detection, read card serial number
if (rfid.anticoll(str) == MI_OK) {
Serial.print("The card's number is : ");
//Display card serial number
for (int i = 0; i < 4; i++) {
Serial.print(0x0F & (str[i] >> 4), HEX);
Serial.print(0x0F & str[i], HEX);
}
Serial.println("");
memcpy(rfid.serNum, str, 5);
// select card and return card capacity (lock card to prevent multiple read, and writing)
rfid.selectTag(rfid.serNum);
//first read the data of data block 4
readCard(4);
}
rfid.halt();
}
//read the card
void readCard(int blockAddr) {
if (rfid.auth(PICC_AUTHENT1A, blockAddr, sectorKeyA[blockAddr / 4], rfid.serNum) == MI_OK) //authenticate
{
//select a block of the sector to read its data
// Recognize the correct RFID tag
Serial.print("Read from the blockAddr of the card : ");
Serial.println(blockAddr, DEC);
if (rfid.read(blockAddr, str) == MI_OK) {
Serial.print("The data is (char type display): ");
Serial.println((char *)str);
Serial.println("space");
Serial.println((char *)str);
if (strcmp(((char *)str), "Forbes Pusscat") == 0) // Compare data read to correct data
{
Serial.println("This is our puss cat!");
motorDrive();
}
}
}
}
void motorDrive() {
count = 0;
while (count < endCount*2)
{
if (count < endCount) // Drive motor revolutions determined by endCount to open cat door
{
//Mode 1
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms); //delay in ms
//Mode 2
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms);
//Mode 3
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
//Mode 4
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
count = count + 1;
Serial.println(count);
}
else if (count == endCount) // Pause before closing door
{
delay(6000);
count = 151;
Serial.println(count);
}
else if (count < 2 * endCount) // Drive motor revolutions determined by endCount to close cat door
{
//Mode 1R
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms);
//Mode 2R
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, HIGH);
digitalWrite(T4B, LOW);
delay(Ts_over4_ms); //delay in ms
//Mode 3R
digitalWrite(T2A, HIGH);
digitalWrite(T4A, LOW);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
//Mode 4R
digitalWrite(T2A, LOW);
digitalWrite(T4A, HIGH);
digitalWrite(T2B, LOW);
digitalWrite(T4B, HIGH);
delay(Ts_over4_ms);
count = count + 1;
Serial.println(count);
}
}
}
good luck.. ~q
Q, you are a genius. It works, thank you. Now I will try to understand how you did it. After that I will try to learn better code. In the mean time I will be working on the hardware to keep stray cats and a racoon (seriously) out of my kitchen.
Thank you again.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.