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)
02-18-2015 07:09 PM
paronaram It's 12min forward.
I change it to 720, same result...

Thanks
02-18-2015 05:21 AM
O2surplus
Quote:
Originally Posted by paronaram View Post
Hi, thanks for reply.
Yes, I did.
It was not working at all, and I found that library was old, updated library and it works now.
but I get 12min off

Thanks
Aram-

Check line #57 your copy of the TimeSet code. This is what it looks like-
const int kAdjustment = 25;
The 25 stands for the number of seconds needed to compile & upload the code to your arduino (on average). You can change the 25 to any number you wish to skew the time forward or back. Since your clock is 12 minutes off (forward or back?) try changing the 25 to 720 or -720 to see if that corrects the time.
02-18-2015 03:10 AM
paronaram Hi, thanks for reply.
Yes, I did.
It was not working at all, and I found that library was old, updated library and it works now.
but I get 12min off

Thanks
02-18-2015 02:55 AM
kman
Quote:
Originally Posted by paronaram View Post
Hello ALL,

I'm new in all this Arduino controlling devices, so please pardon all incorrect lingo and terminology.
I have a quick question: Does anybody know why when I run timeupdate code, it synchronizes time correctly, I get 12 min off?
Device came from O2 and it was PST time, and I'm in EST. So I decided to change the time...
Time on the PC is set correct.

Thanks.
Did you compile it first, and then immediately upload?
02-18-2015 12:34 AM
paronaram Hello ALL,

I'm new in all this Arduino controlling devices, so please pardon all incorrect lingo and terminology.
I have a quick question: Does anybody know why when I run timeupdate code, it synchronizes time correctly, I get 12 min off?
Device came from O2 and it was PST time, and I'm in EST. So I decided to change the time...
Time on the PC is set correct.

Thanks.
02-17-2015 07:49 PM
O2surplus
Quote:
Originally Posted by MrSpiffy View Post
Looks like I made a rookie mistake. Apparently, using pins 0 and 1 is a bit of a no-no during programming. I could disconnect the Arduino from the LCD, and then it might program and run. But I ended up moving those wires to other pins and now it appears to upload and run just fine!

Now, just need to wait for the temperature probe, get some fans, and test final wiring to the LEDs...

Nice work! Your going to love the smooth dimming that "Sink"'s code provides.
02-17-2015 04:46 PM
MrSpiffy Looks like I made a rookie mistake. Apparently, using pins 0 and 1 is a bit of a no-no during programming. I could disconnect the Arduino from the LCD, and then it might program and run. But I ended up moving those wires to other pins and now it appears to upload and run just fine!

Now, just need to wait for the temperature probe, get some fans, and test final wiring to the LEDs...

02-15-2015 12:43 AM
MrSpiffy Hate to resurrect an old thread, but I'm working on implementing this same idea and running into a problem.

Admittedly, I haven't received my temperature probe in the mail, and I don't have any fans or LEDs hooked up, yet. However, I'm not getting any output on my LCD. It does light up, and I'm able to perform a simple "hello world", as well as print text on each line of the screen. But the code the O2Surplus posted isn't working for me.

Any ideas? Does it matter if it's not wired up to the LEDs/fans/probe, yet?

Things I've changed in the code are:
  • LiquidCrystal instead of the LiquidCrystal_I2C library
  • Updated versions of OneWire, TimerOne, and Time.
  • Swapped out the DS1307 library for the DS3231 library.
  • Adafruit Trinket Pro 5V board (can't use pins 2 or 7, for USB only)

I've even tried commenting out anything to do with the temperature probe and fan controls, but that didn't help, either. The code compiles fine in Arduino IDE 1.0.5, so I'm not sure what the problem is. (I can attach libraries, if anyone needs them to compile the code.)

I also noticed some lcd.print and lcd.setCursor lines here and there that are commented out where the timing logic begins. Is there a reason for that? Or should I un-comment those lines?

EDIT: I'm wondering if it might have to do with this being a USB unit..? Perhaps serial commands won't work properly?

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 <DS3231RTC.h>
#include <OneWire.h> // added to original code by O2Surplus 4/2/2014
#include <LiquidCrystal.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 lcd(0,1,14,4,5,8);  // Set the LCD I2C address

//Temperature chip i/o
int DS18S20_Pin = 12; //DS18S20 Signal pin on digital 12

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

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
const int kChan1Pin = 10; // Channel 1 Pin

// 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,1023 }; // daytime LED state
const int kNightState[] = { 0, 0 }; // nighttime LED state

// duration (in seconds) of sunrise/sunset fade
const long kFadeDuration = 3600; // 60 minutes

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] >= 0 && state[0] <= 1023) Timer1.setPwmDuty(kChan0Pin, state[0]);
  if (state[1] >= 0 && state[1] <= 1023) Timer1.setPwmDuty(kChan1Pin, state[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 >= 0 && ctr < kTurnOn ) { // night
      set_state(kNightState);
      
      lcd.setCursor(0,2);
      //lcd.print("                   ");
      lcd.setCursor(5,2);
      lcd.print("NIGHT/MODE");         // LCD coding added by O2Surplus 4/2/2014
      digitalWrite(fanEnable,LOW);
      lcd.setCursor(6,3);
      lcd.print("FANS-OFF");
      
  } else if ( ctr >= kTurnOn && ctr <= (kTurnOn+kFadeDuration) ) { // sunrise
    int foo[2];
    fader(kTurnOn, kNightState, kDayState, foo);
    set_state(foo);
      
      lcd.setCursor(0,2);
      //lcd.print("                   ");
      lcd.setCursor(3,2);
      lcd.print("SUNRISE/MODE");              // LCD coding added by O2Surplus 4/2/2014
      digitalWrite(fanEnable, HIGH);
      lcd.setCursor(7,3);
      lcd.print("FAN-ON");
  } else if ( ctr > (kTurnOn+kFadeDuration) && ctr < kTurnOff ) { // day
    set_state(kDayState);
      
      lcd.setCursor(0,2);
      //lcd.print("                   ");
      lcd.setCursor(5,2);             
      lcd.print("DAY/MODE");              // LCD coding added by O2Surplus 4/2/2014
      digitalWrite(fanEnable, HIGH);
      lcd.setCursor(7,3);
      lcd.print("FAN-ON");
  } else if ( ctr >= kTurnOff && ctr <= (kTurnOff+kFadeDuration) ) { // sunset
    int foo[2];
    fader(kTurnOff, kDayState, kNightState, foo);
    set_state(foo);
      lcd.setCursor(0,2);
      //lcd.print("                   ");
      lcd.setCursor(5,2);
      lcd.print("SUNSET/MODE");              // LCD coding added by O2Surplus 4/2/2014
      //lcd.setCursor(11,2);
      //lcd.print("  ");
      digitalWrite(fanEnable, HIGH);
      lcd.setCursor(7,3);
      lcd.print("FAN-ON");
  } else if ( ctr > (kTurnOff+kFadeDuration) && ctr < 86400 ) { // night
    set_state(kNightState);
      lcd.setCursor(0,2);
      //lcd.print("                   ");
      lcd.setCursor(5,2);
      lcd.print("NIGHT/MODE");          // LCD coding added by O2Surplus 4/2/2014
      digitalWrite(fanEnable,LOW);
      lcd.setCursor(6,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(kChan0Pin, OUTPUT);     
  Timer1.pwm(kChan0Pin, 0);
  pinMode(kChan1Pin, OUTPUT);     
  Timer1.pwm(kChan1Pin, 0);
  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::crc8( addr, 7) != 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 = 0; i < 9; i++) { // 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;
}
02-13-2015 01:37 PM
VitorBello
Problem with Daty State.

Hello Everyone!

Discovered this threat and upload Sink program into the Arduino with a simple CAT4101's boar that I made. I set the NightState period to (5,5) in order to get the MoonLight efect, however I notice that this state did not change during the morning. I set the DayState to the maximum value.

I am using a DS1307, and notice that the sketch is using the DS3231RTC library

Do you guys think this can be the reason of the problem? I will change that today at night but only tomorrow i will have sure.

Thanks a lot!
01-26-2015 01:58 AM
VitoA
Arduino 1.0.6 support

Hi, the original code for this led lighting was really usefull for me.
I've done some changes to be compactible with new arduino ide 1.0.6 instead of v0023/22
Dont know if anyone has done this but in anex are libraries .
Please check and confirm if it works
Only tested with DS3231RTC

https://dl.dropboxusercontent.com/u/...braries106.zip

Tanks for all

VitoA
01-23-2015 03:02 PM
scaLLas Hi all
Have been playing around with the timer 3 library on a mega but need 3 more channels to do the fading
soooo.... I adapted the timer3 library to make timer 4. Its on pins no 6,7 and 8 on the Mega2560
So would love if someone can test it and give feedback?
Works EXACTLY as timer3... only as timer 4!

Gives three more channels that can fade led's!!
01-09-2015 05:04 PM
karmacop Hey guys. I've just discovered this thread and am going to try this out in one of my Snake vivariums. Im wondering if any of you have considered using the Timelord/AVR-Libc library which automatically determines the sunrise/sunset time based on your lat/long?
I'm going to have a crack at integrating it myself but my coding skills are limited to say the least...
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.
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:53 PM.


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