プログラムを貼ってみる実験したけど、もうどうにもならない。どうすればいいの???(失敗

#include <Time.h>
#include <TimeLib.h>

 

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

// bit set / clear
#ifndef cbi
#define cbi(PORT, BIT) (_SFR_BYTE(PORT) &= ~_BV(BIT))
#endif
#ifndef sbi
#define sbi(PORT, BIT) (_SFR_BYTE(PORT) |= _BV(BIT))
#endif


// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

unsigned int localPort = 8888; // local port to listen for UDP packets

char timeServer = "time.nist.gov"; // time.nist.gov NTP server

const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message

byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

// A UDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

byte timecode[60];
unsigned long lastNTPTime = 0;

void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// start Ethernet and UDP
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
Udp.begin(localPort);
NTPSetTime();
setupTimeCode();
}


void loop()
{
int wait_start = second();
while (wait_start == second()); // wait until time is corrected
unsigned long startTime = millis();

// generate 40khz from 3 pin using PWM
pinMode(3, OUTPUT);
digitalWrite(3, LOW);
TCCR2A = _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS20);
OCR2A = F_CPU / 2 / 40000/*hz*/;
OCR2B = OCR2A / 2; /* 50% duty */
sbi(TCCR2A,COM2B1);

// calc signal duration (ms)
int ms = calcTimeCodeDuration();

// wait ms and stop PWM
while (millis() - startTime < ms);
cbi(TCCR2A,COM2B1);

if (millis() - lastNTPTime > 60*60*1000L) {
NTPSetTime();
setupTimeCode();
lastNTPTime = millis();
}
}

// send an NTP request to the time server at the given address
void sendNTPpacket(char* address) {
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
packetBuffer[ 0] = 0b11100011; // LI, Version, Mode
packetBuffer[ 1] = 0; // Stratum, or type of clock
packetBuffer[ 2] = 6; // Polling Interval
packetBuffer[ 3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;

// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
Udp.write(packetBuffer, NTP_PACKET_SIZE);
Udp.endPacket();
}

//=========================== NTP ===========================
void NTPSetTime()
{
int packetSize;
do{
sendNTPpacket(timeServer);
Serial.println("Waiting NTP response ...");
delay(1000*5);
packetSize = Udp.parsePacket();
Serial.print("Received packet of size ");
Serial.println(packetSize);
}
while(!packetSize);
Udp.available();
// バッファに受信データを読み込む
Udp.read(packetBuffer, NTP_PACKET_SIZE);

// 時刻情報はパケットの40バイト目からはじまる4バイトのデータ
unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);

// NTPタイムスタンプは64ビットの符号無し固定小数点数(整数部32ビット、小数部32ビット)
// 1900年1月1日0時との相対的な差を秒単位で表している
// 小数部は切り捨てて、秒を求めている
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = " );
Serial.println(secsSince1900);

// NTPタイムスタンプをUNIXタイムに変換する
// UNITタイムは1970年1月1日0時からはじまる
// 1900年から1970年の70年を秒で表すと2208988800秒になる
const unsigned long seventyYears = 2208988800UL;
// NTPタイムスタンプから70年分の秒を引くとUNIXタイムが得られる
unsigned long epoch = secsSince1900 - seventyYears;
Serial.print("Unix time = ");
Serial.println(epoch);

// Timeライブラリに時間を設定(UNIXタイム)
// 日本標準時にあわせるために+9時間しておく
setTime(epoch + (9 * 60 * 60));

Serial.print("JST is ");
Serial.print(year());
Serial.print('/');
Serial.print(month());
Serial.print('/');
Serial.print(day());
Serial.print(' ');
Serial.print(hour());
Serial.print(':');
Serial.print(minute());
Serial.print(':');
Serial.println(second());
Serial.println();

Serial.print("localtime = ");
Serial.println(epoch);
}

//=========================== JJY ===========================

unsigned int calcTimeCodeDuration()
{
int s = second();
if (s == 0)
setupTimeCode();
return timecode[s] * 100;
}

void setupTimeCode()
{
int i;
memset(timecode, 8, sizeof(timecode));

setupTimeCode100(minute(), 0);
timecode[0] = 2;

setupTimeCode100(hour(), 10);

int d = dayOfYear();
setupTimeCode100(d/10, 20);
setupTimeCode100(d%10*10, 30);

int parity1 = 0, parity2 = 0;
for (i = 12; i < 20; i++) parity1 ^= timecode[i] == 5;
for (i = 1; i < 10; i++) parity2 ^= timecode[i] == 5;
timecode[36] = parity1 ? 5 : 8;
timecode[37] = parity2 ? 5 : 8;

setupTimeCode100(year()%100, 40);
for (i = 44; i > 40; i--)
timecode[i] = timecode[i-1];
timecode[40] = 8;

int w = weekday() - 1;
timecode[50] = (w & 4) ? 5 : 8;
timecode[51] = (w & 2) ? 5 : 8;
timecode[52] = (w & 1) ? 5 : 8;
timecode[59] = 2;

/* dump */
for (i = 0; i < 60; i++) {
Serial.print(timecode[i], DEC);
Serial.print(i % 10 == 9 ? "\r\n" : " ");
}
Serial.println();
}

void setupTimeCode100(int m, int i)
{
timecode[i+0] = *1};
time_t t = makeTime(tm);
return (now() - t) / SECS_PER_DAY + 1;
}

*1:m/10) & 8) ? 5 : 8;
timecode[i+1] = ((m/10) & 4) ? 5 : 8;
timecode[i+2] = ((m/10) & 2) ? 5 : 8;
timecode[i+3] = ((m/10) & 1) ? 5 : 8;
timecode[i+4] = 8;
timecode[i+5] = ((m%10) & 8) ? 5 : 8;
timecode[i+6] = ((m%10) & 4) ? 5 : 8;
timecode[i+7] = ((m%10) & 2) ? 5 : 8;
timecode[i+8] = ((m%10) & 1) ? 5 : 8;
timecode[i+9] = 2;
}

int dayOfYear()
{
tmElements_t tm = {0, 0, 0, 0, 1, 1, CalendarYrToTm(year(