I’m trying to figure out how to do a simple toggle of an led. Here’s what I got so far:
// We name pin D0 as led
int led = D0;
// This routine runs only once upon reset
void setup()
{
// Initialize D0 pin as an output
pinMode(led, OUTPUT);
}
// This routine loops forever
void loop()
{
digitalWrite(led, !digitalRead(led)); // Toggle led
delay(5000);
}
This doesn’t seem to work. The led never turns on. What is the issue here?
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();
}
char state[] = "I state that I will flip the switch every second";
void setup() {
Serial.begin(9600);
Serial.println(state);
Serial.println("Ready??");
Serial.println("ok, flip the switch to turn ON the LED and wait about one second");
Serial.println("Then, flip the switch to turn OFF the LED, wait another second and start over");
}
void loop() {
// Look no loop needed!!
}
I’m loving these answers! I’m wondering if someone can tell me why my first example didn’t seem to work? Can you not do a digitalRead on a pin that’s been set as an OUTPUT? I heard this was an issue with the Arduino Due but previous versions allowed you to read an OUTPUT pin. Is that the case here?
Sure thing! Essentially reading / writing to a pin changes its mode (please correct me someone if I’m wrong here), so you can’t simultaneously read the value and write a value to the same pin at the same time. You could however write one pin high, and read from another pin, and connect the two together directly. I seem to recall someone posting a low-level explanation for this, but I’m having trouble tracking that post down (if someone else has it handy that’s cool) :).
There are at least three answers to this, depending on your use case.
If you just want to know what the state of a LED directly driven by the Core should be, you just write sonething like
#define LEDPIN D5
int ledState;
void setup()
{
pinMode(LEDPIN, OUTPUT);
}
void loop()
{
ledState = digitalRead(LEDPIN);
}
void changeState()
{
// do something to LEDPIN
}
On the other hand, if you actually want to measure what the electrical state of a LED is, that is not directly or solely driven be a dedicated LEDPIN, then you need to setup a dedicated pin in INPUT mode and perform a digitalRead() on that pin measuring the state via a suitable circuitry.
And thirdly if you have a PWM driven LED and you want to know its "illumination level" things get even more complicated
You just need to write some code and flash it from Spark Build or Spark Dev which currently will build with firmware that allows you to perform a digitalRead() on an OUTPUT. Like this LED toggler code: