Final Report Automatic Pet Feeder Using Radio Frequency Technology ECE4884 Senior Design Project Section L02, Fauler Hund Team Robert Fleming Kevin Clark Viet Nguyen Vu Tang Vishak Ganesh Submitted December 7, 2007 Fauler Hund (ECE4884L02) i TABLE OF CONTENTS Executive Summary..........................................................................................................iii 1. Introduction ................................................................................................................. 1 1.1 1.2 1.3 Objective ............................................................................................................. 1 Motivation ........................................................................................................... 1 Background ......................................................................................................... 1 2. Project Description and Goals .................................................................................... 2 3. Technical Specifications .............................................................................................. 3 4. Design Approach and Details………………………………………………………..4 4.1 Design Approach .................................................................................................. 4 4.2 Codes and Standards........................................................................................... 11 4.3 Constraints, Alternatives, and Tradeoffs ............................................................ 11 5. Schedule, Tasks, and Milestones .............................................................................. 12 6. Project Demonstration .............................................................................................. 13 6.1 Overview ............................................................................................................ 13 6.2 Project Demo Instructions .................................................................................. 13 7. Marketing and Cost Analysis ................................................................................... 14 7.1 Marketing Analysis ............................................................................................ 14 7.2 Cost Analysis ...................................................................................................... 15 8. Summary and Conclusions ....................................................................................... 17 9. References .................................................................................................................. 18 Appendix A - Schematics Appendix B – Parts List Appendix C – Source Code Fauler Hund (ECE4884L02) ii EXECUTIVE SUMMARY There are numerous automatic pet feeding devices on the market. Our prototype (the FH-418) is a modification of an existing pet feeder on the market. The FH-418 allows the pet to better regulate its feeding cycle, resulting in more free time for pet owners. The long term objective is to make FH-418 a common house-hold product. The pet feeder is controlled by a microcontroller. A transmitter, located around the pet’s neck, sends a binary code to the receiver. This numerical identifier will be hard coded into the microcontroller. The numerical identification feature will allow the pet feeder to identify a particular pet. Once this ID is verified, the pet feeder dispenses a certain amount of food. The prototype is made up of 5 major components that were successfully integrated These components are the transmitter, receiver, microcontroller, relay, and feeder. However, during the microcontroller implementation phase, integrating the Cypress with the rest of the components proved to be more difficult than expected. This problem was due to a lack of technical support and adequate software documentation. To resolve this issue, the PIC was chosen to replace the Cypress microcontroller. Additional work that could improve the project include increasing the auger speed to dispense food faster, increasing the battery life from its original design of three months to approximately six months, proper packaging of chip components, and a more user-friendly interface. All of these improvements would make the product more marketable. Fauler Hund (ECE4884L02) iii Automatic Pet Feeder Using Radio Frequency Technology 1. INTRODUCTION Over half of American households have pets [1]. Feeding these pets can sometimes be time consuming. FH-418 solves this issue by reducing the amount of time the pet owner spends on feeding the pet. 1.1 Objective The automated pet feeder allows pet owners to conveniently feed their pets. Reliable automatic pet feeders have the potential to ease the schedules of busy pet owners. Existing timer based automatic feeders on the market are improved by adding a pet identification system. The system’s main components consist of the timer, a wireless interface, and a feeding mechanism. When a pet approaches the food bowl at certain times, the pet feeder dispenses its contents. 1.2 Motivation The project modifies an existing product that will save pet owners time and money. The automated pet feeder introduces the additional feature of pet identification. This feature tailors to the needs of the pet and the pet owner’s lifestyle, providing convenience and reliability. 1.3 Background Automatic feeders on the market feed pets on a preset schedule [2]. However, these timer based systems do not take into account the needs of the pet. This issue can lead to wasted food or the food being eaten by another animal, in the case of an outdoor feeder. However, FH418 will resolve the issue by only feeding a pet with a specific numerical ID. Fauler Hund (ECE4884L02) 1 Examples of previous products include timer-based pet feeders and gravity-fed feeders. The gravity feeder lacks any moving parts, is relatively inexpensive, and dispenses food continually. This type of feeder has many disadvantages: the pet may be encouraged to overeat; other pets may eat the food; and food will not remain fresh. Timer based feeders are more expensive and dispense set amounts of food at specified periods of the day [2]. Although this mechanism addresses the issue of overeating, it neglects the problem of keeping food fresh and the issue of other animals eating the pet’s food. This project, however, solves all of these problems by using a transmitter and receiver between the pet’s collar and the feeder. By recognizing that the pet is within close proximity, the dispenser releases a predetermined amount of fresh food. 2. PROJECT DESCRIPTION AND GOALS The prototype pet feeder uses a numerical identification feature based on RF technology. The goal of the pet feeder is to automatically feed a particular pet when it comes up to the bowl. In addition, the feeder employs a timer to control the amount of food dispensed to prevent overeating. The product’s final selling price is estimated to be $165. This price is $10 to $85 more than similar food dispensers in the market. However, this difference in cost can be attributed to better technology. As the component costs of the feeder decreases, the product becomes more affordable to a wider range of pet owners. The functionalities of our product include the following: Automates feeding Dispenses preset amounts of food Recognizes specific pets via collar transmitter Fauler Hund (ECE4884L02) 2 Holds a reservoir of food Requires low maintenance Prevents the pet from overeating Prevents food being eaten by other animals These features have all been successfully implemented in the final design. However, future improvements could include increasing the food dispensing rate, the battery life, and improving user-friendliness by adding a LCD interface. 3. TECHNICAL SPECIFICATIONS Aspect of Design Transmitter/Receiver range Time between transmitter pings Transmitter weight worn by pet Identifier Code Transmitter size Transmitter battery life Transmitter power supply Receiver power supply Feeder mechanism Container volume Timer Transmitter/Receiver Desired Specifications 3 to 6 ft < 2 minutes < 6 ounces 4 bit binary code Does not impede pet 6+ months Small cell battery Standard household AC Motor powered Size based on pet breed Accurate to a minute IR/RF Technology Actual Specifications 9 ft 10 seconds < 6 ounces 10 bit tristate binary code Does not impede pet 1.5 months 3-V Coin cell battery Standard household AC DC Motor Size based on pet breed Accurate to a minute RF Technology Most of the desired specifications have been met. However, the transmitter weight would increase following packaging. Currently the weight of the transmitter is within the 6 ounce limit. The antenna would also have to be modified to have a range shorter than 9 feet. In addition, the battery life will have to be increased in order to achieve the desired 6 month lifespan. Lastly, the speed of the auger will have to be improved. Fauler Hund (ECE4884L02) 3 4. DESIGN APPROACH AND DETAILS 4.1 Design Details The FH-418 pet feeder is a modification of the Ergo AutoPetFeeder. The major modification centers around the replacement of the old timer based controller with a new one that uses a microcontroller and wireless link with a pet collar. This project created a prototype device consisting of five main components, all of which are listed and summarized in Figure 1. The FH-418 prototype pet feeder modifies the Ergo AutoPetFeeder product with its timer removed. A hardware block diagram of the components used for the project is shown below. Figure 1. Overall block diagram A short range radio transmitter that is fitted around the pet’s collar functions as a wireless tracking device. This radio transmitter is the TXE-418-KH2 chip sold by Linx Technologies. It is a single unit that includes all the necessary radio frequency and encoding circuitry to wirelessly transmit the value through ten data lines to an appropriate receiver. The antenna is the only Fauler Hund (ECE4884L02) 4 external RF component. For this project's implementation, the receiver is the RXD-418-KH2 chip and acts as a matching component to the transmitter. The pet feeder is primarily controlled by the PIC18F2321 microcontroller chip. This chip determines the length of time that the auger spins, which dictates the amount of food dispensed. The feeder itself is turned on and off by a relay circuit, which is powered by a standard 120V outlet and controlled by the microcontroller. The pet feeder's tracking device is separate from the rest of the system, and communicates via encoded data packets transmitted on the 418 MHz radio band. All other components are located on the pet feeder itself, and communicate using the connections described in Figure 2. Figure 2. High level interconnects Connection C2 is from the valid transmission pin of the RXD chip to an input pin of the PIC. Since the transmitter and receiver are operating simply as a detection method, only the Fauler Hund (ECE4884L02) 5 reception of a signal is necessary. Connection C3 is from an output pin of the PIC to a combination relay and buffer circuit which drives C5, the 120 Volt AC household current. The General operating method for the FH-418 is described in the flowchart of Figure 3. Figure 3. Operational flowchart The basic component layout shows the feeding mechanism, the transmitter and receiver, the PIC18F2321 microcontroller, and the relay. The moment the receiver detects a signal from the transmitter, the valid transmission pin on the receiver goes high. The PIC microcontroller receives this signal and executes the C code which determines the feeding duration. For the relay to be started, the output of a comparator used must be about 5V. The motor then starts the auger, causing the feeding dispenser to release the appropriate amount of food. A detailed image of the transmitter is shown on the next page in Figure 4. This chip consists of a RF transmitter with an on-board encoder. The default state of the pet feeder is when the RXD chip is actively checking for a valid transmission signal. Once a valid Fauler Hund (ECE4884L02) 6 transmission has been detected, the amount of food that is dispensed will be based on the total time elapsed since the previous feeding cycle. In addition, the dip switch setting selected by the user also affects the amount of food that is dispensed. Once pet has finished feeding, there is a 10 minute delay before the microcontroller renews the detection cycle. Figure 4 displays the transmitter. On the upper left is the TXE chip with a helical antenna linked to it. In order to create a proper short range radio device, the TXE chip needs to be altered in ways. For decreasing the range of the transmitter, a helical antenna has been added. In addition, a potentiometer is used to reduce the total output power. Figure 4. Transmitter board and components The TXE chip features ten tri-state address lines for matching transmitter and receiver pairs. For this application, we use a ten- switch piano dip switch package to swap the address lines from logical high to high impedance. The transmitter uses a three volt coin cell battery in order to allow the transmitter to be attached to a pet collar without using a bulky battery. In Fauler Hund (ECE4884L02) 7 order to improve battery life, the transmitter is controlled with a timer circuit that cycles it on and off at regular intervals. The prototype uses a 555 timer chip to generate a low duty cycle square wave that drives the transmit enable pin of the TXE chip. In order to follow the transmitter's on off cycle, a small LED is connected to the output of the 555 timer to give visual confirmation. This prototype transmitter is assembled according to the schematic detailed in Appendix A. Figure 5. Receiver board and components. The RXD chip on the bottom left, with the use of the helical antenna, receives a radio transmission from the transmitter. The helical antenna on the receiver serves to further reduce the effective range of the transmitter and receiver pair. Testing in the lab indicated an effective range of about 3-8 feet depending on antenna orientation. A ten dip switch package located on the receiver board is connected to the tri-state address lines as found in the transmitter. When these address lines match those on the transmitter, the valid transmission will go high when the transmitter's signal is detected. The receiver board also has a voltage regulator that regulates the Fauler Hund (ECE4884L02) 8 voltage from a standard five volt AC power adapter. This voltage also powers the PIC microcontroller board in Figure 6. Figure 6. PIC board and components. The PIC18F2321 microprocessor, serial adapter for programming the PIC, and a comparator circuit that drives the relay is displayed in Figure 6. The PIC operates from a three volt dc source which can be changed to a five volt source in a final product. This PIC serves as the central control unit of the auto pet feeder. The dip switch (found on the top left hand corner) can be used by the pet owner to control the amount of food that is dispensed by the feeder. The receiver and PIC boards are built according to the schematic in Appendix A. Lastly, the feeder is displayed in Figure 7. Fauler Hund (ECE4884L02) 9 Figure 7. Pet feeder mechanism The Pet Feeder shown in Figure 7 is a relatively primitive, consisting of a reservoir that holds the food and an auger that pushes it food out. When supplied with an appropriate current, the auger turns, forcing the pet food out of the spout in the front. Since the motor runs whenever the unit is supplied with power, a control relay was needed for regulating the amount of food dispensed. Figure 8. Motor controller Fauler Hund (ECE4884L02) 10 Figure 8 contains the circuit that controls the pet feeder's motor. The relay allows a small DC current to control the much larger AC current utilized by the feeder motor. A buffer BJT and the shunt diode prevents the large inductance present in the magnetic mechanism of the relay from discharging into the microcontroller's output pin and allows the relay's coil to discharge completely when not powered. 4.2 Codes and Standards Complying with Part 15 of the FCC rules which states guidelines for RF devices such as general technical and labeling requirements will be required [3]. As a result, production costs will increase due to adhering to these rules. The feeder will use an American 3-pin/Type B/NEMA 5-15 power plug and will follow guidelines for its design and use [4]. 4.3 Constraints, Alternatives, and Tradeoffs Some other design alternatives focused on the wireless link. RFID was initially considered, but due to the high cost of RFID readers and a desire to make this pet feeder affordable, RFID was rejected in favor of a simpler link [5, 6]. Motion detection allows for detection of an animal’s presence, but does not distinguish between animals. For the transmitter, we initially chose a Cypress Microcontroller, but due to the lack of documentation, a PIC microcontroller was chosen as an alternative. This PIC microcontroller stays in a low-power mode that sends out a signal once a minute. This method allows the pet feeder to be programmed to wait for two consecutive pulses. As a result, a pet randomly passing by the feeder does not activate the food dispenser. Another constraint is the range of the transmitter/receiver. By having the range relatively small, triggering only occurs when the pet is close enough to the bowl. Fauler Hund (ECE4884L02) 11 5. SCHEDULE, TASKS, AND MILESTONES Gantt Chart of Projected Schedule Fauler Hund (ECE4884L02) 12 The Gantt chart on the previous page presents the planned schedule of the project. All aspects of the schedule were successfully completed, though minor variations in dates arose. Integration of the microcontroller with the transmitter and receiver was the most difficult as coding was required and members had little to no experience in hardware coding. Robert Fleming led the hardware aspect of the project, specifically, the transmitter and receiver integration. Kevin Clark was the webmaster and supported the microcontroller testing. Vishak Ganesh tested functionality of the system. Viet Nguyen led the software aspect of the microcontroller. Vu Tang tested in hardware functionality and assisted in the component integration. All group members were involved with testing at each stage. 6 PROJECT DEMONSTRATION 6.1 Overview The block diagram displayed in Figure 1 was successfully implemented in the demonstration. All of the design goals set at the beginning of the project were achieved. The transmitter emitted RF signals at consistent intervals without any complications. The microcontroller effectively executed the project firmware upon receiving the valid transmission signal from the receiver. Finally, the relay and feeder responded successfully to the output sent from the microcontroller. 6.2 Project Demo Instructions 1. Set up the receiver, microcontroller, relay, and feeder as shown in Appendix A 2. Ensure that the VDD and GND pins of the receiver and microcontroller are connected to identical pins. 3. Ensure that the valid transmission pin on the receiver is connected to pin 3 on the microcontroller Fauler Hund (ECE4884L02) 13 4. Connect pin 7 of the microcontroller to the input of the comparator 5. Connect the blue wire to the output of the comparator. In addition, connect the black wire to voltage and the green wire to ground. (this step is crucial since the relay controls the feeder using this output) 6. Bring the transmitter close to the receiver and begin demonstration 7. Refer to the schematic in the Appendix A for further clarification PUT PICTURE OF DEMONSTRATION 7. MARKETING AND COST ANALYSIS 7.1 Marketing Analysis The primary objective of our device is to increase the ease with which the pet owner can feed the pet. The pet feeder is especially appropriate for busy pet owners. The most advanced pet feeders in the market today are based on timer technology. This project, however, takes the next step towards automation by adding a sophisticated timer and a short range RF sensor. The feeder dispenses food only when the pet is near the device. Many of the feeders on the market are priced in the range of $80 to $135 [2]. Our product will be priced at around $165. So for a marginally higher cost, the pet feeder includes newer technology that makes the pet feeding process more convenient for owners. In addition, the pet feeder only releases a certain amount of food when the pet is near and stops soon afterwards. This process ensures that the food is fresher as it remains sealed for the majority of the time. There are currently an estimated 140 million dogs and cats in the United States [1]. The households that own these 140 million domestic pets will be our primary market. By using short range RF technology, this pet feeder will gradually become a viable and desired substitute for timer based feeders. Fauler Hund (ECE4884L02) 14 7.2 Cost Analysis Table 1 below shows a cost analysis of parts necessary to build the prototype pet feeder. Item Dog Feeder Microcontroller Relay Motor Sensors (Tran/Rec) Encoder/Decoder Power Supply Bowl Container Base Dispenser Table 1 Automated Pet Feeder Cost Qty Cost Total 1 $100 2 $15 1 $2 1 $5 2 $4 2 $3 1 $10 1 $3 1 $3 1 $3 1 $3 Misc Parts $100 $30 $2 $5 $8 $6 $10 $3 $3 $3 $3 10% $17 Total $190 The initial construction cost of the prototype will be higher than final production cost because certain materials will be purchased directly from retailers. Each team member is expected to work an average of 8 hours a week. Half of weekly labor time was spent in class lectures and group meetings on progress and issues, and the other half was spent on direct project work and documenting design implementation. The team members spent a total of approximately 64 hours per member to complete this project. At a rate of $50/hour for each engineer, we see a total development cost of $16,000. We project sales figures totaling upwards of 300,000 units per year for a total of 1,500,000 units over a five year period. This total represents approximately one percent of the estimated 140 million pets in the United States [1]. The total cost of production will be $9 for each unit. $6 will be spent on the assembly line, and $3 will be spent on testing. Table 2 below Fauler Hund (ECE4884L02) 15 shows the accumulation of total cost from initial development of the prototype to mass production. Table 2 Development Cost and Profit Development Cost (Non-recurring Cost) What it costs the company to develop the product Parts Labor Fringe Benefits, % of Labor Subtotal Overhead, % of Matl, Labor & Fringe Total 190 17,925 4,481 22,956 12,626 $35,582 Determination of Selling Price What the customer pays the company for the finished product Based on: 1,500,000 Units Parts Cost Assembly Labor Testing Labor Total Labor Fringe Benefits, % of Labor Subtotal Overhead, % of Matl, Labor & Fringe Subtotal, Input Costs Sales & Marketing Expense Warranty & Support Expense Amortized Development Costs Subtotal, All Costs Profit Selling Price Total Revenue Total Profit 50 6 3 9 2 61 34 95 41 8 0 144 21 12.40% $165 $247,500,000 $30,781,601 Our group projects a 12.40% profit margin over a five year period at a selling price of about $165 per each unit. This selling price will be competitive compared to similar products such as Petmate’s Le Bistro and ERGO System’s Autopetfeeder that sell at roughly $130 [2, 7]. Fauler Hund (ECE4884L02) 16 8. SUMMARY AND CONCLUSIONS The primary objectives outlined in the beginning of the project were to build a pet feeder that is cost effective and convenient. The prototype achieves these goals. At the beginning of the project, RFID technology seemed to be a feasible way to implement the project. However, due to the high cost of RFID technology, the primary design goals needed to be modified. Standard RF technology was chosen as the best alternative to RFID. After completion of the prototype, RF technology was determined to be both cost effective and easy to implement. Several improvements are critical for the prototype to become a marketable product. These improvements include increasing the battery life, increasing the speed of the auger, and packaging the transmitter, receiver, and microcontroller. Since the prototype passed all preliminary testing, only the improvements listed above will be required to make it more marketable. FH-418 is a perfect fusion of technological prowess and simplicity of design. Fauler Hund (ECE4884L02) 17 9. REFERENCES [1] Pet Food Institute “Pet Population Data” [Online Document], 2006, [cited 2007 September 16], Available HTTP: http://www.petfoodinstitute.org/reference_pet_data.cfm [2] “Petmate Le Bistro Electronic Portion Control Dog Feeder” [Online Catalog], [cited 13 September 2007], Available HTTP: http://www.petsmart.com/product/index.jsp?productId=2751238&cp=&sr=1&origkw=dog+fee der&kw=dog+feeder&parentPage=search&keepsr=1 [3] Federal Communications Commission “Part 15 Regulations” [Online Catalog], 2007 May 4 [cited 2007 September 14], Available HTTP: http://www.fcc.gov/oet/info/rules/part15/part155-4-07.pdf [4] Wikipedia Contributors “NEMA Connector” [Online Encyclopedia], 2007 September 4 [cited 2007 September 14], Available HTTP: http://en.wikipedia.org/wiki/NEMA_connector [5] ZietControl Cardsystems GmbH “Online Order > Hardware” [Online Catalog], 2007 August 7, [Cited 2007 September 14], Available HTTP: https://ssl14.pair.com/chippy/rfidrdr_or.php [6] Avid Wireless “RFID” [Online Catalog], 2005 April 27, [Cited 2007 September 14], Available HTTP: http://www.avidwireless.com/AVIDCart/scripts/index.php?main_page=index&cPath=6 [7] The Pampered Pet Mart “Feeding and Watering Products” [Online Catalog], [cited 13 September 2007], Available HTTP: http://www.thepamperedpetmart.com/Merchant2/merchant.mvc?Screen=PROD&Product_ Code=LAPF&Affiliate=nextag Fauler Hund (ECE4884L02) 18 Appendix A - Schematics Fauler Hund (ECE4884L02) A1 Receiver, Relay, and Microcontroller Circuit Fauler Hund (ECE4884L02) A2 Appendix B – Parts List Fauler Hund (ECE4884L02) B1 Appendix C – Source Code /******* Template.c ************************************************************ * * Template program for Design Project. * Use Fosc = 4 MHz for CPU clock of Fosc/4 = 1 MHz (on pin 10) * Toggle RC2 output every 16 milliseconds for measuring looptime with scope. * Blink "Alive" LED on RB6 every four seconds. * Check pushbutton and turn on "Alive" LED continuously while it is pressed. * Send initial message to PC. * Use 16 ms (nominal) watchdog timeout for looptime. * * Current draw = 3 uA (with LED removed) * ******* Program hierarchy ***************************************************** * * Mainline * Initial * PCdisplayC * TXbyte * BlinkAlive * Pushbutton * LoopTime * *********************************************************************** ******** */ #include <p18f2321.h> /******************************* * Assembler directives ******************************* */ #pragma config OSC = INTIO2 RA7=I/O #pragma config BOR = SOFT #pragma nominal #pragma #pragma #pragma nominal #pragma #pragma #pragma config BORV = 2 config PWRT = ON config WDT = OFF config WDTPS = 4 config MCLRE = ON config PBADEN = DIG config LVP = OFF Fauler Hund (ECE4884L02) // Use internal osc, RA6=Fosc/4, // Brown-out reset enabled/disabled in // software // Brown-out voltage set for 2.7V, // Disable watchdog timer initially // 16 millisecond WDT timeout period // PORTB<4:0> = digital C1 /******************************* * Definitions and equates ******************************* */ /******************************* * Global variables ******************************* */ unsigned int ALIVECNT; LED unsigned unsigned unsigned unsigned unsigned unsigned unsigned unsigned unsigned unsigned unsigned // Counter for blinking "Alive" int FEEDCOUNT; char NEWPB; char OLDPB; long TOTALCOUNTS; long FEEDAMOUNT; long WAITPERIOD; long DIPVALUE; int RIGHTPATH; int a0, a1, a2, a3, a4, a5, a6, a7, x, TEN; long TIMEDIFF; long XD; /******************************* * Constant strings ******************************* */ const char rom MESSAGE[] = "Eat at Joe's\r\n"; const char rom NEWMESSAGE[] = "Running\r\n"; /******************************* * Variable strings ******************************* */ /******************************* * Function prototypes ******************************* */ void void void void void void Initial(void); PCdisplayC(const char rom *); TXbyte(char); BlinkAlive(void); Pushbutton(void); LoopTime(void); void CountDown(void); void DipSwitch(void); void FeedTime(void); void InputSignal(void); /////// Mainline program //////////////////////////////////////// Fauler Hund (ECE4884L02) C2 /******************************* * main() ******************************* */ void main() { Initial(); // Initialize everything while (1) { PORTCbits.RC2 = !PORTCbits.RC2; if(RIGHTPATH == 0) { BlinkAlive(); //Pushbutton(); InputSignal(); } else if(RIGHTPATH == 1) { FeedTime(); } else { CountDown(); } /* if(PORTAbits.RA1){ PORTAbits.RA7 = 1; }else { PORTAbits.RA7 = 0; } */ LoopTime(); // Blink "Alive" LED // Set for 16 ms by watchdog timer } } /******************************* * Initial() * * This subroutine performs all initializations of variables and registers. ******************************* */ void Initial() { RCONbits.SBOREN = 0; disable OSCCON = 0b01100010; CPU clock) ADCON1 = 0b00001111; TRISA = 0b00010011; Fauler Hund (ECE4884L02) // Start up with brown-out reset, then // Use 4 MHz internal oscillator (1 MHz // Make all ADC/digital pins digital // Set I/O for PORTA C3 TRISB = 0b00111111; // Set I/O for PORTB TRISC = 0b11000000; // Set I/O for PORTC (set as input by openi2c) PORTA = 0; ALIVECNT = 0; // Initialize the BlinkAlive variable //////////////////////////////////////////////////// FEEDCOUNT = 0; // Count for how long between last feeding time FEEDAMOUNT = 0; // Count for how long to feed WAITPERIOD = 0; // Count for 2 hour delay DIPVALUE = 0; // Actual feeding period RIGHTPATH = 0; a0 = 0; a1 = 0; a2 = 0; a3 = 0; a4 = 0; a5 = 0; a6 = 0; a7 = 0; /*PORTAbits.RA7 = 0; PORTBbits.RB0 = 1; PORTBbits.RB1 = 1; PORTBbits.RB2 = 1; PORTBbits.RB3 = 1; PORTBbits.RB4 = 0; PORTBbits.RB5 = 0; PORTAbits.RA0 = 0; PORTAbits.RA1 = 0;*/ x = 0; INTCON2bits.RBPU = 0; PORTAbits.RA1 = 0; TIMEDIFF = 0; XD = 0; TEN = 0; ///////////////////////////////////////////////////// SPBRG = 51; // set up UART SPBRGH = 0; NEWPB = 0; OLDPB = 0; TOTALCOUNTS = 0; RCSTA = TXSTA = SPBRG = BAUDCON 0b10010000; 0b00100000; 12; = 0b00111000; // // // // Enable UART Enable TX Set baud rate Invert TX output PCdisplayC(MESSAGE); PIE1bits.TMR1IE = 1; TMR1H = 0xFF; TMR1L = 0x00; T1CON = 0b01001111; PIR1bits.TMR1IF = 0; INTCONbits.GIEL = 1; Fauler Hund (ECE4884L02) // // // // // // Enable local interrupt source Initial value Timer1 runs from 32768 Hz oscillator Clear Timer1 flag Enable wake-up from sleep C4 // WDTCONbits.SWDTEN = 1; } // Enable watchdog timer /******************************* * PCdisplayC(const char *) * * This function sends a constant string to the PC. ******************************* */ void PCdisplayC(const char rom * strPtr) { while ((*strPtr) != 0) // send all bytes until zero { TXbyte(*strPtr++); } } /******************************* * TXbyte(char) * * Sends out a character to the terminal ******************************* */ void TXbyte(char in) { while (!PIR1bits.TXIF) ; TXREG = in; } /******************************* * BlinkAlive() * * This subroutine briefly blinks the LED every four seconds. * With a looptime of about 10 ms, count 200 looptimes ******************************* */ void BlinkAlive() { PORTAbits.RA7 = 0; // Turn off LED if (++ALIVECNT >= 100) // Decrement counter and return if not zero { ALIVECNT = 0; // Reinitialize ALIVECNT // 10.0 ms * 200 counts = 2000 ms PORTAbits.RA7 = 1; // Turn on LED for 10 ms every 2 secs //... } } /******************************* * Pushbutton() * Fauler Hund (ECE4884L02) C5 * This subroutine, called from the mainline loop, after the BlinkAlive call, * echos the state of the pushbutton on the LED. ******************************* */ void Pushbutton() { ++FEEDCOUNT; ++TIMEDIFF; PORTAbits.RA5 = 1; // Power up the pushbutton Nop(); // Delay one microsecond before checking it NEWPB = !PORTAbits.RA4; // Set flag if pushbutton is pressed PORTAbits.RA4 = 0; // Power down the pushbutton if (!OLDPB && NEWPB) // Look for last time = 0, now = 1 { //save time and restart timing: TOTALCOUNTS = FEEDCOUNT; FEEDCOUNT = 0; if(TOTALCOUNTS/100 > 10) { RIGHTPATH = 1; FEEDAMOUNT = 0; XD = TIMEDIFF/100; TIMEDIFF = 0; DipSwitch(); } } OLDPB = NEWPB; // Save present pushbutton state } void InputSignal() { ++FEEDCOUNT; ++TIMEDIFF; Nop(); if(PORTAbits.RA1 == 1) { while(++TEN < 1000) { PORTCbits.RC2 = !PORTCbits.RC2; LoopTime(); ++TIMEDIFF; } if(PORTAbits.RA1 == 1) { TOTALCOUNTS = FEEDCOUNT; FEEDCOUNT = 0; RIGHTPATH = 1; FEEDAMOUNT = 0; TEN = 0; WAITPERIOD = 0; XD = TIMEDIFF/100; TIMEDIFF = 0; Fauler Hund (ECE4884L02) C6 DipSwitch(); } } } /******************************* * LoopTime * * This function puts the chip to sleep, to be awakened by Timer1 rollover. ******************************* */ void LoopTime() { while(!TXSTAbits.TRMT); //wait for transmissions to finish Sleep(); Nop(); T1CONbits.TMR1ON = 0; // Pause Timer1 counter TMR1L += 0xB9; // Cut out all but 328 counts of Timer1 T1CONbits.TMR1ON = 1; // Resume Timer1 counter TMR1H = 0xFE; // Upper byte of Timer1 will be 0xFE PIR1bits.TMR1IF = 0; // Clear interrupt flag } /******************************* * CountDown() * * This subroutine delays the pic from reading signal from receiver for 2 hrs. * With a looptime of about 10 ms, count 720000 looptimes * 10.0 ms * 400 counts = 4000 ms ******************************* */ void CountDown() { ++TIMEDIFF; //PORTAbits.RA7 = 0; // Turn off LED if (++WAITPERIOD >= 3000) // Decrement counter and return if not zero { RIGHTPATH = 0; } } /******************************* * FeedTime() * * This subroutine turns on motor for specific amount of time. * based on DIPVALUE , 10.0 ms * 400 counts = 4000 ms ******************************* */ void FeedTime() { Fauler Hund (ECE4884L02) C7 if (++FEEDAMOUNT >= DIPVALUE) return if not zero { PORTAbits.RA3 = 0; PORTAbits.RA0 = 0; PORTAbits.RA7 = 0; RIGHTPATH = 2; CountDown } else { PORTAbits.RA7 = 1; PORTAbits.RA3 = 1; PORTAbits.RA0 = 1; } } // Decrement counter and // Turn off RELAY // Turn on RELAY // Turn off LED // Change RIGHTPATH to 2 to go into // Turn on LED // Turn on RELAY // Turn on RELAY /******************************* * DipSwitch() * * This subroutine calculates the amount of time to turn on motor * by taking in values from input pins from PORTB and PORTA * 10.0 ms * 400 counts = 4000 ms ******************************* */ void DipSwitch() { x= PORTB & 0x3F; if(x >= 50) { DIPVALUE = XD * .25; } else if(x >= 20 && x < 50) { DIPVALUE = XD * .24; } else DIPVALUE = XD * .225; if(DIPVALUE < 30000) DIPVALUE = 3000; else if(DIPVALUE > 180000) DIPVALUE = 18000; else DIPVALUE = DIPVALUE; } Fauler Hund (ECE4884L02) C8