Issue with DS3231 RTC on arduino nano

Hi all, for my school I'm trying to make a project find on a forum but when I verify the code I receive an error. Follow the code (thanx to hans andersson):

#include <FastLED.h>

DS3231 rtc(SDA, SCL);

void setup()
{
  Serial.begin(9600);
  rtc.begin();
  rtc.setTime(12,39,0);     // hour, minute, second
}

void loop()
{
  Serial.println(rtc.getTimeStr());
  delay (1000);
}

#define PHOTO_RESISTOR_PIN A3
#define LED_PIN     4
#define NUM_LEDS    60
#define LED_TYPE    WS2812
#define COLOR_ORDER GRB
#define FRAMES_PER_SECOND 50
#define MAX_TINT 80
#define MAX_HUE_OFFSET 25
#define MIN_BRIGHTNESS 7
#define MAX_BRIGHTNESS 255
#define MIN_LIGHT 5
#define MAX_LIGHT 300
#define LIGHT_SENSITIVITY 10  //minimum light for light sensor to work as a button
#define NUM_MODES  6
#define MODE_SIMPLE  0
#define MODE_MINIMALISTIC 1
#define MODE_PROGRESS  2
#define MODE_SWEEP  3
#define MODE_SPECTRUM 4
#define MODE_HYSTERICAL 5

CRGB leds[NUM_LEDS];
DS3231 rtc(SDA, SCL);

void setup() {
  delay( 3000 ); // power-up safety delay
  FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.setMaxPowerInVoltsAndMilliamps( 5, 1500);
  rtc.begin();
}


void loop()
{
  static int mode = MODE_SIMPLE;
  static unsigned long old_s=0;
  static int old_light = 0;
  static unsigned long last_dimmed = 0;
  static int tint_a = 0;
  static int tint_b = 0;
  static int tintSteps=3;
  static int hueOffset = 0;
  static int hueSteps=1;

  Time t = rtc.getTime();
  unsigned long h=t.hour;
  unsigned long m=t.min;
  unsigned long s=t.sec;
  if(s!=old_s)
  {
    old_s=s;
    int light=analogRead(PHOTO_RESISTOR_PIN);
    if(light < 3 && old_light > LIGHT_SENSITIVITY)
      last_dimmed = millis();
    else if(light > LIGHT_SENSITIVITY && millis() - last_dimmed < 3000)
    {
       mode=(mode+1)%NUM_MODES;
       last_dimmed=0;
    }
    old_light=light;
    light = constrain(light,MIN_LIGHT,MAX_LIGHT);
    int b1=map(light,MIN_LIGHT,MAX_LIGHT,MIN_BRIGHTNESS,MAX_BRIGHTNESS);
    int b2=FastLED.getBrightness();
    if(b1 != b2 && (b1==MIN_BRIGHTNESS || b1==MAX_BRIGHTNESS || abs(b1-b2)*100/b2 > 10))
      FastLED.setBrightness(b1);

    int h_led=h%12*5+m/12;
    int m_led=m;
    int s_led=s;

    hueOffset+=hueSteps;
    if(abs(hueOffset) == MAX_HUE_OFFSET)
      hueSteps*=-1;

    if(tint_a==0)
    {
      tint_b+=tintSteps;
      tint_b=constrain(tint_b,0,MAX_TINT);
      if(tint_b==0 || tint_b==MAX_TINT)
        tintSteps*=-1;
      if(tint_b==0)
        tint_a=1;
    }
    else
    {        
      tint_a+=tintSteps;
      tint_a=constrain(tint_a,0,MAX_TINT);
      if(tint_a==0 || tint_a==MAX_TINT)
        tintSteps*=-1;
    }

    if (mode == MODE_SIMPLE)
      modeSimple(h_led,m_led,s_led,tint_a,tint_b);
    else if (mode == MODE_MINIMALISTIC)
      modeMinimalistic(h_led,m_led,tint_a,tint_b);
    else if (mode == MODE_PROGRESS)
      modeProgress(h_led,m_led,s_led,tint_a,tint_b);
    else if (mode == MODE_SWEEP)
      modeSweep(h_led,m_led,s_led,hueOffset);
    else if (mode == MODE_SPECTRUM)
      modeSpectrum(h_led,m_led,s_led,tint_a,tint_b);
    else if (mode == MODE_HYSTERICAL)
      modeHysterical(h_led,m_led,s_led,hueOffset);
    }
    FastLED.delay(1000/FRAMES_PER_SECOND); 
}


void modeSimple(int h, int m, int s, int tint_a, int tint_b)
{
  FastLED.clear();
  leds[h]+=CRGB(255,tint_a,tint_b);
  leds[m]+=CRGB(tint_b,255,tint_a);
  leds[s]+=CRGB(tint_a,tint_b,255);
  FastLED.show();
}


void modeMinimalistic(int h, int m, int tint_a, int tint_b)
{
  FastLED.clear();
  leds[h]+=CRGB(255,tint_a,tint_b);
  leds[m]+=CRGB(tint_b,255,tint_a);
  FastLED.show();
}


void modeProgress(int h, int m, int s, int tint_a, int tint_b)
{
  FastLED.clear();
  for(int i=h;i>=0 && (i==h || (i!=m && i!=s));i--)
    leds[i]=CRGB(255,tint_a,tint_b);
  for(int i=m;i>=0 && (i==m || (i!=h && i!=s));i--)
    leds[i]=CRGB(tint_b,255,tint_a);
  for(int i=s;i>=0 && (i==s || (i!=h && i!=m));i--)
    leds[i]=CRGB(tint_a,tint_b,255);
  FastLED.show();
}


void modeSweep(int h, int m, int s, int hueOffset)
{
  FastLED.clear();
  for(int i=1;i<15 &&(h-i+60)%60!=m&&(h-i+60)%60!=s;i++)
    leds[(h-i+60)%60]=CHSV((HUE_RED+hueOffset)%255,255,255-15*i);

  for(int i=1;i<15&&(m-i+60)%60!=h&&(m-i+60)%60!=s;i++)
    leds[(m-i+60)%60]=CHSV(HUE_GREEN+hueOffset,255,255-15*i);

  for(int i=1;i<15 &&(s-i+60)%60!=h&&(s-i+60)%60!=m;i++)
    leds[(s-i+60)%60]=CHSV(HUE_BLUE+hueOffset,255,255-15*i);

  leds[h]=CHSV((HUE_RED+hueOffset)%255,255,255);
  leds[m]=CHSV(HUE_GREEN+hueOffset,255,255);
  leds[s]=CHSV(HUE_BLUE+hueOffset,255,255);
  FastLED.show();
}


void modeSpectrum(int h, int m, int s, int tint_a, int tint_b)
{
  FastLED.clear();
  int count=(h-m+60)%60;
  CRGB rgb_h=CRGB(255,tint_a,tint_b);
  CRGB rgb_m=CRGB(tint_b,255,tint_a);
  CRGB rgb_s=CRGB(tint_a,tint_b,255);
  int s_count=(h-s+60)%60;
  if(s_count < count)
  {
    for(int i=0;i<=s_count;i++)
      leds[(h-i+60)%60]= blend(rgb_h,rgb_m, 255*i/count);
    for(int i=s_count;i<=count;i++)
      leds[(h-i+60)%60]= blend(rgb_s,rgb_m, 255*(i-s_count)/(count-s_count));
  }
  else          
  {
    for(int i=0;i<=count;i++)
      leds[(h-i+60)%60]= blend(rgb_h,rgb_m, 255*i/count);
    leds[s]=rgb_s;
  }
  FastLED.show();
}


void modeHysterical(int h, int m, int s, int hueOffset)
{
  FastLED.clear();
  for(int i=4;i<60;i++)
  {
    for(int j=i-4;j<=i;j++)
      leds[(s+j)%60]= CHSV(HUE_BLUE+hueOffset,255,255);
    leds[h]=CHSV((HUE_RED+hueOffset)%255,255,255);
    leds[m]=CHSV(HUE_GREEN+hueOffset,255,255);
    for(int j=0;j<60;j++)
      if(random(10)==1 && j!=h && j!=m )
        leds[j]=leds[j].fadeToBlackBy(100);
    FastLED.show();
    delay(1);        
  }
  for(int i=0;i<30;i++)
  {
    leds[h]=CHSV((HUE_RED+hueOffset)%255,255,255);
    leds[m]=CHSV(HUE_GREEN+hueOffset,255,255);
    leds[s]=CHSV(HUE_BLUE+hueOffset,255,255);
    for(int j=0;j<60;j++)
      if(j!=h && j!=m && j!=s)
        leds[j]=leds[j].fadeToBlackBy(40);
    FastLED.show();
    delay(3);        
  }
  for(int i=0;i<60;i++)
    if(i!=h && i!=m && i!=s)
      leds[i]=0;
  FastLED.show();
}```

##################################################

```#include <DS3231.h>
DS3231  rtc(SDA, SCL);

void setup()
{
  Serial.begin(9600);
  rtc.begin();
  rtc.setTime(8,15,0);     // hour, minute, second
}

void loop()
{
  Serial.println(rtc.getTimeStr());
  delay (1000);
}

Could someone help me please?
thank you and sorry if I've make a mistake to post in this way

Mary

you appear to be using the DS3231 without including its header file, e.g.

#include <DS3231.h>

you will probably have to use the library manager to load the DS3231 library

In the future, please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

Are you trying to combine two sketches into one? That's what it looks like you are trying to do.

Yes with multiple setup() and loop() functions…

To combine two sketches you are almost certainly going to need to know how each one performs its function.

Google

Combine two Arduino programs

or sketches or whatever.

It's a common problem and has been addressed variously, it all comes back to needing to know a bit more than you might prefer to.

HTH

a7

excuse me. Here the errors

Arduino:1.8.13 (Windows 10), Scheda:"Arduino Nano, ATmega328P"





















O-Clock:59:12: error: redefinition of 'DS3231 rtc'

 DS3231 rtc(SDA, SCL);

            ^~~

C:\Users\mary\Desktop\ide\O-Clock\O-Clock.ino:22:8: note: 'DS3231 rtc' previously declared here

 DS3231 rtc(SDA, SCL);

        ^~~

C:\Users\mary\Desktop\ide\O-Clock\O-Clock.ino: In function 'void setup()':

O-Clock:61:6: error: redefinition of 'void setup()'

 void setup() {

      ^~~~~

C:\Users\mary\Desktop\ide\O-Clock\O-Clock.ino:24:6: note: 'void setup()' previously defined here

 void setup()

      ^~~~~

C:\Users\mary\Desktop\ide\O-Clock\O-Clock.ino: In function 'void loop()':

O-Clock:69:6: error: redefinition of 'void loop()'

 void loop()

      ^~~~

C:\Users\mary\Desktop\ide\O-Clock\O-Clock.ino:31:6: note: 'void loop()' previously defined here

 void loop()

      ^~~~

C:\Users\mary\Desktop\ide\O-Clock\SetTime.ino: At global scope:

SetTime:4:13: error: redefinition of 'DS3231 rtc'

 DS3231  rtc(SDA, SCL);

             ^~~

C:\Users\mary\Desktop\ide\O-Clock\O-Clock.ino:22:8: note: 'DS3231 rtc' previously declared here

 DS3231 rtc(SDA, SCL);

        ^~~

C:\Users\mary\Desktop\ide\O-Clock\SetTime.ino: In function 'void setup()':

SetTime:6:6: error: redefinition of 'void setup()'

 void setup()

      ^~~~~

C:\Users\mary\Desktop\ide\O-Clock\O-Clock.ino:24:6: note: 'void setup()' previously defined here

 void setup()

      ^~~~~

C:\Users\mary\Desktop\ide\O-Clock\SetTime.ino: In function 'void loop()':

SetTime:13:6: error: redefinition of 'void loop()'

 void loop()

      ^~~~

C:\Users\mary\Desktop\ide\O-Clock\O-Clock.ino:31:6: note: 'void loop()' previously defined here

 void loop()

      ^~~~

exit status 1

redefinition of 'DS3231 rtc'

What Arduino board are you using?

@alto777 has very good advice.

Learn what makes up a valid Arduino sketch.

Learn how to combine 2 sketches.

Those errors are because you have more than one void setup(), void loop() and DS3231 rtc(SDA, SCL);

I don't see where you include the DS3231 RTC library.

arduino nano

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.