如何在Arduino中将目标为控制台的数据传递给char数组

如何在Arduino中将目标为控制台的数据传递给char数组,arduino,Arduino,在Arduino上使用RTC的草图中,我有以下几段代码: // print time to Serial void printTime(time_t t){ printI00(hour(t), ':'); printI00(minute(t), ':'); printI00(second(t), ' '); } // print date to Serial void printDate(time_t t){ printI00(day(t), 0); Serial <

在Arduino上使用RTC的草图中,我有以下几段代码:

// print time to Serial
void printTime(time_t t){
  printI00(hour(t), ':');
  printI00(minute(t), ':');
  printI00(second(t), ' ');
}

// print date to Serial
void printDate(time_t t){
  printI00(day(t), 0);
  Serial << monthShortStr(month(t)) << _DEC(year(t));
}

如果您理解,这个示例应该做您需要的基本事情,从sprintf到strcpy、strcat和iota方法的更改将提高内存效率:

// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;
unsigned long currentTime = 0;
unsigned long intervalTime = 1000;   // one print every second

char timeStamp[12] = {'\0'};
char dateStamp[11] = {'\0'};

char meridian [3] = {'\0'};

void setup () {
  Serial.begin(115200);
  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

void loop () {
  if (millis() - currentTime > intervalTime) {
    DateTime now = rtc.now();
    byte twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
    byte zeroHour = 12;                // Variable use to convert "0" zero hour to display it as 12:00 +
    byte displayHour;
    byte MIN = now.minute();
    byte SEC = now.second();

    if (now.hour() == 0)   { // First we test if the hour reads "0"
      displayHour = zeroHour;
      strcpy (meridian, "AM");
    }
    else if (now.hour() >= 13)    {    // if no, Second we test if the hour reads "13 or more"
      displayHour = twelveHour;
      strcpy (meridian, "PM");
    }
    else {
      displayHour = now.hour();
      strcpy (meridian, "AM");
    }
    sprintf(timeStamp, "%02d:%02d:%02d-%s", displayHour, MIN, SEC, meridian);
    sprintf(dateStamp, "%02d/%02d/%04d", now.month(), now.day(), now.year());

    Serial.println(timeStamp);
    Serial.println(dateStamp);
  }
}
//使用通过I2C和Wire lib连接的DS1307 RTC执行日期和时间功能
#包括
#包括“RTClib.h”
RTC_DS1307 RTC;
无符号长currentTime=0;
无符号长间隔时间=1000;//每秒打印一次
字符时间戳[12]={'\0'};
字符日期戳[11]={'\0'};
字符子午线[3]={'\0'};
无效设置(){
序列号开始(115200);
rtc.begin();
rtc.调整(日期时间(F(uu日期),F(u时间));
}
空循环(){
如果(毫秒()-currentTime>intervalTime){
DateTime now=rtc.now();
byte twelveHour=now.hour()-12;//用于以12小时格式显示13个以上小时的变量
字节zeroHour=12;//变量用于转换“0”零小时以将其显示为12:00+
字节显示小时;
字节MIN=now.minute();
字节秒=now.second();
如果(now.hour()==0){//首先我们测试小时是否为“0”
显示小时=零小时;
strcpy(子午线,“AM”);
}
else如果(now.hour()>=13){//如果否,我们首先测试该小时的读数是否为“13或更多”
显示小时=十二小时;
strcpy(子午线,PM);
}
否则{
displayHour=now.hour();
strcpy(子午线,“AM”);
}
sprintf(时间戳,“%02d:%02d:%02d-%s”,显示小时、分钟、秒、子午线);
sprintf(日期戳,“%02d/%02d/%04d”、now.month()、now.day()、now.year());
Serial.println(时间戳);
Serial.println(邮戳);
}
}
char bufferMSGtoClient[100];
// Date and time functions using a DS1307 RTC connected via I2C and Wire lib
#include <Wire.h>
#include "RTClib.h"

RTC_DS1307 rtc;
unsigned long currentTime = 0;
unsigned long intervalTime = 1000;   // one print every second

char timeStamp[12] = {'\0'};
char dateStamp[11] = {'\0'};

char meridian [3] = {'\0'};

void setup () {
  Serial.begin(115200);
  rtc.begin();
  rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
}

void loop () {
  if (millis() - currentTime > intervalTime) {
    DateTime now = rtc.now();
    byte twelveHour = now.hour() - 12; // Variable used to display 13+ hours in 12 hour format
    byte zeroHour = 12;                // Variable use to convert "0" zero hour to display it as 12:00 +
    byte displayHour;
    byte MIN = now.minute();
    byte SEC = now.second();

    if (now.hour() == 0)   { // First we test if the hour reads "0"
      displayHour = zeroHour;
      strcpy (meridian, "AM");
    }
    else if (now.hour() >= 13)    {    // if no, Second we test if the hour reads "13 or more"
      displayHour = twelveHour;
      strcpy (meridian, "PM");
    }
    else {
      displayHour = now.hour();
      strcpy (meridian, "AM");
    }
    sprintf(timeStamp, "%02d:%02d:%02d-%s", displayHour, MIN, SEC, meridian);
    sprintf(dateStamp, "%02d/%02d/%04d", now.month(), now.day(), now.year());

    Serial.println(timeStamp);
    Serial.println(dateStamp);
  }
}