Arduino coding and pinouts 2560? - The Planted Tank Forum
 
LinkBack Thread Tools Display Modes
post #1 of 14 (permalink) Old 10-24-2015, 05:02 AM Thread Starter
Planted Tank Obsessed
 
PTrader: (0/0%)
Join Date: Aug 2011
Location: australia
Posts: 352
Arduino coding and pinouts 2560?

Okay im having some trouble getting my rtc and the code to work with it I am rather new to this so forgive me for that

My goals for this is just to control my lights for now

So a simple pwm putput for that will be fantastic

And I dont require a screen,for now im happy to plug my laptop into the usb port and set times/fades ect with the code

About as far as iv got with the arduino is to do some basic on and off for three leds and im aiming to be using 3 pwm channels at this stage


This is the rtc I have, will take it with me amd grab a battery for it soon,



Now id like to learn during this process so if someone has some example code thats easy to underatand id be very apreciative thanks

Sent from my D6653 using Tapatalk
boxhead1990 is offline  
Sponsored Links
Advertisement
 
post #2 of 14 (permalink) Old 10-24-2015, 09:02 AM
Algae Grower
 
PTrader: (2/100%)
Join Date: Nov 2012
Location: Alaska
Posts: 73
You might get better help on an arduino board, but post a sketch of your circuit and your code so folks can see what you are trying right now. It's hard to tell if that RTC includes the required pull up resistors for the I2C communication protocol or not. I usually work with AVR's, not an arduino platform, but they are similar enough so I can try and help you out.
AKnickolai is offline  
post #3 of 14 (permalink) Old 10-24-2015, 03:00 PM
Algae Grower
 
PTrader: (0/0%)
Join Date: Dec 2014
Location: Toronto
Posts: 107
The arduino boards have the pull-ups built in for the SDA/SCL lines (not built in on boards with additional lines SDA1/SCL1 such as on the Mega or Due).

Post a picture of where your wiring leads to on the Arduino, as well as your code. Looks like that RTC has jumpers for the address, try flashing the I2C scanner on your arduino to make sure the board can see the RTC and determine the address.

Arduino Playground - I2cScanner
renesis is offline  
 
post #4 of 14 (permalink) Old 10-24-2015, 03:44 PM
Children Boogie
 
mistergreen's Avatar
 
PTrader: (13/100%)
Join Date: Dec 2006
Location: Cincinnati
Posts: 16,743
here's a library for your RTC

https://github.com/trunet/DS3231RTC

You can use the standard Arduino Time.h & TimeAlarms.h library with your RTC to make scheduled events.


To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
mistergreen is offline  
post #5 of 14 (permalink) Old 10-24-2015, 05:07 PM
Algae Grower
 
PTrader: (0/0%)
Join Date: Jul 2015
Location: Chicago area
Posts: 17
cairo is offline  
post #6 of 14 (permalink) Old 10-25-2015, 05:22 AM Thread Starter
Planted Tank Obsessed
 
PTrader: (0/0%)
Join Date: Aug 2011
Location: australia
Posts: 352
Quote:
Originally Posted by renesis View Post
The arduino boards have the pull-ups built in for the SDA/SCL lines (not built in on boards with additional lines SDA1/SCL1 such as on the Mega or Due).

Post a picture of where your wiring leads to on the Arduino, as well as your code. Looks like that RTC has jumpers for the address, try flashing the I2C scanner on your arduino to make sure the board can see the RTC and determine the address.

Arduino Playground - I2cScanner
Sda is pinned to a4
Scl is to a5

Sent from my D6653 using Tapatalk
boxhead1990 is offline  
post #7 of 14 (permalink) Old 10-25-2015, 05:24 AM Thread Starter
Planted Tank Obsessed
 
PTrader: (0/0%)
Join Date: Aug 2011
Location: australia
Posts: 352
I havent really gone very far with code yet just playing woth an example fading sketch atm

Il have a look at that light schedule sketch and see what I can make of it

Sent from my D6653 using Tapatalk

Couldnt get the rtc board to play ball but im gunna give that sketch in the link a go and buy the rtc he is using and go from there, altho his code is a bit complicated

Sent from my D6653 using Tapatalk

Last edited by Darkblade48; 10-28-2015 at 03:59 AM. Reason: Please use the edit function for back to back posts
boxhead1990 is offline  
post #8 of 14 (permalink) Old 10-25-2015, 06:53 PM
Algae Grower
 
PTrader: (0/0%)
Join Date: Jul 2015
Location: Chicago area
Posts: 17
@boxhead1990
On Arduino Mega board Sda is pin 20, and Scl pin 21.
cairo is offline  
post #9 of 14 (permalink) Old 10-25-2015, 08:56 PM
Wannabe Guru
 
ichy's Avatar
 
PTrader: (3/100%)
Join Date: Apr 2015
Location: United States
Posts: 1,461
Can you see anything from the RTC in your serial monitor window?

+++++++++++++++++++
Just started Nano

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

++++++++++++++++++++
ichy is offline  
post #10 of 14 (permalink) Old 10-26-2015, 01:01 AM Thread Starter
Planted Tank Obsessed
 
PTrader: (0/0%)
Join Date: Aug 2011
Location: australia
Posts: 352
I can now thanks for that info alp my referances said to plug into a4 and a5 so next step is to get some code together to make some things happen I guess but I have no set time sketches in my ds3232 libraries

Sent from my D6653 using Tapatalk
boxhead1990 is offline  
post #11 of 14 (permalink) Old 10-26-2015, 01:34 AM
Wannabe Guru
 
ichy's Avatar
 
PTrader: (3/100%)
Join Date: Apr 2015
Location: United States
Posts: 1,461
A well written sketch should have a comments section that you can read for pin assignments.
Worse case, you can scan the code to see what pins are being called. You can either move your wires to those pins or some cases just change the code to match your pin assignments, just be aware of analog and digital pins.

+++++++++++++++++++
Just started Nano

To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.

++++++++++++++++++++
ichy is offline  
post #12 of 14 (permalink) Old 10-28-2015, 03:12 AM Thread Starter
Planted Tank Obsessed
 
PTrader: (0/0%)
Join Date: Aug 2011
Location: australia
Posts: 352
Got it all going now

Sketch im using won't send signal to channel 3 even tho I followed the authors comments corectly hmmmmm



Sent from my D6653 using Tapatalk
boxhead1990 is offline  
post #13 of 14 (permalink) Old 10-29-2015, 06:11 AM Thread Starter
Planted Tank Obsessed
 
PTrader: (0/0%)
Join Date: Aug 2011
Location: australia
Posts: 352
Im gunna be still attempting to write my own code as the code im using the lights bounce between there settings instead of fading between each

The sketch says it should fade between the higher value and lower value and vice versa but from what I can see its not ramping its just getting to the marker in the code and then switching the intensity on the spot

Which isnt why I went this route for my lights but its definatly nice not having to use a timer and replacing the digital timer every now and then as the relay in ot stops working and no longer clicks on and off(I hated the mechinal wall socket timers as they are so clunky to set)

Sent from my D6653 using Tapatalk
boxhead1990 is offline  
post #14 of 14 (permalink) Old 11-03-2015, 09:43 PM Thread Starter
Planted Tank Obsessed
 
PTrader: (0/0%)
Join Date: Aug 2011
Location: australia
Posts: 352
okay so here is my current code that i am using i will update with with a time lapse tomorow or when i remember and i will try and atleast remember to do upload it ,

but i will say it ramps up pretty nicely but ramping down it only does a little bit of dimming then its pretty much straight off in comparison but today i actually remembered to set up the go pro to show it dimming and ramping at 10 second intervals then changed my mind and set it on 2 second intervals lol

i may leave it go for 24 hours or so and that will show me exactly what the lights are doing anyways

now please note this isnt my code this is benjaf's code

i have 2 white channels a small string of 4 (ones actually dead on arival and il replace it later) and a 2 bigger strings of 16 diodes

now the bigger strings start up alot slower when runing from the same pwm channels so i have set my small strings on there own channel and i havent fine tuned it to start all the whites at the same rate but i dont think i will as it sorta seems like the lights slowly move across till its all being lit up

https://www.plantedtank.net/forums/20...ml#post8494162


Code:
/*
 * Name:	LightController.ino
 * Author:	User "benjaf" at plantedtank.net forums
 * URL:		https://github.com/benjaf/LightController
 * This example is set up for 2 channels with a maximum of 10 points.
 * Anything that may require editing is labeled with �CHANGE�
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. 
 */

// ----------------------- RTC Library -----------------------
// Use Wire and RTClib (https://github.com/adafruit/RTClib):
// Please note that there are a significant differences between the original JeeLabs RTCLib and the Adafruit fork!
#include <Wire.h>
#include "RTClib.h"
#include "ChannelManager.h"

// ----------------------- Constants -----------------------
// �CHANGE�
const int MaxChannels = 4;   // Max number of channels, change if more or less are required
const int MaxPoints = 25;    // Max number of light intensity points, change if more or less are required

// ----------------------- Variables -----------------------
// RTC
RTC_DS1307 RTC;

// Time
DateTime CurrentTime;

// ----------------------- Lights -----------------------

// Schedule Point format: (Hour, Minute, Intensity)
// Difference in intensity between points is faded/increased gradually
// Example: 
// 	Channels[0].AddPoint(8, 0, 0);
//	Channels[0].AddPoint(8, 30, 255);
//	Channels[0].AddPoint(11, 0, 255);
//  ...
//
// Explanation:
//  00:00 - 08:00 -> Light OFF
//  08:00 - 08:30 -> Increment light towards Fully ON
//  08:30 - 11:00 -> Light Fully ON
//
// Min intensity value: 0 (OFF)
// Max intensity value: 255 (Fully ON)
//
// If only 1 point is added, channel always maintains value of that point
//
// There is the option of which fade mode to use.
// Basically there are 2 modes: Linear and Exponential
// - Linear is what you would expect - 50% on = 50% duty cycle.
// - Exponential is an estimation of how our eyes react to brightness. The 'real' formula is actually inverse logarithmic,
// 	 but for our use exponential is close enough (feel free to add more fade modes if you disagree) - and much faster to compute!

Channel Channels[MaxChannels];
Point Points[MaxChannels][MaxPoints];


// Add more timing definitions here if more channels are added:
// �CHANGE�
void InitializeChannels(int channels) {
	// Channel 0: (Example)
	// This is an example of a typical lighting schedule (Lights on 08:30 - 19:00 w. 30 minutes of sunrise / sunset added)
	// using linear fading on pin 10.
	
	//int channelNo = 0;
	//int pin = 10;
	//Channels[channelNo] = Channel(pin, MaxPoints, fademode_linear, Points[channelNo]);
	//Channels[channelNo].AddPoint(8, 0, 0);
	//Channels[channelNo].AddPoint(8, 30, 255);
	//Channels[channelNo].AddPoint(19, 0, 255);
	//Channels[channelNo].AddPoint(19, 30, 0);
  	
	// Channel 0:
	int channelNo = 0;	// Currently editing channel 0
	int pin = 44;		// Channel 0 uses pin 44 white
	Channels[channelNo] = Channel(pin, MaxPoints, fademode_linear, Points[channelNo]);	// Initialize channel and choose FadeMode
	Channels[channelNo].AddPoint(9, 0, 255);	// Add Point (can also use decimal values ranging from 0 to 1 if you prefer)
	Channels[channelNo].AddPoint(10, 0, 135);
 Channels[channelNo].AddPoint(13, 30, 135);
	Channels[channelNo].AddPoint(19, 30, 135);
	Channels[channelNo].AddPoint(20, 15, 255);
	
	// Channel 1:
	channelNo = 1;	// Currently editing channel 1
	pin = 45;		// Channel 1 uses pin 45 blue
	Channels[channelNo] = Channel(pin, MaxPoints, fademode_linear, Points[channelNo]);
	Channels[channelNo].AddPoint(8, 0, 253);
	Channels[channelNo].AddPoint(9, 30, 100);
  Channels[channelNo].AddPoint(13, 30, 50);
	Channels[channelNo].AddPoint(19, 30, 50);
	Channels[channelNo].AddPoint(20, 0, 253);

 // Channel 2:
 channelNo = 2;  // Currently editing channel 1
  pin = 46;   // Channel 1 uses pin 46 red
  Channels[channelNo] = Channel(pin, MaxPoints, fademode_linear, Points[channelNo]);
  Channels[channelNo].AddPoint(8, 0, 254);
  Channels[channelNo].AddPoint(9, 30, 160);
  Channels[channelNo].AddPoint(13, 30, 100);
  Channels[channelNo].AddPoint(19, 30, 100);
  Channels[channelNo].AddPoint(20, 0, 254);
  // Channel 3:
  channelNo = 3;  // Currently editing channel 0
  pin = 8;   // Channel 0 uses pin 44 white(12v line)
  Channels[channelNo] = Channel(pin, MaxPoints, fademode_linear, Points[channelNo]);  // Initialize channel and choose FadeMode
  Channels[channelNo].AddPoint(9, 0, 255);  // Add Point (can also use decimal values ranging from 0 to 1 if you prefer)
  Channels[channelNo].AddPoint(10, 0, 135);
 Channels[channelNo].AddPoint(13, 30, 135);
  Channels[channelNo].AddPoint(19, 30, 135);
  Channels[channelNo].AddPoint(20, 0, 255);
}

// ----------------------- Functions -----------------------
long lastUpdateTime = 0;

// Update light intensity values
void UpdateLights(DateTime currentTime)
{
	long now = Seconds(currentTime.hour(), currentTime.minute(), currentTime.second());	// Convert current time to seconds since midnight
	if(now != lastUpdateTime)  	// Perform update only if there is a perceivable change in time (no point wasting clock cycles otherwise)
	{
		for(int channel = 0; channel < MaxChannels; channel++)    		// For each Channel
		{
			analogWrite(Channels[channel].GetPin(), Channels[channel].GetLightIntensityInt(now));	// Get updated light intensity and write value to pin (update is performed when reading value)
		}
	}
	lastUpdateTime = now;
}


// Convert HH:mm:ss -> Seconds since midnight
long Seconds(int hours, int minutes, int seconds) {
	return ((long)hours * 60 * 60) + (minutes * 60) + seconds ;
}

// ----------------------- Setup -----------------------
void setup() {
	// Initialize channel schedules
	InitializeChannels(MaxChannels);
	
	// Clock
	Wire.begin();
	RTC.begin();
	//RTC.adjust(DateTime(__DATE__, __TIME__));  // Set RTC time to sketch compilation time, only use for 1 (ONE) run. Will reset time at each device reset!
}

// ----------------------- Loop -----------------------
void loop() {
	// Get current time
	CurrentTime = RTC.now();
	
	// Update lights
	UpdateLights(CurrentTime);
}
boxhead1990 is offline  
Reply

Tags
None

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:
OR

Log-in










Thread Tools
Show Printable Version Show Printable Version
Email this Page Email this Page
Display Modes
Linear Mode Linear Mode



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
Trackbacks are On
Pingbacks are On
Refbacks are On

 
For the best viewing experience please update your browser to Google Chrome