Why doesn't arduino nano together with DS3231 make LED strip work as a clock? (I use FastLED library)

130 Views Asked by At

For some reason, my program doesn't make the LED clock work properly. I know that the problem is either in the breadboard or the program itself, since with the other program and different circuit everything works well. Everything is fine with the power supply and the connections between the peices of the LED strip are done correctly...

The program doesn’t light up the LEDs hence making it work as a clock; even though a separate program can light up the LEDs themselves, and the other program makes the clock print into the serial monitor the time

THINGS I HAVE ALREADY DONE: I have already tried using serial prints, Switching up the LEDs separately using another code (everything worked fine), Setting up the DS3231 separately and using serial prints to make sure it is working (everything worked fine), Making sure that the code does not take too much memory, I have additionally tried different processors to run the code (there are no mistakes in the code itself and avrdude is not displayed)

Can you, please, help me?

These are the connections on the breadboard: Connections on the breadboard

This is the clock itself:

connection of the parts of the LED strip

You can see the code here:

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

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


#define NUM_LEDS 172 // 6*7*4 +2+2  Number of LED controlled
#define COLOR_ORDER GRB  // Define color order for your strip
#define DATA_PIN 12 // Data pin for led comunication D9

CRGB leds[NUM_LEDS]; // Define LEDs strip

const byte digits[10][42] PROGMEM = 
                      {{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},   // Digit 0
                       {0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1},   // Digit 1
                       {1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},   // Digit 2
                       {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},   // Digit 3
                       {0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0,1,1,1,1,1,1},   // Digit 4
                       {0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},   // Digit 5
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0},   // Digit 6
                       {0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1},   // Digit 7
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},   // Digit 8
                       {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0}};  // Digit 9
                       
int last_digit = 0;
int ledColor = CRGB::White; // Color used (in hex)

void setup(){ 
  //Make the clock work and set its initial parameters
  Serial.begin(9600); 
  rtc.begin();
  //rtc.setTime(19, 48, 50);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(3, 6, 2017);   // Set the date to the current date

  Wire.begin();
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
  LEDS.setBrightness(200); // Set initial brightness
} 

int GetTime(){
  tmElements_t Now;
  RTC.read(Now);
  //time_t Now = RTC.Now();// Getting the current Time and storing it into a DateTime object 
  int hour=Now.Hour;
  //Serial.println(hour); //ia going to show the hour set to the clock by the other
  //Serial.println("--");
  int minute=Now.Minute;
  //Serial.println(minute); //ia going to show the hour set to the clock by the other
  //Serial.println("--");

  return (hour*100+minute);
  };

// Convert time to array needed for display 
void TimeToArray(){
  int Now = GetTime();  // Get time
  //Serial.print("Time is: ");
  //Serial.println(Now);
  //delay(1000);
  
  int cursor = 271; // last led number
  
  //dots of the clock
  leds[83]=ledColor;
  leds[84]=ledColor;
  leds[85]=ledColor;
  leds[86]=ledColor;
    
  for(int i=1;i<=4;i++){
    int digit = Now % 10; // get the last digit of the time starting from the last digit of the minutes

    if (i==1){
    //  Serial.print("the last digit of the minures is : ");Serial.print(digit);Serial.print(" ");

      cursor = 129;
      
      for(int k=0; k<=41;k++){ 
        // Serial.print(digits[digit][k]);
        if (digits[digit][k]== 1){leds[cursor]=ledColor;}
         else if (digits[digit][k]==0){leds[cursor]=0x000000;};
         cursor ++;
        };
      // Serial.println();
      }

    else if (i==2){
      // Serial.print("The first digit of the minutes is : ");Serial.print(digit);Serial.print(" ");

      cursor =87;
      
      for(int k=0; k<=41;k++){ 
        // Serial.print(digits[digit][k]);
        if (digits[digit][k]== 1){leds[cursor]=ledColor;}
         else if (digits[digit][k]==0){leds[cursor]=0x000000;};
         cursor ++;
        };
      // Serial.println();
      }

     else if (i==3){
      // Serial.print("The last digit of the hours is : ");Serial.print(digit);Serial.print(" ");
      cursor =41;
      for(int k=0; k<=41;k++){ 
        // Serial.print(digits[digit][k]);
        if (digits[digit][k]== 1){leds[cursor]=ledColor;}
         else if (digits[digit][k]==0){leds[cursor]=0x000000;};
         cursor ++;
        };
      // Serial.println();
      }
      
    else if (i==4){
      // Serial.print("The first digit of the hours is : ");Serial.print(digit);Serial.print(" ");
      cursor =0;
      if(digit !=0){
         for(int k=0; k<=41;k++){ 
        // Serial.print(digits[digit][k]);
        if (digits[digit][k]== 1){leds[cursor]=ledColor;}
         else if (digits[digit][k]==0){leds[cursor]=0x000000;};
         cursor ++;
        };
    }
    }
     Now /= 10;
  }; 
};
  
void loop()  // Main loop
{
  GetTime(); // get the time 
  TimeToArray(); // Get leds array with required configuration
  FastLED.show(); // Display leds array
  delay(1000);
}

Thank you very much in advance!!

1

There are 1 best solutions below

3
Michaël Roy On

You defined the LED digit patterns in PROGMEM, but did no call pgm_read_byte_near() to access them. As a result, you were loading digit patterns from SRAM, which is zeroed at boot time.

I had to move things around a bit to make a bit more sense of what exactly was going on.

See comment below:

#include <DS3231.h>

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

#define NUM_LEDS 172 // 6*7*4 +2+2  Number of LED controlled
#define COLOR_ORDER GRB  // Define color order for your strip
#define DATA_PIN 12 // Data pin for led comunication D9

DS3231 rtc(SDA, SCL);
CRGB leds[NUM_LEDS]; // Define LEDs strip
const byte digits[10][42] PROGMEM =
{ {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // Digit 0
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, // Digit 1
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // Digit 2
  {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // Digit 3
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1}, // Digit 4
  {0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, // Digit 5
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}, // Digit 6
  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // Digit 7
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, // Digit 8
  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0}
};  // Digit 9

constexpr int ledColor = CRGB::White; // Color used (in hex)
static void ShowTime() {

  //dots of the clock
  leds[83] = ledColor;
  leds[84] = ledColor;
  leds[85] = ledColor;
  leds[86] = ledColor;

  tmElements_t Now;
  RTC.read(Now);

  for (int i = 1; i <= 4; ++i) {
    int digit = 0;
    int cursor = 0;

    switch (i)
    {
      case 1:
        cursor = 0;
        digit = Now.Hour / 10;
        break;
      case 2:
        cursor = 41;
        digit = Now.Hour % 10;
        break;
      case 3:
        cursor = 87;
        digit = Now.Minute / 10;
        break;
      case 4:
        cursor = 129;
        digit = Now.Minute % 10;
        break;
    }

    // added pgm_read_byte_near() to access PROGMEM data
    for (int k = 0; k < 42; ++k, ++cursor)
      leds[cursor] = pgm_read_byte_near(&digits[digit][k]) ? ledColor : 0;
  }
  FastLED.show(); // Display leds array
}

void setup() {
  //Make the clock work and set its initial parameters
  Serial.begin(9600);
  rtc.begin();
  //rtc.setTime(19, 48, 50);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(3, 6, 2017);   // Set the date to the current date

  Wire.begin();
  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
  LEDS.setBrightness(200); // Set initial brightness
}

void loop()  // Main loop
{
  ShowTime();
  delay(1000);
}