The RFID will be used as a key for the system. When your pet is close to the feeder, the RFID will read the value from the tag and will decide whether to supply more food or not. The RFID system uses SPI communication and stores the values from the tags in EEPROM memory. In this case, if the system breaks down (e.g. due to a power supply issue), the information will still be in the memory.
For more information on Arduino SPI Communication, please refer back to Arduino Communication Protocols Tutorial.
For the RFID, the following libraries must be added:
Figure 13: Wire diagram with RDIF added
Figure 14: Connection between RFID and the rest of the components
We have 2 RFID tags. The red one will be attached to a pet and the blue one, for the sake of testing, will be attached to an outsider (i.e. basically not your pet/something else). The system has 2 functions:
Figure 15: Tags assigned shown on serial monitor
We’ll be using servo motor SG90. It has an wide angle for servo (0-180 degrees). Our locking system will be similar to lock controlled by an angle (we will control how much food is released when the “lock” is opened/unlocked).
Here are some important points:
Figure 16: Wire diagram for the whole project
Before we can talk about the programming of the motor, we need to build support for the feeder. Now is a good time to look into the mechanical part of the feeder. We’ll need the following material:
Figure 17: Bottle (food dispenser) attached to the metal plate using two wires
The “lock” system can’t stand in the air so we need to fix it with a piece of hard material. This provides a nice opening of the food dispenser. This also needs to be fixed with drills or tape it to the metal plate, so it doesn’t collapse in case you put a lot of food in the dispenser. As you can see in the picture below, the metal part is bent on the outer edge in order to prevent the misalignment of the lock.
Figure 18: How to position the stand
Figure 19: Finding the right place to drill a hole to wire to the motor
You’re pretty much done at this point. All you need to do is to fix the feeder wherever you wish, preferably in a safe place where your pet can’t demolish the feeder.
Figure 20: Catching the motor near the stand
For better precision, it’s recommended that you don’t curve wire that connects the food dispenser cover to the motor because otherwise, it can diminish the force of the motor.
Figure 21: Complete pet automatic feeder
Figure 22: The final product
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
#include <Wire.h> #include <Time.h> #include <TimeLib.h> #include <DS1307RTC.h> #include <Servo.h> #include <EEPROM.h> #include <SPI.h> #include <MFRC522.h> #define SS_PIN 10 #define RST_PIN 9 Servo myservo; boolean match = false; boolean programMode = false; boolean replaceMaster = false; int lightSensor = 0; int distanceSensor=1; int pos = 0; int successRead; byte storedCard[4]; byte readCard[4]; byte masterCard[4]; MFRC522 mfrc522(SS_PIN, RST_PIN); void setup() { Serial.begin(9600); setSyncProvider(RTC.get); myservo.attach(9); Serial.begin(9600); SPI.begin(); mfrc522.PCD_Init(); if (EEPROM.read(1) != 143) { do { successRead = getID(); } while (!successRead); for ( int j = 0; j < 4; j++ ) { EEPROM.write( 2 + j, readCard[j] ); } EEPROM.write(1, 143); } for ( int i = 0; i < 4; i++ ) { masterCard[i] = EEPROM.read(2 + i); Serial.print(masterCard[i], HEX); } } void loop() { int valueFromLightSensor = analogRead(lightSensor); int valueFromDistanceSensor = analogRead(distanceSensor); int distance= 4800/(valueFromDistanceSensor - 20); Serial.println(distance); do { successRead = getID(); } while (!successRead); if (programMode) { if ( isMaster(readCard) ) { programMode = false; return; } else { if ( findID(readCard) ) { } } } else { if ( isMaster(readCard)) { programMode = true; int count = EEPROM.read(0); } else { if ( findID(readCard) ) { if ((hour()>=8) && (hour()<=12 )){ if (distance>=20){ for(pos = 130; pos>=1; pos-=1) { myservo.write(pos); delay (20); } for(pos = 50; pos < 180; pos += 1) { myservo.write(pos); delay(20); } } delay(10000); } if ((hour()>=12) && (hour()<=16 )){ if (distance>=20){ for(pos = 130; pos>=1; pos-=1) { myservo.write(pos); delay (20); } for(pos = 50; pos < 180; pos += 1) { myservo.write(pos); delay(20); } } delay(10000); } if ((hour()>=0) && (hour()<=8 )){ if (distance>=20){ for(pos = 130; pos>=1; pos-=1) { myservo.write(pos); delay (20); } for(pos = 50; pos < 180; pos += 1) { myservo.write(pos); delay(20); } } delay(20000); } if ((hour()>=16) && (hour()<=20 )){ if (distance>=20){ Serial.println(distance); for(pos = 130; pos>=1; pos-=1) { myservo.write(pos); delay (20); } for(pos = 50; pos < 180; pos += 1) { myservo.write(pos); delay(20); } } delay(10000); } } } } } int getID() { if ( ! mfrc522.PICC_IsNewCardPresent()) { return 0; } if ( ! mfrc522.PICC_ReadCardSerial()) { return 0; } Serial.println(F("Scanned PICC's UID:")); for (int i = 0; i < 4; i++) { // readCard[i] = mfrc522.uid.uidByte[i]; Serial.print(readCard[i], HEX); } Serial.println(""); mfrc522.PICC_HaltA(); return 1; } void readID( int number ) { int start = (number * 4 ) + 2; for ( int i = 0; i < 4; i++ ) { storedCard[i] = EEPROM.read(start + i); } } boolean checkTwo ( byte a[], byte b[] ) { if ( a[0] != NULL ) match = true; for ( int k = 0; k < 4; k++ ) { if ( a[k] != b[k] ) match = false; } if ( match ) { return true; } else { return false; } } int findIDSLOT( byte find[] ) { int count = EEPROM.read(0); for ( int i = 1; i <= count; i++ ) { readID(i); if ( checkTwo( find, storedCard ) ) { return i; break; } } } boolean findID( byte find[] ) { int count = EEPROM.read(0); for ( int i = 1; i <= count; i++ ) { readID(i); if ( checkTwo( find, storedCard ) ) { return true; break; } else { } } return false; } boolean isMaster( byte test[] ) { if ( checkTwo( test, masterCard ) ) return true; else return false; } |