Jump to content
43oh

Aquarium Controller


Recommended Posts

Working on a personal project so I thought I would start a build log. Here are the basics and expectations I have.

 

I want to wake up in the morning, push a button on/near my fish tank and have the following take place:

1. The lights will come on as if the sun is raising.

2. Automatic feeder rotates to feed the fish.

3. Lights stay on for 10hours.

4. Lights turn off as if the sun was setting.

 

Future expansion will include testing for water temp and sounding an alarm if the water gets too hot or too cold, monitoring water flow through the filter and sounding an alarm if it slows, and maybe other things as they come to me.  I've already done the basics above on the arduino but now I'd like to learn C.

 

I'm starting with looking over sample code for the parts I need and working on learning how each part works so I can customize it for my needs. I'll post my working code when I finish learning more about controlling a servo.

Link to post
Share on other sites

Okay, lets get started with some snippit code to ensure I'm on the right track because right now my code likes like a huge mess and I'm starting to confuse myself.

 

This is just stopping the WDT and setting up the pins,

 

void main(void)
{
  WDTCTL = WDTPW + WDTHOLD;  // Stop the dog

// Setup the pins
// P2DIR = <PIN7><PIN6><PIN5><PIN4><PIN3><PIN2><PIN1><PIN0>
// 1=Out, 0=In
P2DIR = 0000 0111; // Set P2.0, P2.1, P2.2 to output direction
P2OUT = 0;  // Set P2.0, P2.1, P2.2 off

Link to post
Share on other sites

And this is turning on three LEDs that will simulate turning on 3 groups of LEDs in the LED light bar I have over the tank.

 

 

// Turn on LEDs.
P2OUT = 0000 0001;  //Turn on LED1
__delay_cycles(5000000);  // SW Delay of 5M cycles at 1Mhz, Delay for 5sec
P2OUT = 0000 0011;  //Turn on LED2
__delay_cycles(5000000);  // SW Delay of 5M cycles at 1Mhz, Delay for 5sec
P2OUT = 0000 0111;  //Turn on LED3
__delay_cycles(5000000);  // SW Delay of 5M cycles at 1Mhz, Delay for 5sec
 

Link to post
Share on other sites

Still need code to wait for a external button push, and to rotate a servo. I ran some sample code on my 430 that was suppose to just rotate a servo back and forth but all I got was errors so I'm going to take a break and try again tonight.

 

Also need to research use of TimerA so I'm not just using a delay for 10hours while the light is on.

Link to post
Share on other sites

I wonder if an external RTC and ambient light sensor would be a worthwhile substitute for the push button.

 

RTC keeps time and the ambient light sensor (standing in for the push button) is only operable when the RTC says the time of day coincides with sunrise and sunset.

Link to post
Share on other sites

I've thought about the light sensor and a clock, but where the tank is located in the condo it may not get enough light to signal day light and if I turn a light on or off in the room I don't want to trip the sensor. This way is still using automation to help with feeding and ensuring the light doesn't stay on for too long yet still gives me the sense that I am interacting with my fish which is important to me.

Currently I like to make a cup of coffee or a quick breakfast and sit down after feeding the fish so I can watch them eat. Not only is it calming but allows me to count the fish, check their health, etc.

Link to post
Share on other sites

Skipping ahead a bit, here is the snippit to turn off the LEDs after the 10hours has elapsed...

 

 

// Turn off LEDs
P2OUT = 0000 0110;  //Turn off LED1
__delay_cycles(5000000);  // SW Delay of 5M cycles at 1Mhz, Delay for 5sec
P2OUT = 0000 0100;  //Turn off LED2
__delay_cycles(5000000);  // SW Delay of 5M cycles at 1Mhz, Delay for 5sec
P2OUT = 0000 0000;  //Turn off LED3
__delay_cycles(5000000);  // SW Delay of 5M cycles at 1Mhz, Delay for 5sec
 

Link to post
Share on other sites

bobnova,

I'd love to. I'd like to have each group of LEDs on a PWM but I'm not there yet with my C skills. I can't even get a servo to turn or a switch to work right.

I've found myself doing some things with Energia, then going into their source trying to figure out how and why what I've done works.
Link to post
Share on other sites

Below is code shamelessly copied from somewhere else on this forum. Some of it (the P2 parts) are related to other things the person was doing. Not sure P1.2 and P1.3 are actually the pins. Test this with nothing but LEDs connected!  One of them does one of the Launchpad LEDs. Don't remember which, it's been a bit.

This'll get you started anyway, sorry it's not more specific.

Energia is nice too (that's where I do my programming actually. You can still do direct port stuff in it), you'll need to set some of it manually though as it defaults to 250Hz (500Hz?) PWM speed. Fish probably won't like that. I don't like it either, really.

 

WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  P1DIR |= 0x0C;                            // P1.2/TA0.1 and P1.3 output
  P1SEL |= 0x0C;                            // P1.2 and P1.3 TA1/2 options

  P2DIR |= 0x02;                            // P2.1/TA1.1 output
  P2SEL |= 0x02;                            // P2.1 options

  CCR0 = 640-1;                             // PWM Period
  CCTL1 = OUTMOD_7;                         // CCR1 reset/set
  CCTL2 = OUTMOD_7;                         // CCR1 reset/set
  CCR1 = CCR0 / 2;                               // CCR1 PWM duty cycle
  CCR2 = CCR0 / 2;
  TACTL = TASSEL_2 + MC_1;                  // SMCLK, up mode

This (as is) gives you a 50% PWM on a G2553 on two pins (I forget which, 1_something) at ~25kHz.

 

CCR1 and CCR2 control the duty cycle for the two outputs, CCR0 is the number they count up to (639, in this case. They start at 0 so that's 640 steps).

 

If CCR1 is set to 0 you have 0% on time (full off). At 639 you have 100%. At 160 it's 25%.

 

If you raise CCR0 it takes longer for the MCU to count from zero to that number, and the PWM speed decreases. With CCR0 set to 1279 you'll have a 12.5kHz PWM signal, and setting CCR1 to 320 will be the new 20%.

Similarly you can lower CCR0 for a higher speed, with CCR0 set to 329 you'll have ~50kHz, and CCR1 at 80 is the new 25%.

 

CCR1 and CCR2 can be different numbers, so one set of lights (day lights, say) can be fading out while a second set (moon lights) fade on.

 

Do note that with "high" PWM speeds (over maybe 1kHz) and MOSFETs (I'm assuming that's how you're controlling high power LED lights) it's a good idea to put a resistor between the MCU pin and the MOSFET's Gate pin to limit current. The MOSFET is a capacitor of sorts, and charging/discharging it rapidly gives a decently high average current. A 470 ohm resistor will keep the MCU happy and shouldn't restrict your PWM speed much.

 

Once you have PWM squared away, a "for" loop incrementing through CCR1/CCR2 will do the trick to slowly fade them in, if nothing else needs to be happening during that time.

Link to post
Share on other sites

Spoil9, here is a great post on controlling single servos all the way up to 8 servos using potentiometer and the ADC.

http://forum.43oh.com/topic/252-launchpad-controlling-up-to-8-rc-servos/

 

Here is some code from my snippet library. When I find the website that I got it from I will make sure to update.

 

Link to post
Share on other sites

Thanks for the code. I'm working on getting an LED to toggle via an external pushbutton using code from TI Wiki. I think the biggest problem I am having is that the chip is not on the launchpad because I don't have the couplers so I'm using the launch pad to program the chip which is on a breadboard. I'll pick up those couplers tomorrow and then I'll be able to put the chip back on the launchpad and use it like I'm suppose to.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...