[Beginner] How to blink a D0 LED

I have never used wiring and I am used to python 3 but that is for another time. I found some code on the internet that makes two leds blink. I only want one to blink and when I tried to mod the code I got a bunch of error because I am complete n00b at programming in wiring. Can some one please mod the code so only the led on DO blinks. On a side note how fast can I make the led blink? Here is the code

We've heavily commented this code for you. If you're a pro, feel free to ignore it.

Comments start with two slashes or are blocked off by a slash and a star.
You can read them, but your device can't.
It's like a secret message just for you.

Every program based on Wiring (programming language used by Arduino, and Particle devices) has two essential parts:

setup - runs once at the beginning of your program
loop - runs continuously over and over

You'll see how we use these in a second. 

This program will blink an led on and off every second.
It blinks the D7 LED on your Particle device. If you have an LED wired to D0, it will blink that LED as well.
// First, we're going to make some variables.
// This is our "shorthand" that we'll use throughout the program:

int led1 = D0; // Instead of writing D0 over and over again, we'll write led1
// You'll need to wire an LED to this one to see it blink.

int led2 = D7; // Instead of writing D7 over and over again, we'll write led2
// This one is the little blue LED on your board. On the Photon it is next to D7, and on the Core it is next to the USB jack.

// Having declared these variables, let's move on to the setup function.
// The setup function is a standard part of any microcontroller program.
// It runs only once when the device boots up or is reset.

void setup() {

  // We are going to tell our device that D0 and D7 (which we named led1 and led2 respectively) are going to be output
  // (That means that we will be sending voltage to them, rather than monitoring voltage that comes from them)

  // It's important you do this here, inside the setup() function rather than outside it or in the loop function.

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

}

// Next we have the loop function, the other essential part of a microcontroller program.
// This routine gets repeated over and over, as quickly as possible and as many times as possible, after the setup function is called.
// Note: Code that blocks for too long (like more than 5 seconds), can make weird things happen (like dropping the network connection).  The built-in delay function shown below safely interleaves required background activity, so arbitrarily long delays can safely be done if you need them.


void loop() {
  // To blink the LED, first we'll turn it on...
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);

  // We'll leave it on for 1 second...
  delay(13);

  // Then we'll turn it off...
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);

  // Wait 1 second...
  delay(13);

  // And repeat!
}

Please use this format to insert code in discourse

``` <-- insert this

//paste code here

``` <-- insert this
void setup(){
  pinMode(D0, OUTPUT);
}

void loop(){
  digitalWrite(D0, !digitalRead(D0));
  delay(1000);
}

You can blink it as fast as you want (speed of code execution) it to be, beyond what the eyes can observe :wink:

Again I am a complete n00b to wiring so should that code go at the bottom?

Replace the entire code in the Web IDE.

Meaning, delete everything and paste the code above.

I just got this error when I tried to run that code.

/spark/compile_service/shared/workspace/6_hal_12_0/firmware-privablinking_a_led.cpp: In function 'void loop()':
/spark/compile_service/shared/workspace/6_hal_12_0/firmware-privablinking_a_led.cpp:6:36: error: expected ')' before ';' token
void setup(){
^
make[1]: *** [../build/target/user/platform-6blinking_a_led.o] Error 1
make: *** [user] Error 2

Flash unsuccessful.

opps sorry. There’s a ) missing:

void setup(){
  pinMode(D0, OUTPUT);
}

void loop(){
  digitalWrite(D0, !digitalRead(D0));
  delay(1000);
}
1 Like

What exactly does that code do?

It blinks D0 every 1 second.

So it turns the led on for 1 second and then it turns the off for one second and then it repeats? Is that right?

Yup.

The longer and beginner readable code is:

void setup(){
  pinMode(D0, OUTPUT);
}

void loop(){
  digitalWrite(D0, HIGH);
  delay(1000);
  digitalWrite(D0, LOW);
  delay(1000);
}
1 Like

Thanks for your help!