簡體   English   中英

為什么 arduino nano 與 DS3231 一起使 LED 燈條不能作為時鍾工作? (我使用 FastLED 庫)

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

出於某種原因,我的程序無法使 LED 時鍾正常工作。 我知道問題出在面包板或程序本身,因為使用其他程序和不同的電路一切正常。 電源一切正常,LED 燈條各部分之間的連接正確無誤……

該程序不會點亮 LED,因此使其像時鍾一樣工作; 即使一個單獨的程序可以自己點亮 LED,而另一個程序使時鍾將時間打印到串行監視器中

我已經完成的事情:我已經嘗試過使用串行打印,使用另一個代碼分別打開 LED(一切正常),單獨設置 DS3231 並使用串行打印以確保它正常工作(一切正常),制作確保代碼不會占用太多 memory,我還嘗試了不同的處理器來運行代碼(代碼本身沒有錯誤,也沒有顯示 avrdude)

你能幫我么?

這些是面包板上的連接:面包板上的連接

這是時鍾本身:

LED燈條各部分的連接

你可以在這里看到代碼:

#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);
}

非常感謝您!

您在 PROGMEM 中定義了 LED 數字模式,但沒有調用 pgm_read_byte_near() 來訪問它們。 結果,您從 SRAM 加載數字模式,它在啟動時被清零。

我不得不稍微移動一下,以便更清楚地了解到底發生了什么。

請參閱下面的評論:

#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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM