Successful project - excitement and thanks!

Status
Not open for further replies.

tboydva

Member
OK, just posting a little newbie excitement at making a working PIC project. Wanted to "show it off" and thank those that help folks on this forum (and acknowledge the websites like Nigel's and Spency's which have helped me immensely).

So, I decided to put a CO2 system "on" my aquarium. You could read about it here, but the long-shot is freshwater aquarium plants are limited by dissolved CO2. In the year I've had the system up and running, it's decided to go nuts - don't know if it's the regulator diaphragm (which reportedly can "buckle" and fail temporarily), or perhaps the fingers of my 3 year old boy who watches intently as I adjust the regular. Anyway, during the failures, it drops the pH significantly and all the fish turn belly-up. Very sad when they become part of the family. So, I decided to use an optical sensor to try and shut off the regulator solenoid if the bubble count got too high. I'm including the circuit and a picture of the photodiode one the bubble window... Here's the source:

Code:
/*
* BUBBLE "MEETER" FOR TURNING OFF SOLENOID
*
* This program counts the number of "high" transitions on RB0 and if it is
* over a certain value (specified in code), it induces a high signal on RB1
*
* PIC16F628A
* 8 Mhz crystal, HS clock
*
* PORTB.F0, in : counter input
* PORTB.F1, out: run on signal
* PORTB.F2, in: Reset
*
* Author : Bruno Gavand, november 2005,
* The interupt routine used; the rest (including RS232 removed) and further
* modified by TJB; Feb08
*
*/

/*
* RAM variables
*/
unsigned long   cntr ;          // number of RB0 transition
unsigned int    ovrflw ;        // number of timer0 overflows
unsigned char   kill;           // if 1, RB1 changes state

/*
* interrupt routine called 2000000/256 times by seconds :
* the timer TMR0 is increased each 4 clock cycles (quartz frequency is 8 Mhz),
* and overflows when reseting from 255 to 0,
* calling the interrupt procedure with bit T0IF set
*
* also called on each RBO transition, with bit INTF set
*/
void    interrupt(void)
{
	if(INTCON.INTF)
	{
		/*
		* RB0 interrupt
		*/
		cntr++ ;                // inc. transition counter
		if (cntr > 2)
		{
		 kill = 1;              // set the kill bit to 1 if more than one bubble
                            // is counted with one second.
		}
		INTCON.INTF = 0 ;       // clear interrupt flag to enable next call
	}
	else if(INTCON.T0IF)
	{
		/*
		* TIMER 0 overflow
		*/
		ovrflw++ ;              // inc. overflow counter
		INTCON.T0IF = 0 ;       // clear interrupt flag to enable next call on overflow
	}
}

void main()
{
	TRISB.F0 = 1 ;            // RB0 interrupt pin as input
	TRISB.F1 = 0 ;            // RB1 is output
	TRISB.F2 = 1 ;            // RB2 is input - receives reset
	kill = 0 ;                // The kill bit set to 0
	OPTION_REG = 0b11011000 ; // no prescaler

  // Set Pin PORTB 1 to high and delay for 10 seconds
  PORTB.F1 = 1;
  Delay_ms(10000);
  

	for(;;)
	{
		cntr = 0 ;              // clear counters
		ovrflw = 0 ;

		INTCON = 0b10110000 ;   // T0IF, INTF and GIE enabled
		if (kill == 1)          // If kill gets set in interrupt....
		   {
		   PORTB.F1 = 0;           // Set PORTB, bit one to low shutting down the
                               // system
		   }
		else
		   {
       PORTB.F1 = 1;          // otherwise keep relay closed.
		   }
    if (PORTB.F2 = 1)       // If the PortB goes to 0...
    {
    kill = 0;               // Reset the kill
    }
		while(ovrflw < 7813) ;  // wait 1 second : 7813 =
								            // 8 000 000 / 4 / 256, rounded up
								            // This modified for 8 MHz crystal!
		INTCON.GIE = 0 ;        // stop all interrupts
	}
}

//

So, I wanted the solenoid "off" if the power to the circuit is off just in case. So the program turns it on, waits for stabilization, then starts monitoring the bubbles per second. If over 2, it shuts down (saving the fish I hope!).

Anyway, just a thanks to everyone lurking here - without the reading material, I never could have done it!!!
 

Attachments

  • schematic.png
    22.6 KB · Views: 260
  • Jun08 145.jpg
    319.3 KB · Views: 246
Congratulations. Have you tested it by letting your 3YO loose on it?

Mike.
 
Ahhh, that's gonna be the true "acid" test (OK, pun intended). We're on day 3 and all seems OK - one trip event so perhaps junior isn't the culprit after all. I did forget to give credit to someone (who I can't find now through my googling) who gave me the idea putting the photodiode on his beer fermenter airlock. That's gonna be next - it's why I had an extra CO2 tank lying around!
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…