Jump to content
Pod Your Reef

New light controller with Arduino Mega...have a question


skull

Recommended Posts

Hello guys:

 

I am trying to build my aquarium led lighting controller with an Arduino Mega (ATmega1280chip), plus an RTC1307 real time clocking module.

 

Basically I have 6 channels (Different colors) connected to the arduino PWM pins with a CAT4101 driver, not using any resistors nor anything else on the circuit, just the CAT4101 and the RSET resistor.

 

Now my question is, I have taken several codes from the web for dimmering the leds using RTC time module, but none of them works on my arduino mega, I took a look also on the Pico Reef led in this website, but it doesn't work either.

 

 

Is there any code out there or something easy I can use for pwm my leds using rtc for the Arduino Mega?

Also should I use any type of resistor between the PWM ping on the CAT4101 and the Arduino PIN?

 

 

This is the circuit I am using:

catalyst_cat4101a.gif


EN/PWM pin on the CAT4101 goes directly to the digital pins 7-12 on my arduino Mega.

 

Any help or easy code for ArduinoPWM+RTC you can provide me so I can test my circuit?

 

 

 

Thank you in advace.

 

Steve.

Link to comment

This is one of the codes I have found and it will fit perfect for my needs, however when uploaded to the Arduino Mega board my leds keeps always ON at 100%..

//2.7 Gallon Pico Reef LED Light Controller Version 1.06

//Code written by plant_arms 2012-08-31

//Based on various code libraries avalable on the public domain along with coding compiled by the author.

 

#include <Wire.h>

#include "RTClib.h"



RTC_DS1307 RTC;



int bluePin = 9;				// blue LEDs connected to digital pin5

int whitePin = 10;			   // white LEDs connected to digital pin6



double blueFadeValue = 255;

double whiteFadeValue = 255;



double userWhiteMax =		4;	// maximum percentage intensity of white leds, 0-100 user defined

double userBlueMax =		 9;	// maximum percentage intensity of blue leds, 0-100 user defined

double blueMaxPwm;				 // variable used to convert the userBlueMax value into a PWM value

double whiteMaxPwm;				// variable used to convert the userWhiteMax value into a PWM value



int second;

int hour;

int minute; 

int blueLightsOn =		  9;	  // time of day (hour, 24h clock) to begin blue lights fading on

int whiteLightsOn =		10;	  // time of day (hour, 24h clock) to begin white lights fading on

int whiteLightsOff =	   20;	  // time of day (hour, 24h clock) to begin white lights fading out

int blueLightsOff =		21;	  // time of day (hour, 24h clock) to begin blue lights fading out

double fadeTime =		  60;	  // amount of time in minutes for the fade in/out to last **must be in 60 minute increments** 



 

void setup () {

	Serial.begin(115200);

	Wire.begin();

	RTC.begin();

 

  if (!RTC.isrunning()) { //uncomment the ! to be able to reset the datetime value

	// this will allow you to set RTC to the current computer date and time if uncommented and compiled 

	//RTC.adjust(DateTime(__DATE__, __TIME__));

  }

 

}



   void loop () {

	 setFade();

	 ledRtc();

	 SerialOut();

				  

   }  

   

	

 

 //void setFade class used to covert the user percentage input to a PWM value and set to white/blue  

   void setFade(){

	 blueMaxPwm = (userBlueMax*2.55);  //converts the user input value (0-100) to a PWM value (0-255)

	 whiteMaxPwm = (userWhiteMax*2.55); //converts the user input value (0-100) to a PWM value (0-255)   

   }

	

 

  

 //void ledRtc class to control the leds with the ds1307 timer, absolute value and using the time periods(<= etc) is to gain the correct fade position in case of a power outage

   void ledRtc(){

   

   DateTime now = RTC.now();

   hour = now.hour();

   minute = now.minute();

   second = now.second();

   int totBlueHrOn = hour - blueLightsOn;

   int totBlueHrOff = hour - blueLightsOff;

   int totWhiteHrOn = hour - whiteLightsOn;

   int totWhiteHrOff = hour - whiteLightsOff;

   int totalMinute = minute;

   abs(totBlueHrOn); 

   abs(totBlueHrOff);

   abs(totWhiteHrOn); 

   abs(totWhiteHrOff);

   abs(totalMinute);

   int totalBlueOn   = ((totBlueHrOn*60) + totalMinute);

   int totalBlueOff  = ((totBlueHrOff*60) + totalMinute);

   int totalWhiteOn  = ((totWhiteHrOn*60) + totalMinute);

   int totalWhiteOff = ((totWhiteHrOff*60) + totalMinute);

   

   //fades on blue lights from blueLightsOn through blueLightsOn plus fadeTime and outputs light intensity based on the current time   

   if((hour >= blueLightsOn)&&(hour < (blueLightsOn+(fadeTime/60)))&&(totalBlueOn < fadeTime)){ 

	 blueFadeValue = 255-(blueMaxPwm*(totalBlueOn/fadeTime)); 

	 analogWrite(bluePin, blueFadeValue);

   }

   

   //fades on white lights from whiteLightsOn to whiteLightsOn plus fadeTime and outputs light intensity based on the current time	

   if((hour >= whiteLightsOn)&&(hour <= (whiteLightsOn+(fadeTime/60)))&&(totalWhiteOn <= fadeTime)){  

	 whiteFadeValue = 255-(whiteMaxPwm*(totalWhiteOn/fadeTime)); 

	 analogWrite(whitePin, whiteFadeValue);

   }

  

   //puts blue lights up to full if the fade in period is complete and less than bluelightsoff, so full from 10 to 20:59

   if((hour >= (blueLightsOn+(fadeTime/60)))&&(hour < blueLightsOff)){ 

	 blueFadeValue = 255-blueMaxPwm;

	 analogWrite(bluePin, blueFadeValue);

   }

   

   //puts white lights up to full if the fade in period is complete and less than whitelightsoff, so full from 11 to 19:59

   if((hour >= (whiteLightsOn+(fadeTime/60)))&&(hour < whiteLightsOff)){ 

	 whiteFadeValue = 255-whiteMaxPwm;

	 analogWrite(whitePin, whiteFadeValue);

   }

   

   //fades out whites from whiteLightsOff to whiteLightsOff plus fadeTime and outputs light intensity based on the current time 

   if((hour >= whiteLightsOff)&&(hour < (whiteLightsOff+(fadeTime/60)))&&(totalWhiteOff <= fadeTime)){ 

	 whiteFadeValue = 255-(whiteMaxPwm - (whiteMaxPwm*(totalWhiteOff/fadeTime)));

	 analogWrite(whitePin, whiteFadeValue);

   

  }

  //fade out blues from blueLightsOff to blueLightsOff plus fadeTime and outputs light intensity based on the current time

   if((hour >= blueLightsOff)&&(hour < (blueLightsOff+(fadeTime/60)))&&(totalBlueOff <= fadeTime)){

	 blueFadeValue = 255-(blueMaxPwm - (blueMaxPwm*(totalBlueOff/fadeTime)));

	 analogWrite(bluePin, blueFadeValue);

   

  }

   

   //this sets the white lights to be off from end of whiteLightsOff plus fadeTime to midnight

   if((hour >= (whiteLightsOff+(fadeTime/60))&&(hour <= 24))){  

	 whiteFadeValue = 255;

	 analogWrite(whitePin, whiteFadeValue);

  }  

  

   //this sets the blue lights to be off from end of blueLightsOff plus fadeTime to midnight

   if((hour >= (blueLightsOff+(fadeTime/60))&&(hour < 24))){  

	 blueFadeValue = 255;

	 analogWrite(bluePin, blueFadeValue);

  } 

  

  //this sets the white lights to be off from midnight to whiteLightsOn in case of reset of power outage

   if((hour >= 0)&&(hour < whiteLightsOn)){  

	 whiteFadeValue = 255;

	 analogWrite(whitePin, whiteFadeValue);

  }  

  

   //this sets the blue lights to be off from midnight to blueLightsOn in case of reset or power outage

   if((hour >= 0)&&(hour < blueLightsOn)){  

	 blueFadeValue = 255;

	 analogWrite(bluePin, blueFadeValue);

  } 

}

	  

	   

  

 //SerialOut class to output print statements to the serial lcd display, time, temp, and light prcnt

   void SerialOut(){ 

	//DateTime now = RTC.now();

	//print the time pulled in with spaced 0's

	if (hour < 10) {

	   Serial.print('0');

	   Serial.print(hour);}

	 else {Serial.print(hour);}

	Serial.print(':');

	if (minute < 10) {

	   Serial.print('0');

	   Serial.print(minute);}

	 else {Serial.print(minute);}

	Serial.print(':');

	if (second < 10) {

	  Serial.print('0');

	  Serial.print(second);}

	  else { 

	Serial.print(second);}

	

	Serial.print(100-(whiteFadeValue/2.55));

	Serial.print("%");

		

	 Serial.print(100-(blueFadeValue/2.55));

	Serial.print("%");

	

	Serial.println();

	delay(1000);

 }
Link to comment

Do you have any coding experience at all? Did you change the pin designations in the example code above to match what you have the CAT4101's connected to? The code above is only set up for two channels. You will have to modify it to make it work with the number of channels you want to use.

 

As for resistors between the drivers and the Arduino, while it's not necessary, it's not a bad idea to have them. They won't improve or make you situation worse. Adding a resistor (something like a 10K) would limit current, and help prevent damage to the micro if things were to go wrong.

Link to comment

Evilc66:

 

Yes, I have some very basic code knowledge, but with basic I mean really really basic :-)

Yes, besides I plugued the Blue on the pin 9 and the white on the pin 10, and for now I am not using the other channels, once this start working I will replicate the channels.

 

The thing is..the leds stays always ON at 100% no matters the time hour minute nor secons, no matters if I use pin 9, pin 10, if I change the pin 10 to 13, it's always the same, like if the coding is not fitting well on my mega board.

 

 

Steve.

Link to comment

Do you have the grounds of the driver and the Arduino connected? Without a proper ground reference, you won't get very reliable results, if any at all.

 

You may have been better off taking baby steps to make sure that the circuit was at least working. Doing something like connecting a 10K linear pot to an analog input channel and using that to change the duty cycle on a pwm output channel would at least allow you to see that you have working hardware. From that point you can start making it more fancy. With your limited programming knowledge, you jumped into the deep end with a rock tied to your ankles.

Link to comment

Thank you Evilc66

 

The thing is...if I test the same circuit with the simplest blink and fade codes with analogWrite the leds fades perfectly, and blink, that is what confuses me, can that be a problem with my Arduino Mega board?

Link to comment

Thank you Evilc66

 

The thing is...if I test the same circuit with the simplest blink and fade codes with analogWrite the leds fades perfectly, and blink, that is what confuses me, can that be a problem with my Arduino Mega board?

Hmmm... sounds like you'll have to mess with this thing a bit more. I'm not that great at coding either - though luckily I got mine to work fairly quickly. What took me forever was altering the code so that the time adjustment moved in 10 minute increments instead of 1 minute increments... Haha.

 

Do you have the correct versions of the included libraries? If the simple dimming programs work it seems like the RTC code or the connection between it and the arduino is off... Maybe find a way to test the RTC separately and verify that's working then try to combine again?

Link to comment

Archived

This topic is now archived and is closed to further replies.

  • Recommended Discussions

×
×
  • Create New...