Arduino code Moon phase

fabyox

New member
Hi everyone.

Hi have four bank of led, 2 blues and 2 whites, and some led Smd 5050 to simulate moon phase.

in the future i want to add a wheter sketch aswell, but i am newbie, and for now moon phase is enough.

I have this symple sketch taht i like, cause is very simple, and update leds by seconds.

Code:
#include "Wire.h"
#include 
#define DS1307_ADDRESS 0x68  // This is the address

const int Blue1Pin = 10;
const int Blue2Pin = 5;
const int White3Pin = 3;
const int White4Pin = 11;

const unsigned long HOUR = 60 * 60;
const unsigned long MINUTE = 60;
const int TARGET_BRIGHTNESS = (255 * 4 / 5); // Target brightness is 3/4 of full

LiquidCrystal lcd(12, 13, 4, 6, 7, 8); 

/***** RTC Functions *******/
// Convert normal decimal numbers to binary coded decimal
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}
// 1) Sets the date and time on the ds1307
// 2) Starts the clock
// 3) Sets hour mode to 24 hour clock
// Assumes you're passing in valid numbers.
void setDateDs1307(byte second,        // 0-59
                   byte minute,        // 0-59
                   byte hour,          // 1-23
                   byte dayOfWeek,     // 1-7
                   byte dayOfMonth,    // 1-28/29/30/31
                   byte month,         // 1-12
                   byte year)          // 0-99
{
   Wire.beginTransmission(DS1307_ADDRESS);
   Wire.write(0);
   Wire.write(decToBcd(second));
   Wire.write(decToBcd(minute));
   Wire.write(decToBcd(hour));
   Wire.write(decToBcd(dayOfWeek));
   Wire.write(decToBcd(dayOfMonth));
   Wire.write(decToBcd(month));
   Wire.write(decToBcd(year));
   Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307(byte *second,
          byte *minute,
          byte *hour,
          byte *dayOfWeek,
          byte *dayOfMonth,
          byte *month,
          byte *year)
         {
         }

void setup()
{
  Wire.begin();
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  Wire.begin();

  // Change these values to what you want to set your clock to.
  // You probably only want to set your clock once and then remove
  // the setDateDs1307 call.
  second = 55;
  minute = 03;
  hour = 14;
  dayOfWeek = 4;  // Sunday is 0
  dayOfMonth = 4;
  month = 8;
  year = 11;
  setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
}

// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)  
{
  return ( (val/16*10) + (val%16) );
}

void loop()
{
  /////  Get time from RTC into RTCHour, RTCMinute, RTCSecond
  // Gets the date and time from the ds1307

   // Set the register pointer to 0 (Second)
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write((byte)0);
  Wire.endTransmission();

  //  Read Second, Minute, and Hour
  Wire.requestFrom(DS1307_ADDRESS, 3);

  int RTCSecond = bcdToDec(Wire.read());
  int RTCMinute = bcdToDec(Wire.read());
  int RTCHour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  
  lcd.setCursor(0, 0);
  lcd.print(RTCHour, DEC);
  lcd.print(":");
  lcd.print(RTCMinute, DEC);
  lcd.print(":");
  lcd.print(RTCSecond, DEC);
  lcd.print(" ");

  unsigned long time = RTCHour * HOUR + RTCMinute * MINUTE + RTCSecond;  // Time in seconds

  analogWrite(Blue1Pin,   brightness(time, 14*HOUR,          14*HOUR+7*MINUTE));
  analogWrite(Blue2Pin,   brightness(time, 14*HOUR+1*MINUTE, 14*HOUR+6*MINUTE));
  analogWrite(White3Pin, brightness(time, 14*HOUR+2*MINUTE,  14*HOUR+4*MINUTE));
  analogWrite(White4Pin, brightness(time, 14*HOUR+3*MINUTE,  14*HOUR+5*MINUTE));

  delay(1000);
}

byte brightness(unsigned long time, unsigned long fadeUpStart, unsigned long fadeDownStart)
{
  //  Mid day, light is at maximum brightness
  if (time >= fadeUpStart + 1*MINUTE  && time <= fadeDownStart)
    return TARGET_BRIGHTNESS;

  // Dawn:  fade up the light
  if (time >= fadeUpStart && time <= fadeUpStart + 1*MINUTE) //FADE UP
  {
    unsigned long seconds = time - fadeUpStart;  // Number of seconds into the fade time 
    return TARGET_BRIGHTNESS * seconds / (MINUTE*1);  // Fade up based on portion of interval completed.
  }

  // Evening: Fade down the light
  if (time >= fadeDownStart && time <=   <=   fadeDownStart + 1*MINUTE)  // Fading down
  {
    unsigned long seconds = (fadeDownStart + (MINUTE*1)) - time;  // Number of seconds remaining in the fade time 
    return TARGET_BRIGHTNESS * seconds / (MINUTE*1);  // Fade down based on portion of interval left.
  }

  // The remaining times are night and the lights is off
  return 0;  // Shouldn't get here
}

of course the period part it's just for test for now...

Now i would like to get Moon Phase Like:


Code:
int moon = 5;    

int iBlueIntensity;    //declare the integer of blue intensity
float fBlueIntensity;    //declare the floating point version of blue intensity

// RTC variables 
byte second, rtcMins, oldMins, rtcHrs, oldHrs, dayOfWeek, dayOfMonth, month, year, psecond;   


float fSecond, fHour, fMinute;        //turn the times read from the RTC into float for math ops
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; //declare variables to hold the times

  getDateDs1307(&second, &rtcMins, &rtcHrs, &dayOfWeek, &dayOfMonth, &month, &year); 

  fSecond = (float) second;            //sets fSecond as the float version of the integer second from the RTC
  fMinute = (float) minute;            //same as above, but for the minute
  fHour = (float) hour;                //ditto, but for the hour
  int lunarCycle = moonPhase(year, month, dayOfMonth); //get a value for the lunar cycle
  
   if ( ((hour == 14) && (minute < 7)) || (hour < 14))//Off at 730am.
  {
    fBlueIntensity = 0;        //...then we want the blue LED at the max brightness (255)
  }

  else if (hour > 14) //&& (hour < 19))             // On at 5pm
  {    
    fBlueIntensity = 255 * (((fHour - 17) + (fMinute / 59)) / 7);   //...set intensity out of 255 based on time since 17:00
  }
  else
  {
    fBlueIntensity = 0;
  }

 //---------account for the moon cycle with the blue LEDs---------//

  if (lunarCycle == 0)        //new moon
  {
    fBlueIntensity /= 2;
  }
  if ((lunarCycle == 1) || (lunarCycle == 7))    //cresent
  {
    fBlueIntensity /= 1.75 ;
  }
  if ((lunarCycle == 2) || (lunarCycle == 6))    //half moon
  {
   fBlueIntensity /= 1.5;
  }
  if ((lunarCycle == 3) || (lunarCycle == 5))    //gibbous
  {
    fBlueIntensity /= 1.25;
  }

  //full moon is full intensity 


  //----------FLOAT TO INT-------------//
  iBlueIntensity = (int) fBlueIntensity;

BBY

  //---prepare the intensities to be written to pin----//

  if (iBlueIntensity < 0) //if the blue intensity is less then 0, set it to 0
  {
    iBlueIntensity = 0;
  }
   analogWrite(moon, iBlueIntensity);
  delay(1000);

The answer is how can I tie together the two pieces of code?

thanks to all those who can help me
 
Back
Top