Re: Time for additional Ethernet add-on with PWM output
Posted: Mon Jan 14, 2013 4:32 pm
Exactly...
Read that post.
Sent from my Galaxy S3 using Tapatalk 2
Read that post.
Sent from my Galaxy S3 using Tapatalk 2
Community discussion about Reef Angel Controllers and reefing related subjects
https://forum.reefangel.com/
Code: Select all
// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f);
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
Code: Select all
#include <Wire.h>
#define DS1307_I2C_ADDRESS 0x68 //set Real Time Clock Location
#include <SPI.h>
#include <Ethernet.h>
int ontime = 420 ; // Time of day (in minutes from midnite eg. 450=7:30 AM) to begin photoperiod fade in 420 = 7 AM
int photoperiod = 420 ; // Amount of time array is on at full power in minutes 510 = 8.5 hours * Light ramps up beginning at
int blueramptime = 60 ; // Time for blue LEDs to dim on and off in minutes * 10 AM and full pwer at 11:30 AM
int whiteramptime = 30 ; // Time for white LEDs to dim on (after blues) and off (before blues) in minutes * Ramp down at 8 PM and off at 9:30 PM
int moonontime = 1290 ; // Time in Minutes that moonlight comes on 1290=9:30 PM
int moontime = 120 ; // Amount of time array is on at moon power in minutes
int moonpower = 3 ; // Moon Intensity 0-255
int bluemin = 0 ; // Minimum dimming value of blue LEDs, range of 0-255
int bluemax = 255 ; // Maximum dimming value of blue LEDs, range of 0-255
int whitemin = 0 ; // Minimum dimming value of white LEDs, range of 0-255
int whitemax = 255 ; // Maximum dimming value of white LEDs, range of 0-255
int fan1 = 14; // Fanspeed digital pin 12
int fan2 = 15; // Fanspeed digital pin 13
int blue1 = 9; // blue LEDs connected to digital pin 9 (pwm)
int white1 = 6; // white LEDs connected to digital pin 6 (pwm)
int blue2 = 5; // blue LEDs connected to digital pin 5 (pwm)
int white2 = 3; // white LEDs connected to digital pin 3 (pwm)
int percentbright = 0 ; // Percentage of brightness holder
int x = 1 ; // Number holder for countdowns
int relmoontime = 0 ; // Relative (to current time if turned on during moon time) moon on time
const int buttonPin = 41; // The number of the pushbutton Digital pin
int buttonState = 0; // Variable for reading the pushbutton status
int analogPinb = 0; // Potentiometer connected to analog pin 0 for blue
int analogPinw = 2; // Potentiometer connected to analog pin 2 for white
int bval = 0; // Variable to store the blue read value
int wval = 0; // Variable to store the white read value
int daybyminute = 1; // Current timeof day in minutes
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0D, 0x28, 0x0E };
IPAddress ip(192,168,1, 114);
IPAddress remoteserver(198,171,134,6); // forum.reefangel.com
EthernetClient clientout;
unsigned long lastmillisout=millis();
unsigned long lastmillisin=millis();
boolean sending=false;
char strout[400];
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
/* R T C C L O C K D S 1 3 0 7 */
byte decToBcd(byte val) // Convert normal decimal numbers to binary coded decimal
{
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) // Convert binary coded decimal to normal decimal numbers
{
return ( (val/16*10) + (val%16) );
}
void getDateDs1307(byte *second,
byte *minute,
byte *hour,
byte *dayOfWeek,
byte *dayOfMonth,
byte *month,
byte *year)
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
// A few of these need masks because certain bits are control bits
*second = bcdToDec(Wire.read() & 0x7f);
*minute = bcdToDec(Wire.read());
*hour = bcdToDec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
*dayOfWeek = bcdToDec(Wire.read());
*dayOfMonth = bcdToDec(Wire.read());
*month = bcdToDec(Wire.read());
*year = bcdToDec(Wire.read());
}
void ethernetmodule()//allows the ethernet module to run in loop without failure
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
while (client.available()) {
char c = client.read();
if (sending==false) Serial.write(c);
lastmillisin=millis();
}
while (Serial.available()){
memset(strout,0,sizeof(strout));
int b=Serial.readBytesUntil('>',strout,sizeof(strout));
client.print(strout);
client.print(">");
if (millis()-lastmillisin>2000)
{
// Serial.flush();
lastmillisin=millis();
client.stop();
}
}
if (millis()-lastmillisin>2000)
{
//Serial.flush();
lastmillisin=millis();
client.stop();
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
else
{
if (Serial.available())
{
memset(strout,0,sizeof(strout));
int b=Serial.readBytesUntil('\n',strout,sizeof(strout));
if (b>10)
{
sending=true;
Serial.print(strout);
Serial.println(" HTTP/1.0");
Serial.println();
if (clientout.connect(remoteserver, 80))
{
clientout.print(strout);
clientout.println(" HTTP/1.0");
clientout.println();
lastmillisout=millis();
}
}
}
while (clientout.available()) {
char c = clientout.read();
Serial.print(c);
if (millis()-lastmillisout>8000)
{
lastmillisout=millis();
clientout.stop();
sending=false;
}
}
if (!clientout.connected())
{
clientout.stop();
sending=false;
}
}
if (millis()-lastmillisout>8000)
{
lastmillisout=millis();
clientout.stop();
sending=false;
}
//GET /status/submitp.aspx?t1=806&t2=0&t3=0&ph=441&id=test&em=0&rem=0&key=&atohigh=0&atolow=0&r=0&ron=0&roff=255 HTTP/1.0
//GET /status/submitp.asp?t1=806&t2=0&t3=0&ph=441&id=test&em=0&rem=0&key=&atohigh=0&atolow=0&r=0&ron=0&roff=255 HTTP/1.0
//Host: forum.reefangel.com
//
//GET /status/submitp.aspx?t1=806&t2=0&t3=0&ph=441&id=test&em=0&rem=0&key=&atohigh=0&atolow=0&r=0&ron=0&roff=255 HTTP/1.0
//Host: forum.reefangel.com
//Connection: Keep-Alive
}
void onesecond() //function that runs once per second while program is running
{
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
}
void setup() {
// initialize the manual pushbutton pin as an input:
pinMode(buttonPin, INPUT);
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
Wire.begin();
// Open serial communications and wait for port to open:
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
// Ethernet connection ok and server started
Serial.print(hour, DEC);
Serial.print(":");
Serial.print(minute, DEC);
Serial.print(":");
Serial.print(second, DEC);
Serial.print(" ");
Serial.print(month, DEC);
Serial.print("/");
Serial.print(dayOfMonth, DEC);
Serial.print("/");
Serial.println(year, DEC);
// print end
analogWrite(blue1, bluemin);
analogWrite(white1, whitemin);
analogWrite(blue2, bluemin);
analogWrite(white2, whitemin);
analogWrite(fan1, 0);
analogWrite(fan2, 0);
}
void loop()
{
ethernetmodule();//loop the ethernet code
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
int bluerampup;
if (daybyminute >= ontime) {
bluerampup = (blueramptime - (daybyminute - ontime));
if (bluerampup <= 0)
bluerampup = 1;
}
else {
bluerampup = blueramptime;
}
int whiterampup;
if (daybyminute >= (ontime + blueramptime)) {
whiterampup = (((whiteramptime + blueramptime) - (daybyminute - ontime - bluerampup)));
if (whiterampup <= 0)
whiterampup = 1;
}
else {
whiterampup = whiteramptime;
}
int whiterampdown;
if ((ontime + photoperiod + blueramptime + whiteramptime) <= daybyminute){
whiterampdown = ((ontime + photoperiod + blueramptime + (2*whiteramptime)) - daybyminute);
if (whiterampdown < 0)
whiterampdown = 1;
}
else {
whiterampdown = whiteramptime;
}
int bluerampdown;
if ((ontime + photoperiod + blueramptime + (2*whiteramptime)) <= daybyminute) {
bluerampdown = ((ontime + photoperiod + (2*blueramptime) + (2*whiteramptime)) - daybyminute);
if (bluerampdown < 0)
bluerampdown = 1;
}
else{
bluerampdown = blueramptime;
}
int relmoontime;
if (moonontime + moontime - 1440 > 0) // If moontime goes beyond midnite
if (daybyminute < 1440) // If current time less than midnite
relmoontime = (moonontime + moontime - daybyminute); //rel moon time minus midnite
else
relmoontime = (moonontime +moontime - (1440 - daybyminute) ); //re moon time minus midnite minus time after midnite
else
if (daybyminute>=moonontime)
relmoontime= moontime + moonontime - daybyminute;
else
relmoontime = 1;
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
if (daybyminute >= ontime && daybyminute <= (ontime + blueramptime + whiteramptime)) //if time is in range of fade in, start fading in
{
if (bluerampup >= blueramptime)
bluerampup = blueramptime;
// fade blue LEDs in from min to max.
for (int i = bluemin ; i <= bluemax; i++) // setting blues in smooth rampup
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH Then go manual
if (buttonState == HIGH) {
i = 255;
}
percentbright = int(i/2.55); // Convert the 0-255 to a percentage
analogWrite(blue1, i);
analogWrite(blue2, i);
if (i <=75) {
analogWrite(fan1, 70);
}
else {
analogWrite(fan1, i);
}
int countdown = ((bluerampup*60)/bluemax); // calculates seconds to next step based on ramp time
if (countdown < 1)
countdown = 1;
while (countdown>0)
{
onesecond(); // updates clock once per second
countdown--;
}
}
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
int countdown = 61; // wait one minute after rampup to account for rounding difference
while (countdown>0)
{
onesecond(); // updates clock once per second
countdown--;
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH Then go manual
if (buttonState == HIGH)
countdown = 0;
}
// fade white LEDs in from min to max.
if (whiterampup >= whiteramptime)
whiterampup = whiteramptime;
analogWrite(blue1, bluemax);
analogWrite(blue2, bluemax);
analogWrite(fan1, 255);
for (int i = whitemin; i <= whitemax; i++) // setting white in smooth rampup
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH Then go manual
if (buttonState == HIGH) {
i = whitemax;
}
percentbright = int(i/2.55);
analogWrite(white1, i);
analogWrite(white2, i);
if (i <=75) {
analogWrite(fan2, 70);
}
else {
analogWrite(fan2, i);
}
int countdown = ((whiterampup*60)/whitemax); // calculates seconds to next step
if (countdown < 1)
countdown = 1;
while (countdown>0)
{
onesecond(); // updates clock once per second
countdown--;
}
}
countdown = 61; // wait one minute after rampup to account for rounding difference
while (countdown>0)
{
onesecond(); // updates clock once per second
countdown--;
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH Then go manual
if (buttonState == HIGH)
countdown = 0;
}
}
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
if (daybyminute >= (ontime + blueramptime + whiteramptime) && daybyminute <= (ontime + blueramptime + whiteramptime + photoperiod)) // if time is in range of photoperiod, turn lights on
//to maximum fade value
{
analogWrite(blue1, bluemax);
analogWrite(blue2, bluemax);
analogWrite(fan1, 255);
analogWrite(white1, whitemax);
analogWrite(white2, whitemax);
analogWrite(fan2, 255);
int countdown = 61; // wait one minute toaccount for rounding difference
while (countdown>0)
{
onesecond(); // updates clock once per second
countdown--;
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH Then go manual
if (buttonState == HIGH) {
countdown = 0;
}
}
}
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
if (daybyminute >= (ontime + photoperiod + blueramptime + whiteramptime) && (ontime + photoperiod + whiteramptime + 2*blueramptime) >= daybyminute)
{
// fade white LEDs out from max to min in increments of 1 point:
if (whiterampdown >= whiteramptime)
whiterampdown = whiteramptime;
for (int i = whitemax; i >= whitemin; i--) // setting i value 255 to 1
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH Then go manual
if (buttonState == HIGH) {
i = 0;
}
analogWrite(blue1, bluemax);
analogWrite(blue2, bluemax);
analogWrite(fan1, 255);
percentbright = (float)(i/(whitemax/100.0));
analogWrite(white1, i);
analogWrite(white2, i);
if (i <=75) {
analogWrite(fan2, 70);
}
else {
analogWrite(fan2, i);
}
int countdown = ((whiterampdown*60)/whitemax); // calculates seconds to next step
if (countdown < 1)
countdown = 1;
while (countdown>0)
{
onesecond(); // updates clock once per second
countdown--;
}
}
// fade blue LEDs out from max to min in increments of 1 point:
if (bluerampdown >= blueramptime)
bluerampdown = blueramptime;
for (int i = bluemax ; i >= bluemin; i--) // decrease from 255 to 1
{
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH Then go manual
if (buttonState == HIGH) {
i = 0;
}
percentbright = (float)(i/(bluemax/100.0));
analogWrite(blue1, i);
analogWrite(blue2, i);
if (i <=75) {
analogWrite(fan1, 70);
}
else {
analogWrite(fan1, i);
}
int countdown = ((bluerampdown*60)/bluemax); // calculates seconds to next step
if (countdown < 1)
countdown = 1;
while (countdown>0)
{
onesecond(); // updates clock once per second
countdown--;
}
}
int countdown = 61; // wait one minute to account for rounding difference
while (countdown>0)
{
onesecond(); // updates clock once per second
countdown--;
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH Then go manual
if (buttonState == HIGH) {
countdown = 0;
}
}
}
getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
daybyminute = ((hour * 60) + minute); //converts time of day to a single value in minutes
if (daybyminute >= moonontime) //|| If it is time to turn on the moonlights
{
for (int i = (relmoontime * 60)-65 ; i >= 0; i--) // relative moon time countdown
{ // if time is in range of photoperiod, turn lights on to maximum fade value
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH Then go manual
if (buttonState == HIGH) {
i = 0;
}
analogWrite(blue1, moonpower);
analogWrite(blue2, moonpower);
if (moonpower <=75) {
analogWrite(fan1, 0);
}
else {
analogWrite(fan1, moonpower);
}
analogWrite(white1, 0);
analogWrite(white2, 0);
analogWrite(fan2, 0);
onesecond(); // updates clock once per second
}
analogWrite(blue1, 0);
analogWrite(blue2, 0);
analogWrite(fan1, 0);
analogWrite(white1, 0);
analogWrite(white2, 0);
analogWrite(fan2, 0);
relmoontime=0;
}
} // END LOOP
rimai wrote:What do you have in those ports?
Did you try without the equipment?
Curious to know if the problem is the equipment turning on or the relay being activated
rimai wrote:Does it mean that if you remove the DelayedOn(), everything works?
Ok I need to sync every hour right now i do every minute and try that. I'll also look at the now()rimai wrote:IDK...
I know it is something in the now() function that is causing your issue.
Take a look at the Time library.
Are you constantly reading the RTC in your code?
You should only read every hour or so and sync with it.