Building an Automatic Fish Feeder

Hey there! I hope you have some fun Thanksgiving plans that include lots of gorging yourself and relaxing. Today’s song is an old favorite of mine.

I’ve been looking for a project for my Tessel2 microcontroller for a few months now. It’s cool because it allows you to program hardware using Javascript, which is my favorite.

 

With the coming holidays, I’ll be traveling for a few days. Since I’m the proud father of a Betta fish named Rhaegar, I wanted to make sure he stayed alive and happy, so I decided to make him an automatic fish feeder.

 

First, I printed out the model from Thingiverse.

IMG_0448
Top-down view of the final print with the servo attached

 

It’s a pretty simple idea: a tank that gravity-feeds food to an auger. The auger spins with the help of the attached continuous-rotation servo, which is controlled by the Tessel.

 

IMG_0447
3/4 view of the finished piece

Then, I wrote the code. I wanted to make sure the fish was fed every twelve hours, and that I was alerted when he was fed. I used Twilio as my text messaging service, which was super easy.

 

index.js


var tessel = require('tessel');
var servolib = require('servo-pca9685');
var servo = servolib.use(tessel.port['A']);
var twilio = require('twilio');

var servo1 = 1; // We have a servo plugged in at position 1
var TWELVE_HOURS = 60 * 60 * 1000 * 12; /* ms */

servo.on('ready', function () {
	var client = new twilio.RestClient('AC60444a3748dee195b9250b351b25f0f6', 'API_SECRET_GOES_HERE');

	var timeSinceLastFeed = 0;
  
  	var dispenseFood = function(){

  		if(((new Date) - timeSinceLastFeed) > TWELVE_HOURS) {
		//if(((new Date) - timeSinceLastFeed) > 1500) { //for testing at a faster pace
                        client.messages.create({
		            body: 'Fish is about to be fed!',
		            to: 'MY_PHONE_NUMBER',  // Text this number
		            from: '+18455354398' // From a valid Twilio number
		            }, function(err, message) {
		        });
	  		servo.move(servo1, .7); //moves clockwise at full speed
	  		setTimeout(function(){servo.move(servo1, 0.4)}, 75); //moves clockwise at slow speed
	  		
	  		timeSinceLastFeed = new Date();
	  	
	  		return true;
	  	}
	  	else {
	  		console.log('It has been less than twelve hours since the last feeding; can\'t feed yet!');
	  		return false;
	  	}
  	};
  	
  	dispenseFood(); //run once on start
  	setInterval(dispenseFood, 60 * 60 * 1000); //check every hour, just in case
  	//setInterval(dispenseFood, 2000); for testing
});

Then, I did lots of testing. I needed to make sure the amount of food was consistent in each feeding, which was exceedingly difficult. I decreased the time between feedings and tried both worms and pellets to see which was the easiest to produce consistent results, and ended up with a mixture of both.

View post on imgur.com

IMG_0456
Testing the amount of time to spin the auger for optimum food release

 

Next, I pushed the code out to the Tessel to connect to our WiFi and run the program automatically on boot, which was as simple as


t2 push index.js

 

Then, I secured it to the top of the fishtank with some tape (hey, it’s a prototype. Don’t judge me.)

Affixed temporarily to the top of the tank
Affixed temporarily to the top of the tank

We’re still in the testing phase, so hopefully I don’t over/underfeed him. Bettas are pretty hearty fish, though, so I’m not too worried.

 

Thanks for reading! I hope you’re having a fantastic day. See you next time!