Improved Arduino LED lighting code - Page 7
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


Reply
 
Thread Tools Display Modes
Old 04-09-2012, 03:32 PM   #91
sink
Algae Grower
 
PTrader: (2/100%)
Join Date: Jun 2011
Location: Boston, MA
Posts: 148
Default

Quote:
Originally Posted by Gisimo View Post
Hello i have a problem with the code, when i make changes to the code and upload it, it doesn't work unless i remove the ds1307 before i update the changes why is that? and i can't seem to find the file for setting the time on a ds1307 i currently use a arduino 1,0 file and program for that, is that the problem?
Thanks...
Hi Gisimo, Currently the libraries I use don't work on Arduino 1.0, so you need to use the version previous to that (22 or 23). Don't worry -- you aren't really losing any functionality when you do that and in fact code compiled with the older versions uses less memory to run. All of the needed files are in the latest version of the archive (see the first post). I'm using a DS3231 RTC now, so it defaults to using that. To use the DS1307 you'll need to swap some lines in the code. EDIT: Apparently they've changed the comment editor and it eats any code contained in < and > symbols thinking they are HTML tags, even inside code blocks. Truly brilliant. Basically, change this on line 48:
Code:
 //#include DS1307RTC.h
#include DS3231RTC.h
To this: (ignore the missing < and >)
Code:
 #include DS1307RTC.h
 //#include DS3231RTC.h

Last edited by sink; 04-09-2012 at 03:44 PM.. Reason: Sigh. Tags...
sink is offline   Reply With Quote Quick reply to this message
Sponsored Links
Advertisement
 
Old 04-09-2012, 08:18 PM   #92
Gisimo
Algae Grower
 
PTrader: (0/0%)
Join Date: Apr 2012
Location: Denmark
Posts: 17
Default

Hi, and thanks.

I hope that will work - but i haven't found the set time pde file. Can anyone help me with a link so I can update the time on my ds1307 with arduino 0023?
Thanks
Gisimo is offline   Reply With Quote Quick reply to this message
Old 04-09-2012, 08:36 PM   #93
O2surplus
Planted Tank Enthusiast
 
O2surplus's Avatar
 
PTrader: (4/100%)
Join Date: Jan 2010
Location: "The Land of Fruits & Nuts"
Posts: 681
Default

Quote:
Originally Posted by Gisimo View Post
Hi, and thanks.

I hope that will work - but i haven't found the set time pde file. Can anyone help me with a link so I can update the time on my ds1307 with arduino 0023?
Thanks

Here's the sketch, and the library you'll need is attached to this post.
Code:
/*
 * Name:    timeset.pde
 * Author:    User "sink" at plantedtank.net forums
 * URL:        http://bitbucket.org/akl/tank-control
 *
 * This code sets the time on a DS1307 RTC chip attached to an Arduino
 * microcontroller board.  The time is set to the system time when the
 * sketch was compiled (using preprocessor macros), so make sure that:
 *
 *     1. Your system (PC) time is accurate.
 *     2. You recompile the sketch and then upload immediately.
 *
 * The time is skewed forward some seconds to accomodate for compilation and
 * upload time.  If you find the time is consistently off by the same amount,
 * you can modify the adjustment constant below.
 *
 * This code requires the following libraries: Wire, 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.  
 */

#include <Time.h>
#include <Wire.h>
#include <DS1307RTC.h>

/*
 * Time adjustment forward in seconds.  Used to compensate for
 * compilation/upload time.
 */
const int kAdjustment = 10;

/*
 * 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);
}

/*
 * 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 setup() {

  // convert compilation time and set system clock
  char const *date = __DATE__;
  char const *time = __TIME__;
  int sec, min, hour, day, month, year;
  
  char s_month[5];
  static const char month_names[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
  sscanf(date, "%s %d %d", s_month, &day, &year);
  month = (strstr(month_names, s_month)-month_names)/3;
  sscanf(time, "%d:%d:%d", &hour, &min, &sec);

  setTime(hour, min, sec, day, month+1, year);
  adjustTime(kAdjustment); // crude fwd correction
  time_t t = now();

  // set RTC
  RTC.set(t);

  Serial.begin(115200);
}

void loop () {
  digitalClockDisplay();  
  Serial.println(); 
  delay(1000);
}
Attached Files
File Type: zip libraries.zip (18.4 KB, 74 views)
O2surplus is online now   Reply With Quote Quick reply to this message
Old 04-10-2012, 07:50 AM   #94
Gisimo
Algae Grower
 
PTrader: (0/0%)
Join Date: Apr 2012
Location: Denmark
Posts: 17
Default

thank you so much
Gisimo is offline   Reply With Quote Quick reply to this message
Old 04-11-2012, 11:06 AM   #95
Gisimo
Algae Grower
 
PTrader: (0/0%)
Join Date: Apr 2012
Location: Denmark
Posts: 17
Default

Hi I have a Question, is it normal, that i need to press reset if there have been a power loss?[censored]
Gisimo is offline   Reply With Quote Quick reply to this message
Old 04-11-2012, 02:56 PM   #96
O2surplus
Planted Tank Enthusiast
 
O2surplus's Avatar
 
PTrader: (4/100%)
Join Date: Jan 2010
Location: "The Land of Fruits & Nuts"
Posts: 681
Default

Are you talking about the reset button on your Arduino? The Arduino should retain the dimming sketch indefinitely unless you upload a new sketch to it. The DS1307 RTC should keep the correct time as long as it's backup battery hold a charge. Unless your hardware has a defect a temporary power loss should not be a problem. The Arduino should just pickup and run it's program at whatever point that it should be at, as determined by the DS1307 RTC.
O2surplus is online now   Reply With Quote Quick reply to this message
Old 04-11-2012, 06:28 PM   #97
Gisimo
Algae Grower
 
PTrader: (0/0%)
Join Date: Apr 2012
Location: Denmark
Posts: 17
Default

Yes i am talking about the reset button on the arduino!! I accidentally pulled the wrong cord and when i plug it in again it didn't light up so i pressed the reset button and that got the lights on again!!
Maybe i shut have waited a bit longer or is there somthing wrong ?
Gisimo is offline   Reply With Quote Quick reply to this message
Old 04-11-2012, 06:34 PM   #98
sink
Algae Grower
 
PTrader: (2/100%)
Join Date: Jun 2011
Location: Boston, MA
Posts: 148
Default

Quote:
Originally Posted by O2surplus View Post
Are you talking about the reset button on your Arduino? The Arduino should retain the dimming sketch indefinitely unless you upload a new sketch to it. The DS1307 RTC should keep the correct time as long as it's backup battery hold a charge. Unless your hardware has a defect a temporary power loss should not be a problem. The Arduino should just pickup and run it's program at whatever point that it should be at, as determined by the DS1307 RTC.
This is correct -- you definitely shouldn't have to reset.

Make sure your RTC and battery are properly connected and that your coin battery has sufficient charge. The battery isn't purely to provide backup voltage -- the RTC expects it to be there and will act erratically otherwise.
sink is offline   Reply With Quote Quick reply to this message
Old 04-11-2012, 06:44 PM   #99
sink
Algae Grower
 
PTrader: (2/100%)
Join Date: Jun 2011
Location: Boston, MA
Posts: 148
Default

Quote:
Originally Posted by Gisimo View Post
Yes i am talking about the reset button on the arduino!! I accidentally pulled the wrong cord and when i plug it in again it didn't light up so i pressed the reset button and that got the lights on again!!
Maybe i shut have waited a bit longer or is there somthing wrong ?
Something else is wrong. What sketch is loaded on the Arduino? Usually when this happens it means the reset is also resetting the time on the RTC, either through the sketch or though power cycling the RTC.

Also rather than using the timeset.pde sketch, I'd recommend using the python script in the latest version of my code. It's a bit more work to set up since you have to download Python, but it is less error prone and when something goes wrong it is easier to see what is going on.
sink is offline   Reply With Quote Quick reply to this message
Old 04-11-2012, 08:17 PM   #100
Gisimo
Algae Grower
 
PTrader: (0/0%)
Join Date: Apr 2012
Location: Denmark
Posts: 17
Default

I use the sketch from this site! And my RTC is ok,, when i use the reset button the light start were it got cut off it doesn't start at the beginning! So what is wrong then?
Gisimo is offline   Reply With Quote Quick reply to this message
Old 04-11-2012, 08:33 PM   #101
sink
Algae Grower
 
PTrader: (2/100%)
Join Date: Jun 2011
Location: Boston, MA
Posts: 148
Default

Quote:
Originally Posted by Gisimo View Post
I use the sketch from this site! And my RTC is ok,, when i use the reset button the light start were it got cut off it doesn't start at the beginning! So what is wrong then?
Ok good. I just want to make sure are were using the latest version of tank_control.pde, with I assume the minor modification on lines 48/49 to use the DS1307. This is correct?

Do you mean that the light level resumes at the correct level for where it got cut off, or at the correct level for the current time? Have you confirmed the clock advances correctly when only running on the battery?

If you are using the correct sketch (with the minor modifications), the clock advances correctly both when powered and on battery, and the sketch properly dims the lights and resumes the lights correctly when power is reapplied then you have a hardware problem somewhere.

I'd speculate that the Arduino is failing to boot the first time you apply power, but it works once your power supply has stabilized. So I'd have a look at your power supply/regulators. How are you supplying power to the Arduino/RTC?
sink is offline   Reply With Quote Quick reply to this message
Old 04-11-2012, 08:56 PM   #102
Gisimo
Algae Grower
 
PTrader: (0/0%)
Join Date: Apr 2012
Location: Denmark
Posts: 17
Default

I use a 12 volt dc power supply connected to the dc input on the arduino uno and i use the same for my lights! The light faded out at the right time to night so i think it is ok? I have just yesterday change the lines you told me about and it helped alot!! My rtc i connected to gnd and +5 v on the arduino and the two other is connected to a4 and a5 directly!!
Gisimo is offline   Reply With Quote Quick reply to this message
Old 04-12-2012, 09:42 AM   #103
Gisimo
Algae Grower
 
PTrader: (0/0%)
Join Date: Apr 2012
Location: Denmark
Posts: 17
Default

my ds1307 is this: http://www.sparkfun.com/products/99

i measure the voltage on the battery and it is 3,34 volt so it is ok....

the sda is wired to A4 and sdl is wired to A5. the +5 volt and the gnd from ds1307 is connected to +5 volt and gnd on the Arduino uno.

i use 12 volt to my lamp and to run the arduino i have put an one way diode on the red wire and connect it to the dc plug.

i controle the dimming with two irf730 (like a motor controle) and it Worked fine so no problems there...

I donīt now how to find out if my ds1307 i working correctly!!!!! help please......
Gisimo is offline   Reply With Quote Quick reply to this message
Old 04-12-2012, 02:36 PM   #104
O2surplus
Planted Tank Enthusiast
 
O2surplus's Avatar
 
PTrader: (4/100%)
Join Date: Jan 2010
Location: "The Land of Fruits & Nuts"
Posts: 681
Default

Sink's "tank control" sketch features a serial output for the clock. I suggest re- flashing your arduino with the Timeset sketch and then the tank control sketch. Then open the serial monitor in the Arduino IDE while your still connected to your board. You may have to adjust the baud rate to see the output from the clock, but there's only a few choices to choose from. IF you see the time output in the serial monitor then you know the clock is working ok. If not- continue trouble shooting the hardware until you do get a clock output in the serial monitor.
O2surplus is online now   Reply With Quote Quick reply to this message
Old 04-12-2012, 03:09 PM   #105
sink
Algae Grower
 
PTrader: (2/100%)
Join Date: Jun 2011
Location: Boston, MA
Posts: 148
Default

Quote:
Originally Posted by O2surplus View Post
Sink's "tank control" sketch features a serial output for the clock. I suggest re- flashing your arduino with the Timeset sketch and then the tank control sketch. Then open the serial monitor in the Arduino IDE while your still connected to your board. You may have to adjust the baud rate to see the output from the clock, but there's only a few choices to choose from. IF you see the time output in the serial monitor then you know the clock is working ok. If not- continue trouble shooting the hardware until you do get a clock output in the serial monitor.
While that is true on the older versions, the newer versions of my code actually use the serial output to talk with the PC to set & query time, so they don't spam the clock time continuously as it would interfere with the protocol.

Use the one linked below, which I just hacked up to include logging. When you've uploaded it fire up the serial console in Arduino and take note of the clock time and counter and compare it to your watch. Then disconnect the power for a few minutes. When you start it back up and look at the serial console, the clock shouldn't have lost time with respect to your watch. If it has, then the RTC clock isn't advancing on battery.

http://pastebin.ca/2135363
sink is offline   Reply With Quote Quick reply to this message
Reply

Quick Reply
Message:
Options

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.



Thread Tools
Display Modes

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

Forum Jump


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


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