The Planted Tank Forum - Reply to Topic
Planted Tank Forums
Your Tanks Image Hosting *Tank Tracker * Plant Profiles Fish Profiles Planted Tank Guide Photo Gallery Articles

Go Back   The Planted Tank Forum > Specific Aspects of a Planted Tank > DIY > Improved Arduino LED lighting code

Thread: Improved Arduino LED lighting code Reply to Thread
Title:
  
Message:
Post Icons
You may choose an icon for your message from the following list:
 

Register Now

In order to be able to post messages on the The Planted Tank Forum forums, you must first register.
Please enter your desired user name, your email address and other required details in the form below.
User Name:
Password
Please enter a password for your user account. Note that passwords are case-sensitive.
Password:
Confirm Password:
Email Address
Please enter a valid email address for yourself.
Email Address:

Log-in

Human Verification

In order to verify that you are a human and not a spam bot, please enter the answer into the following box below based on the instructions contained in the graphic.



Additional Options
Miscellaneous Options

Topic Review (Newest First)
08-03-2014 08:41 AM
cvasantrao OK. Will Try it out
Thanks
08-02-2014 06:57 PM
farrenator Why can't you use the digital outputs on your existing arduino board?

Quote:
Originally Posted by cvasantrao View Post
Just joined. I already have a arduino controller for my aquarium with light switching on at sunrise and switching off at sunset and moonlight coming on at sunset and switching off at 11 in the night.Found this interesting and planning to add dimming. Can the board design be shared. I am willing to share my code.
08-02-2014 06:14 PM
cvasantrao Just joined. I already have a arduino controller for my aquarium with light switching on at sunrise and switching off at sunset and moonlight coming on at sunset and switching off at 11 in the night.Found this interesting and planning to add dimming. Can the board design be shared. I am willing to share my code.
06-27-2014 04:00 AM
O2surplus
Quote:
Originally Posted by gus6464 View Post
O2,

Can your new code on page 11 be loaded on your custom Typhon boards? Is is possible to add the temp sensor to them?

Unfortunately - the answer is no. The new code doesn't have any provisions in it for a user interface, and my version of the "Typhon" doesn't have a Dallas OneWire connection built in. I'd have to redesign it a bit to break out a digital pin to be used as an input for the temp sensor. My controller does have an I2C connection though. I'd have to do some research as it may be possible to build a small add on circuit board that could convert the temp data over to something readable by the I2C input.
06-25-2014 07:33 AM
gus6464 O2,

Can your new code on page 11 be loaded on your custom Typhon boards? Is is possible to add the temp sensor to them?
06-25-2014 03:35 AM
O2surplus
Quote:
Originally Posted by sink View Post
O2Surplus! You pimp! Love the LCD integration, very nice work. I don't even have a fishtank at the moment, having sold everything off before I moved across the country this month. But I'm contemplating an aquaponics setup in my garage, which would get me back into tinkering with this code again in coming months. In the meantime I'll put a note in the FP pointing people at your more recent improvements! Glad to see folks are still working with it.

Whoa! Anders YOU'RE ALIVE! Glad to see you're back! You've been missed around here for a long time! I've been doing my best to keep this thread alive since it's the only aquarium code that I've ever found that takes advantage of TIMER1 to produce 10 bit dimming. Hopefully you'll get another tank set up and feel the need to tweak your code even further. I know that I'd love it to eventually have independent start times and fade durations for each channel. (hint-hint I can't figure that out on my own. LOL)
06-24-2014 10:49 PM
sink O2Surplus! You pimp! Love the LCD integration, very nice work. I don't even have a fishtank at the moment, having sold everything off before I moved across the country this month. But I'm contemplating an aquaponics setup in my garage, which would get me back into tinkering with this code again in coming months. In the meantime I'll put a note in the FP pointing people at your more recent improvements! Glad to see folks are still working with it.
06-11-2014 11:09 AM
DaChicken
LED Controller

Good Morning all!!

I must say... what an awesome thread to read here....

thank you all for participating in this project. It is amazing and helpful for the noobs like us.

I have uploaded the code to my Arduino Uno and it seems to be working, except that the time is 20 minutes late....

http://i1282.photobucket.com/albums/...ps329a0652.jpg

The main problem for me is the hardware setup....

Can anyone please give me a diagram for the setup to be used in order to work with O2's latest Arduino 1.05.-rev2 software?

The setup I tried was the one for the Typhoon controller but I keep on making a mistake... can anyone please assist?

Typhoon Controller Setup:


Regards

Hendrik Venter
05-13-2014 06:05 PM
Guy.hall Cheers o2, will give it a go ta

Sent from my Nexus 5 using Tapatalk
05-13-2014 05:43 PM
O2surplus Guy- try this version. This one doesn't leave any artifact text in the display.
Just remember to adjust the variables to your liking before you load it.


PHP Code:
/*
 * Name:    tank-control.pde
 * Author:    User "sink" at plantedtank.net forums
 * URL:        http://bitbucket.org/akl/tank-control
 *
 * This is control code for an aquarium lighting system.  It is intended to be
 * run on an Arduino microcontroller board.  It allows independant
 * high-resolution control of two PWM outputs (normally connected to LED
 * drivers) and complete flexibility with respect to intensity, timing
 * schedules, and sunrise/sunset.
 *
 * This code requires the following libraries: Wire, TimerOne, Time, DS1307RTC
 *
 * The latest version of this code can always be found at above url.
 */

/*
 * Copyright (c) 2011, User "sink" at plantedtank.net forums
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.  
 */
/* NOTICE: 4/2/2014- Addtional support for I2C LCD & Dallas OneWire Temperature readout added by O2Surplus.
              The original Libraries used in this version of the code were updated for use with Arduino 1.5-r2.
              Please make sure to use the proper version of said libraries. */
              
#include <TimerOne.h>
#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>
#include <OneWire.h> // added to original code by O2Surplus 4/2/2014
#include <LiquidCrystal_I2C.h>// added to original code by O2Surplus 4/2/2014

//LiquidCrystal_I2C lcd(0x27,16,2);  // set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x2721045673POSITIVE);  // Set the LCD I2C address

//Temperature chip i/o
int DS18S20_Pin 2//DS18S20 Signal pin on digital 2

OneWire ds(DS18S20_Pin);  // on digital pin 2

int fanEnable 8;// assigns cooling fan ON/OFF control to digital pin 8.

/*
 * IMPORTANT:  These *must* be the pins corresponding to the Timer1 timer on
 * the ATmega168/328.  These are digital pins 9 and 10 on the Uno/Duemilanove.
 */
const int kChan0Pin 9// Channel 0 Pin "Cool White"
const int kChan1Pin 10// Channel 1 Pin "Warm White"

// All times are in seconds since midnight (valid 0 - 86399)
const long kTurnOn 32400// time dawn begins - 0900hrs
const long kTurnOff 75600// time sunset begins - 2100hrs

/*
 * Light "state" represents the PWM duty cycle for each channel This normally
 * dictates light intensity. It is an array { duty_chan_1, duty_chan_2 }.
 * Possible values for duty cycle are 0 - 1023.
 */
const int kDayState[] = { 1023,512 }; // daytime LED state
const int kNightState[] = { 5}; // nighttime LED state

// duration (in seconds) of sunrise/sunset fade
const long kFadeDuration 7200// 2 hours

long ctr;

/*
 * fader -- Determine output state for a given time to provide smooth fade from
 * one state to another.
 *     Args:
 *     start_time  -- time (in seconds) of start of fade
 *     start_state -- beginning state
 *     end_state   -- ending state
 *     out         -- array to update with state
 */
void fader(long start_time, const int start_state[], const int end_state[], int out[2]) {

  
float per_second_delta_0 = (float) (end_state[0]-start_state[0])/kFadeDuration;
  
float per_second_delta_1 = (float) (end_state[1]-start_state[1])/kFadeDuration;

  
long elapsed ctr-start_time;

  
out[0] = start_state[0] + per_second_delta_0 elapsed;
  
out[1] = start_state[1] + per_second_delta_1 elapsed;
}

// return seconds elapsed since midnight
long seconds_since_midnight() {
  
time_t t now();
  
long hr hour(t);
  
long min minute(t);
  
long sec second(t);
  
long total hr 3600 min 60 sec;
  return 
total;
}

// set output state
void set_state(const int state[]) {
  if (
state[0] >= && state[0] <= 1023Timer1.setPwmDuty(kChan0Pinstate[0]);
  if (
state[1] >= && state[1] <= 1023Timer1.setPwmDuty(kChan1Pinstate[1]);
}

/*
 * determine_state -- This is where the actual timing logic resides.  We
 * examine ctr (seconds since midnight) and then set output state accordingly.
 * Variable ctr rolls back to 0 at midnight so stages that cross midnight (ie:
 * nighttime) are broken up into two stages.
 */
void determine_state() {
  if ( 
ctr >= && ctr kTurnOn ) { // night
      
set_state(kNightState);
      
      
lcd.setCursor(0,2);
      
lcd.print("     NIGHT TIME     ");         // LCD coding added by O2Surplus 4/2/2014
      
digitalWrite(fanEnable,LOW);
      
lcd.setCursor(0,3);
      
lcd.print("      FANS=OFF      ");
      
  } else if ( 
ctr >= kTurnOn && ctr <= (kTurnOn+kFadeDuration) ) { // sunrise
    
int foo[2];
    
fader(kTurnOnkNightStatekDayStatefoo);
    
set_state(foo);
      
      
lcd.setCursor(0,2);
      
lcd.print("      SUNRISE       ");              // LCD coding added by O2Surplus 4/2/2014
      
digitalWrite(fanEnableHIGH);
      
lcd.setCursor(0,3);
      
lcd.print("       FAN=ON       ");
  } else if ( 
ctr > (kTurnOn+kFadeDuration) && ctr kTurnOff ) { // day
    
set_state(kDayState);
      
      
lcd.setCursor(0,2);      
      
lcd.print("      DAY TIME      ");              // LCD coding added by O2Surplus 4/2/2014
      
digitalWrite(fanEnableHIGH);
      
lcd.setCursor(0,3);
      
lcd.print("       FAN=ON       ");
  } else if ( 
ctr >= kTurnOff && ctr <= (kTurnOff+kFadeDuration) ) { // sunset
    
int foo[2];
    
fader(kTurnOffkDayStatekNightStatefoo);
    
set_state(foo);
      
      
lcd.setCursor(0,2);     
      
lcd.print("       SUNSET       ");              // LCD coding added by O2Surplus 4/2/2014
      
digitalWrite(fanEnableHIGH);
      
lcd.setCursor(0,3);      
      
lcd.print("       FAN=ON       ");
  } else if ( 
ctr > (kTurnOff+kFadeDuration) && ctr 86400 ) { // night
    
set_state(kNightState);
      
      
lcd.setCursor(0,2);     
      
lcd.print("     NIGHT TIME     ");          // LCD coding added by O2Surplus 4/2/2014
      
digitalWrite(fanEnable,LOW);
      
lcd.setCursor(0,3);      
      
lcd.print("      FANS=OFF      ");
  }
}

/*
 * Utility function for pretty digital clock time output
 * From example code in Time library -- author unknown
 */
void printDigits(int digits) {
  
Serial.print(":");
  if(
digits 10)
    
Serial.print('0');
  
Serial.print(digits);
 
}
void displayDigits(int digits) {
  
lcd.print(":");
  if(
digits 10)
    
lcd.print('0');
  
lcd.print(digits);
  
}

/*
 * Display time
 * Adapted from example code in Time library -- author unknown
 */
void digitalClockDisplay() {
  
Serial.print(hour());
  
printDigits(minute());
  
printDigits(second());
  
Serial.print(" ");
  
Serial.print(month());
  
Serial.print("/");
  
Serial.print(day());
  
Serial.print("/");
  
Serial.print(year()); 
  
Serial.println(); 
}
void lcdClockDisplay() {
  
lcd.setCursor(0,0);
  
lcd.print(hour());
  
displayDigits(minute());
  
displayDigits(second());
  
lcd.print("  ");
  
lcd.print(month());
  
lcd.print("/");
  
lcd.print(day());
  
lcd.print("/");
  
lcd.print(year()); 
   
}
void setup() {
  
Serial.begin(115200); // Max for Arduino Uno
  
setSyncProvider(RTC.get);
  
setSyncInterval(120);
  
Wire.begin();
  
lcd.begin(20,4);         // initialize the lcd for 20 chars 4 lines and turn on backlight
  
Timer1.initialize(6666); // 150Hz PWM
  
pinMode(kChan0PinOUTPUT);     
  
Timer1.pwm(kChan0Pin0);
  
pinMode(kChan1PinOUTPUT);     
  
Timer1.pwm(kChan1Pin0);
  
pinMode(fanEnable,OUTPUT);   // Fan control coding added by O2Surplus 4/2/2014
}

void loop () {
  
float temperature getTemp();// Temperature serial display coding addded by O2Suplus 4/2/2014
  
Serial.println(temperature);
  
ctr seconds_since_midnight();// Original code
 
  
lcd.setCursor(0,1);          // Temperature to I2C LCD added by O2Surplus 4/2/2014
  
lcd.print("Temp C = ");
  
lcd.print(temperature);
 
  
determine_state();// Original code
  
Serial.print("ctr: ");
  
Serial.print(ctr); // display counter
  
Serial.println();
  
digitalClockDisplay(); //display time
  
Serial.println();
  
lcd.setCursor(0,0); //Time/Date to I2C display added 4/2/2014 by O2Surplus
  
lcd.println();
  
lcdClockDisplay();
  
  
delay(1000); // no need to do anything until next second
}// Original Code ends here.


float getTemp(){
  
//returns the temperature from one DS18S20 in DEG Celsius // Added by O2Suplus 4/2/2014

  
byte data[12];
  
byte addr[8];

  if ( !
ds.search(addr)) {
      
//no more sensors on chain, reset search
      
ds.reset_search();
      return -
1000;
  }

  if ( 
OneWire::crc8addr7) != addr[7]) {
      
Serial.println("CRC is not valid!");
      return -
1000;
  }

  if ( 
addr[0] != 0x10 && addr[0] != 0x28) {
      
Serial.print("Device is not recognized");
      return -
1000;
  }

  
ds.reset();
  
ds.select(addr);
  
ds.write(0x44,1); // start conversion, with parasite power on at the end

  
byte present ds.reset();
  
ds.select(addr);    
  
ds.write(0xBE); // Read Scratchpad

  
  
for (int i 09i++) { // we need 9 bytes
    
data[i] = ds.read();
  }
  
  
ds.reset_search();
  
  
byte MSB data[1];
  
byte LSB data[0];

  
float tempRead = ((MSB << 8) | LSB); //using two's compliment
  
float TemperatureSum tempRead 16;
  
  return 
TemperatureSum;
  

05-13-2014 01:21 PM
Guy.hall Thank you, didn't think of that but will give it a shot :-)

Sent from my Nexus 5 using Tapatalk
05-13-2014 12:19 PM
VintonC
Quote:
Originally Posted by Guy.hall View Post


I have this problem running the code, when it changes mode etc, it doesn't clear the text properly?

Everything still operates as normal, it just looks a bit funny lol

Sent from my Nexus 5 using Tapatalk
The old text isn't getting overwritten or cleared. You should make the strings the same length that are used to indicate the state change or mode, like 'FAN-ON ' or FAN-OFF'. Notice the extra space at the end of fan-on, it will clear the 'F' from fan off. You can put the extra spaces at either end of the string. It's just a matter of what looks better to you.

I haven't looked at the code, so I can't tell you where to make the change.
05-12-2014 06:45 PM
Guy.hall

I have this problem running the code, when it changes mode etc, it doesn't clear the text properly?

Everything still operates as normal, it just looks a bit funny lol

Sent from my Nexus 5 using Tapatalk
04-17-2014 09:50 PM
O2surplus
Quote:
Originally Posted by jeffkrol View Post
Yep that is the next project.. My orig order was a plug and play one (just direct plug to the Typhon serial out) from your recommendation I believe, but they sent a "solder it yourself" board.. grrr ..
As soon as the parts come from LED Groupbuy come.. I get to "play" a bit..
(so who did they steal their 5up Meanwell boards from.. )
As you may remember I upgraded my shooting iron..

now which one is ground.............................. (just kidding)
LOL- I wouldn't call it stealing ... the designs were posted online and they just jumped on the band wagon like everyone else. Stealing is when you're a Chinese seller that's selling my designs as your own, but at the same time, too lazy to remove my name from the silkscreen. It's too bad for them now as most of my designs have become nearly impossible to mas produce using only machines. They'd need to employ an army of slaves... er.... "workers" to perform all the hand soldering- LOL. Oh Yeah? What was I thinking? they already do.
04-17-2014 09:12 PM
jeffkrol
Quote:
Originally Posted by O2surplus View Post
LOL- Using a USB port alone does not qualify you as a "Geek". But if you whip out a FTDI adapter too......Now you're qualified.
Yep that is the next project.. My orig order was a plug and play one (just direct plug to the Typhon serial out) from your recommendation I believe, but they sent a "solder it yourself" board.. grrr ..
As soon as the parts come from LED Groupbuy come.. I get to "play" a bit..
(so who did they steal their 5up Meanwell boards from.. )

As you may remember I upgraded my shooting iron..

now which one is ground.............................. (just kidding)
This thread has more than 15 replies. Click here to review the whole thread.

Posting Rules
You may post new threads
You may post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


All times are GMT. The time now is 01:07 AM.


Powered by vBulletin®
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright Planted Tank LLC 2012