How to toggle a digital pin

I was totally serious, here’s a version that syncs the clock with an NTP server, and then blinks on 5 second intervals (code from this thread! https://community.spark.io/t/getting-utc-time-from-ntp-server/1213/3 - Thanks! )

UDP UDP;

char string[ 17 ] = { "" };

int hour, minute, second;

unsigned int localPort = 123;
unsigned int timeZone = -6;

unsigned int serverNum = 0;
unsigned long timeout;

const char timeServer[] = "pool.ntp.org";
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
int synced = 0;
int state = 0;
unsigned int offset = 0;
unsigned long secsSince1900 = 0;



void setup()
{
	UDP.begin(localPort);
	Serial.begin(9600);
	Serial.println("NTP-clock");
	pinMode(D7, OUTPUT);
}

void loop()
{
    if (!synced) {
        Serial.println("Syncing...");
        syncTime();
        synced = 1;
    }
    receiveTimeTick();
    displayTime();
    
    if ((second % 5) == 0) {
        //toggle every 5 seconds, synced with NTP.
        toggle();
    }
    
    delay(1000);
}

void toggle() {
    digitalWrite(D7, (state) ? HIGH : LOW);
    state = !state;
}




void displayTime() {
    if (!synced) {
        return;
    }
    
    // now convert NTP time into everyday time:
    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
    const unsigned long seventyYears = 2208988800UL;    
    // subtract seventy years:
    unsigned long epoch = secsSince1900 - seventyYears;
    
    //add elapsed seconds.
    epoch += ((millis() - offset) / 1000.0);

    hour = (epoch % 86400L) / 3600;         
    minute = (epoch % 3600) / 60;
    second = (epoch % 60);

    Serial.print (hour);
    Serial.print (":");
    Serial.print (minute);
    Serial.print (":");
    Serial.println (second);
}

void syncTime() {
    sendNTPpacket(timeServer);  // send an NTP packet to a time server
	// wait to see if a reply is available
}

void receiveTimeTick() {
    if (synced) {
        return;
    }
    
    if ( UDP.parsePacket() ) {
    	UDP.read(packetBuffer, NTP_PACKET_SIZE);  // read the packet into the buffer

	    //the timestamp starts at byte 40 of the received packet and is four bytes,
	    // or two words, long. First, esxtract the two words:

	    unsigned long highWord = (packetBuffer[40] << 8) + packetBuffer[41];
	    unsigned long lowWord = (packetBuffer[42] << 8) + packetBuffer[43];
	    // combine the four bytes (two words) into a long integer
	    // this is NTP time (seconds since Jan 1 1900):
        secsSince1900 = highWord << 16 | lowWord;
	    secsSince1900 += timeZone*60*60;
	    
	    offset = millis();
	    synced = 1;
    }

	while ( UDP.parsePacket() ) {               // clean-up buffer
		UDP.read(packetBuffer, NTP_PACKET_SIZE);  // read the packet into the buffer
	}
}


// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(const 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);
	UDP.write(packetBuffer, NTP_PACKET_SIZE); //NTP requests are to port 123
	UDP.endPacket();
}
1 Like