[Newb] Automatic Fish Feeder // Needs Time

Greetings,
First project, an automatic fish feeder for my Beta fish.

Its pretty simple. It detects night & day via light reading from a photocell hanging out in my Window. It triggers the servo, which has a circular attachment (plastic piece that rotates). It has a whole it the circle that accepts 1-2 pellets from the food container that swings around to drop the pellets through a whole in the other side. I usually feed my beta about 4-5 pellets twice a day so it goes back and forth for food 3 times per feeding. It pauses for an hour after each cycle to allow the serial number to pass. I also hooked up a light as the fish bowl was pretty dark. I got it off of an LED flash light that had different modes so it can flash red when feeding then switch back to white. That’s about it, simple works :].

So I’m looking for some advice. I would like to incorporate a time system and get rid of the photocell for consistency. Being a newb, the only shield I have is the SD shield from seeedstudio and I really have looked into it much. I have checked out the tuts. and was going to look into data capture later to track the feedings. If I could use that to tell time, that would be great. But really open to opinions or code revisions.

PICS/GALLERY: (The first pic with the cup, was first concept :smiley: lol)
Imgur

CODE:
PLEASE BE WARNED! I’m not very good at coding. I made it work though.

#include <Servo.h> //servo library import

Servo servo;

int photocellPin = 0;
int photocellReading;
int lightLvl = 0;
int pos = 0;
int bowlLight = 8;
int startLed = 13;

void setup() {
  pinMode(bowlLight, OUTPUT);
  pinMode(startLed, OUTPUT);

//Bowl Light initial setup // OFF-WHITE
  digitalWrite(bowlLight, HIGH);
  delay(100);
  digitalWrite(bowlLight, LOW);
  delay(100);
  
  Serial.begin(9600);
  servo.attach(9);
}
 
void loop() {
  photocellReading = analogRead(photocellPin);  
  
  Serial.print("OFF = ");
  Serial.println(photocellReading);
  
  photocellReading = 1023 - photocellReading;
  
  lightLvl = map(photocellReading, 0, 1023, 0, 90);
  delay(15);
  

if (lightLvl == 30)
  {
  Serial.print("DAY FEED = ");
  
  //Change bowl light to alert(RED)
  digitalWrite(bowlLight, HIGH);
  delay(100);
  digitalWrite(bowlLight, LOW);
  delay(100);
  digitalWrite(bowlLight, HIGH);
  delay(100);
  digitalWrite(bowlLight, LOW);
  delay(100);
  digitalWrite(bowlLight, HIGH);
  delay(100);
  digitalWrite(bowlLight, LOW);
  delay(100);
  
  //Shake food to loosin
  servo.write(140);
  delay(500);
  servo.write(50);
  delay(500);
  servo.write(140);
  delay(500);
  servo.write(50);
  delay(500);
  
  
  servo.write(160); //pickup pellet
  delay(1000);
      servo.write(155);
      delay(500);
        servo.write(160);
        delay(500);
          servo.write(155);
          delay(500);
        servo.write(160);
        delay(500);
        
  servo.write(0); //drop pellet
  delay(1000);
      servo.write(5);
      delay(100);
        servo.write(0);
        delay(100);
          servo.write(5);
          delay(100);
            servo.write(0);
            delay(100);
              servo.write(50);
              delay(500);
          servo.write(0);
            delay(1000);
            
    servo.write(160); //pickup
  delay(1000);
      servo.write(155);
      delay(500);
        servo.write(160);
        delay(500);
          servo.write(155);
          delay(500);
        servo.write(160);
        delay(500);
        
  servo.write(0); //drop
  delay(1000);
      servo.write(5);
      delay(100);
        servo.write(0);
        delay(100);
          servo.write(5);
          delay(100);
            servo.write(0);
            delay(100);
              servo.write(50);
              delay(500);
          servo.write(0);
            delay(1000);
            
    servo.write(160); //pickup
  delay(1000);
      servo.write(155);
      delay(500);
        servo.write(160);
        delay(500);
          servo.write(155);
          delay(500);
        servo.write(160);
        delay(500);
        
  servo.write(0); //drop
  delay(1000);
      servo.write(5);
      delay(100);
        servo.write(0);
        delay(100);
          servo.write(5);
          delay(100);
            servo.write(0);
            delay(100);
              servo.write(50);
              delay(500);
          servo.write(0);
            delay(1000);
  
     //Change bowl light back to WHITE
  digitalWrite(bowlLight, HIGH);
  delay(100);
  digitalWrite(bowlLight, LOW);
  delay(100);
  digitalWrite(bowlLight, HIGH);
  delay(100);
  digitalWrite(bowlLight, LOW);
  delay(3600000);
  }

  else
  {
    servo.write(0);
  }
  
  if (lightLvl == 80)
  {
  Serial.print("NIGHT FEED = ");
     
     //Change bowl light to alert
  digitalWrite(bowlLight, HIGH);
  delay(100);
  digitalWrite(bowlLight, LOW);
  delay(100);
  digitalWrite(bowlLight, HIGH);
  delay(100);
  digitalWrite(bowlLight, LOW);
  delay(100);
  digitalWrite(bowlLight, HIGH);
  delay(100);
  digitalWrite(bowlLight, LOW);
  delay(100);
  
    //Shake food to loosin
  servo.write(140);
  delay(500);
  servo.write(50);
  delay(500);
  servo.write(140);
  delay(500);
  servo.write(50);
  delay(500);
  
  servo.write(160); //pickup pellet
  delay(1000);
      servo.write(155);
      delay(500);
        servo.write(160);
        delay(500);
          servo.write(155);
          delay(500);
        servo.write(160);
        delay(500);
        
  servo.write(0); //drop pellet
  delay(1000);
      servo.write(5);
      delay(100);
        servo.write(0);
        delay(100);
          servo.write(5);
          delay(100);
            servo.write(0);
            delay(100);
              servo.write(50);
              delay(500);
          servo.write(0);
            delay(1000);
            
    servo.write(160); //pickup
  delay(1000);
      servo.write(155);
      delay(500);
        servo.write(160);
        delay(500);
          servo.write(155);
          delay(500);
        servo.write(160);
        delay(500);
        
  servo.write(0); //drop
  delay(1000);
      servo.write(5);
      delay(100);
        servo.write(0);
        delay(100);
          servo.write(5);
          delay(100);
            servo.write(0);
            delay(100);
              servo.write(50);
              delay(500);
          servo.write(0);
            delay(1000);
            
    servo.write(160); //pickup
  delay(1000);
      servo.write(155);
      delay(500);
        servo.write(160);
        delay(500);
          servo.write(155);
          delay(500);
        servo.write(160);
        delay(500);
        
  servo.write(0); //drop
  delay(1000);
      servo.write(5);
      delay(100);
        servo.write(0);
        delay(100);
          servo.write(5);
          delay(100);
            servo.write(0);
            delay(100);
              servo.write(50);
              delay(500);
          servo.write(0);
            delay(1000);
  
    //Change bowl light back to WHITE
  digitalWrite(bowlLight, HIGH);
  delay(100);
  digitalWrite(bowlLight, LOW);
  delay(100);
  digitalWrite(bowlLight, HIGH);
  delay(100);
  digitalWrite(bowlLight, LOW);
  delay(3600000);
}
  
  else
  {
    servo.write(0);
  }  
}

Can you provide a link to the sd card shield? D you know ifmit has a real time clock? That is what you want for time keeping.

I guess you haven't got one already, so this is all you need. A real-time clock for $2.32.

http://www.ebay.com.au/itm/270985342203?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649

will solve your problem, so rush out and get yours today. Use the SD card later for recording events later.

liudr: Can you provide a link to the sd card shield? D you know ifmit has a real time clock? That is what you want for time keeping.

I'm not fimiliar with ifmit. But I got the shield at RadioShack. Mine is black, I've seen blue. I don't know if there is a difference.

http://www.radioshack.com/product/index.jsp?productId=13297701

Nick_Pyner: I guess you haven't got one already, so this is all you need. A real-time clock for $2.32.

http://www.ebay.com.au/itm/270985342203?ssPageName=STRK:MEWNX:IT&_trksid=p3984.m1497.l2649

will solve your problem, so rush out and get yours today. Use the SD card later for recording events later.

That simple eh? :) Awesome I'll order one tomorrow.

dritchie0042: That simple eh?

It certainly is. I just use a simple daily "if it's just past midnight, do this" command, but there is a time alarms library which enables a raft of controls, and could be the sort of thing you need.

BTW I would be a bit chary of that seeeduino stuff. It looks very expensive.

Nick_Pyner:

dritchie0042: That simple eh?

BTW I would be a bit chary of that seeeduino stuff. It looks very expensive.

Agreed. I didn't want to wait on shipping though. To be honest I oblivious to the market when I bought it.

"Well this looks like it does things and stuff!" XD

"EDIT: Added a a function for the light so it turns off at night, and on in the morning."

That sd card shield does not have a real time clock. You will need an RTC module then.

dritchie0042: I usually feed my beta about 4-5 pellets twice a day so it goes back and forth for food 3 times per feeding.

Does your fish actually care what time of day it is fed? Couldn't you just start your Arduino during daytime and arrange for it to be feed twice a day at regular intervals from then on? The feeding times would slowly drift due to inaccuracies in the Arduino clock, and if that matters to you, you could simply restart the Arduino every few months.

(If that doesn't work for you, I'd have thought the daylight sensing was likely to give you a better behaviour than the RTC since your fish probably regulates its daily cycle by daylight rather than by the clock.)

PeterH:

dritchie0042: I usually feed my beta about 4-5 pellets twice a day so it goes back and forth for food 3 times per feeding.

Does your fish actually care what time of day it is fed? Couldn't you just start your Arduino during daytime and arrange for it to be feed twice a day at regular intervals from then on? The feeding times would slowly drift due to inaccuracies in the Arduino clock, and if that matters to you, you could simply restart the Arduino every few months.

(If that doesn't work for you, I'd have thought the daylight sensing was likely to give you a better behaviour than the RTC since your fish probably regulates its daily cycle by daylight rather than by the clock.)

Not necessarily. The feeding times don't have to be exact by any means, just enough time for it to get hungry again.

Thanks for the suggestion, intervals is an option. The whole point is when I go on a camping trip this summer, I can leave it. I'm going to try all three suggestions and see which seems more reliable.

I didn't start this project with a fish feeder in mind. I was just playing with a photocell lol. It just came to be during the process.

dritchie0042:
I didn’t start this project with a fish feeder in mind. I was just playing with a photocell lol. It just came to be during the process.

That is so often the Arduino way…

The photocell has to go.

I forgot about thunder storms "/. Everytime lightning strikes it goes off XD

dritchie0042: The photocell has to go.

I forgot about thunder storms "/. Everytime lightning strikes it goes off XD

the fish is at least calmed down with food ;)