RTC DS1307 programming to control led

Hi

Have been working on this code to control a series of LED’s to cycle through the colour wheel over 24 hours, just wanted to know if i’m on the right track
thanks for your input.

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

const int analogInPin = A0;

const int analogOutredPin = 5;
const int analogOutgreenPin = 3;
const int analogOutbluePin = 6;
const boolean invert = true; 

int sensorValue = 0;
int outputValue = 0;   
int color = 255;
int R, G, B;  

void setup() {
 
  Serial.begin(9600); 
    
    setSyncProvider(RTC.get); 
  if(timeStatus()!= timeSet) 
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");      
}

void loop() 
{
   digitalClockDisplay();  
   delay(1000);
  
  sensorValue = analogRead(analogInPin);            
  
  outputValue = map(sensorValue, 0, 1023, 0, 255);  
  
  int brightness = 255;           
  hueToRGB( color, brightness);
  
  analogWrite(analogOutredPin, outputValue);     
  analogWrite(analogOutgreenPin, outputValue);
  analogWrite(analogOutbluePin, outputValue);
  
  color--;               
  if(color < 1)   
     color = 255;
       delay(10);

}  


void hueToRGB( int hue, int brightness)
{
    unsigned int scaledHue = (hue * 6);
    unsigned int segment = scaledHue / 256; 
                                            
    unsigned int segmentOffset = 
      scaledHue - (segment * 256); 

    unsigned int complement = 0;
    unsigned int prev = (brightness * ( 255 -  segmentOffset)) / 256;
    unsigned int next = (brightness *  segmentOffset) / 256;

  if(invert)
    {
      brightness = 255-brightness;
      complement = 255;
      prev = 255-prev;
      next = 255-next;
    }

    switch(segment ) {
    case 0:      // red
     R = brightness;
     G = next;
     B = complement;
    break;
   
    case 1:     // yellow
    R = prev;
    G = brightness;
    B = complement;
    break;
    case 2:     // green
    R = complement;
    G = brightness;
    B = next;
    break;
   
    case 3:    // cyan
    R = complement;
    G = prev;
    B = brightness;
    break;
    
    case 4:    // blue
    R = next;
    G = complement;
    B = brightness;
    break;
   
    case 5:      // magenta
    default:
    R = brightness;
    G = complement;
    B = prev;
    break;
  }
}

void digitalClockDisplay(){
  
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year()); 
  Serial.println(); 
}

void printDigits(int digits){
  
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}

const int analogOutredPin = 5;

How come the r in red is lower case?

  sensorValue = analogRead(analogInPin);

What is connected to the analog pin? There was nothing in your description of what you are trying to do that would clarify this.

  outputValue = map(sensorValue, 0, 1023, 0, 255);

Take a look at the map function. This function call is a resource-intense way to divide by 4.

  analogWrite(analogOutredPin, outputValue);     
  analogWrite(analogOutgreenPin, outputValue);
  analogWrite(analogOutbluePin, outputValue);
  
  color--;               
  if(color < 1)   
     color = 255;
       delay(10);

Each pin is going to change, in step, showing the same color, for 1010 milliseconds. The intensity of the LED will change from full off to full on in something over 250 seconds. The color will never change.

Have been working on this code to control a series of LED’s to cycle through the colour wheel over 24 hours, just wanted to know if i’m on the right track

No. You are not. Capitalization and punctuation aside, your code and your goal are not in sync.

Apologies for not being succinct, i was thinking the rtc module would be attached to the analogin, would read a value, before sending it back to the arduino to be mapped to a colour, say at 3:15pm the colour hue would be at a value like 100, using the huetoRGB function to do it.

The part i'm not clear on is getting the time data from the rtc mapped onto a colour value.

i was thinking the rtc module would be attached to the analogin

Depending on which RTC module you have, the RTC may be connected to TWO analog pins, and be communicated with using I2C. The time is not read using analogRead, though.

There should be some function(s) in the RTC library to get hours, minutes, and seconds. You can then determine how many seconds have elapsed since some time, such as midnight. I'd suggest, though, that you write the code to cycle through all the colors in a much shorter period of time, to test that the time to hue to color conversion is working.

In the hueToRGB function, hue and brightness are both inputs. They both range from 0 to 255, giving a range of possible inputs from 0 to 65025. Given that range, you need to decide whether to increment brightness from 0 to 255, for each value of hue, or whether to increment hue from 0 to 255 for each value of brightness.

The output of the hueToRGB function is the values in R, G, and B. These are the values to use in the analogWrite() function calls.

I'd start with ignoring the RTC for now. Use millis() to get the time that the Arduino has been running. Each time millis() has incremented by some amount (now - then > interval), increment hue or brightness, call hueToRGB(), and analogWrite() the results to the LED pins. Use a small interval so that you can see changes fairly quickly. When hue or brightness gets above 255, reset it to 0 and increment the other.

I used this sketch before, and it worked, setting the delay to increment the hue by 1 value, in the range of 255 over 24 hours, every 360000ms. The problem is, it resets to whatever the int colour value is set to when the arduino is turned off and on.

The aim of the project is to have the LED's land on a specific colour on a specific hour, and cycle through the hues in-between. So say at 9.00am the colour is yellow, becoming more orange by about 11.00am.

need to work out how to integrate the rtc data, in hours minutes and seconds to call on specific hues at specific times.

Forgot to add the code i used before.

const int redPin   = 5;   
const int greenPin = 6;
const int bluePin  = 3;
const boolean invert = true; 

int color = 100; 
int R, G, B;       

void setup()
{Serial.begin(9600);
}

void loop()
{
   int brightness = 255;           
  hueToRGB( color, brightness);
  
  analogWrite(redPin, R);
  analogWrite(greenPin, G);
  analogWrite(bluePin, B );

  color--;               
  Serial.println(color);
  delay (5000);
  if(color < 1)   
     color = 255;
       delay(10);
}


void hueToRGB( int hue, int brightness)
{
    unsigned int scaledHue = (hue * 6);
    unsigned int segment = scaledHue / 256; 
                                            
    unsigned int segmentOffset = 
      scaledHue - (segment * 256); 

    unsigned int complement = 0;
    unsigned int prev = (brightness * ( 255 -  segmentOffset)) / 256;
    unsigned int next = (brightness *  segmentOffset) / 256;

  if(invert)
    {
      brightness = 255-brightness;
      complement = 255;
      prev = 255-prev;
      next = 255-next;
    }

    switch(segment ) {
    case 0:      // red
     R = brightness;
         G = next;
     B = complement;
    break;
   
    case 1:     // yellow
    R = prev;
    G = brightness;
    B = complement;
    break;
    case 2:     // green
    R = complement;
    G = brightness;
    B = next;
    break;
   
    case 3:    // cyan
    R = complement;
    G = prev;
    B = brightness;
    break;
    
    case 4:    // blue
    R = next;
    G = complement;
    B = brightness;
    break;
   
    case 5:      // magenta
    default:
    R = brightness;
    G = complement;
    B = prev;
    break;
  }
}

I am running a similar project and would be interested to know if the OP has overcome his RCT to LED PWM code?

Or anyone else for that matter?