Comment out Code – 3 Ways in Arduino IDE 1 and 2

Commenting out code. What does that mean exactly? Now, I’m not talking about ‘commenting code’. That’s a discussion in and of itself. I’m talking about commenting OUT code.

That’s when you selectively turn off some of the code in your program. In this lesson, we’re gonna cover three different ways to comment out code in your Arduino programs. They’re all super easy, and you’re gonna have them down pat, in no time.

By the end of this lesson, you’ll be equipped to comment out code in 3 different ways.

Commenting Out Code

So let’s say we have a line of code that we don’t want included in the sketch when it gets uploaded.

We simply want to temporarily turn off a line of code. What do we do? Well, I just put two forward slashes in front of the line.

// digitalWrite(7, HIGH);  -> This code has been commented out!

These two forward slashes are called a comment, and you’ll notice all of the text, all the code after that line get grayed out. It kind of becomes this grayer color. And what this means is that this line of code is not gonna be verified when we verify it, or it’s not gonna get included when we upload it either. So this is effectively a way to turn off a line of code.

Edit: Comment/uncomment

Now, another way to add the same forward slash is to first put your cursor where you want to comment out some code, go up to Edit, and then click Comment/Uncomment. You can also select an entire code block and go up to Edit, Comment/Uncomment.

commenting out code drop down in Arduino IDE

Programming Electronics Academy members, learn about the Arduino IDE toolchain in the Familiarization section of the Arduino Course for Absolute Beginners.

Not a member yet?  Sign up here.

Keyboard Shortcuts to Comment out Code

There is also a keyboard shortcut for doing this. After placing your cursor at the beginning of the desired line,  press Control + Forward Slash on a PC, and it’s Command + Forward Slash on a Mac.

To uncomment the selected lines, simply press it again and it will toggle back off.

multi-line commenting

Now, there is another way to comment out several lines of code, and that’s to use a multi-line comment.

/*
int ledPins[] = { 6, 7, 8, 9, 10, 11, 12, 13};
int pinCount = sizeof(ledPins) / sizeof(ledPins[0]);
*/

By using both a forward slash & asterisk between any amount of code, we can comment out anything in between. Important note: we cannot use the keyboard shortcut to comment out multi-line comments.

Using Preprocessor Directives to comment out code

Now, there’s also a third way to comment out multiple lines of code using a preprocessor directive. A preprocessor is something that runs on your program, on your sketch, before it actually gets compiled. And you can run some directives that allow some of the code to be included and some of it not to be included. The preprocessor directive we’ll be talking about is the “if statement”.

#if 1
  for (int thisPin = 0; thisPin < pinCount; thisPin++) {
    digitalWrite(ledPins[thisPin], HIGH);
    delay(timer);
    digitalWrite(ledPins[thisPin], LOW);
  }
#endif 

We’ll start our if statement with a ‘hashtag’ or ‘octothorp’.  And then we’ll add “if 0”. Then, where we want the comment to end, we’ll add another ‘octothorpe’ aka ‘pound sign’ aka ‘hashtag’, and write “endif”. So this is a preprocessor directive that is going to evaluate whatever value is read.

Programming Electronics Academy members, check out the Control Structures section of the Arduino Course for Absolute Beginners to learn and practice your programming control flow.

Not a member yet?  Sign up here.

So it’s like a condition. For instance, you may have some code that you’re constantly needing to turn off and on as you’re prototyping. This is a useful way to do it multi-line.

Variety is the Spice of Life

So there are several different ways to comment out code in the Arduino IDE. I hope you found this helpful and feel free to check out any of our other lessons and videos!

AppLab Bricks open in background with actual brick

Arduino AppLab Bricks → Marketing Garbage or New Powerful Interface?

Arduino Ventuno single board computer - top side

New Ventuno Q Dual Brain Single Board Computer

AppLab Pip Install

How to Add Python Packages in Arduino AppLab (No pip install needed)

Arduino Power Section Schematic

Kit-on-a-Shield Schematic Review

Just how random is the ESP32 random number generator?

Just how random is the ESP32 random number generator?

2 Comments

  1. Sistem Informasi on June 6, 2023 at 10:15 pm

    when you selectively turn off some of the code in your program.?

    • Michael James on June 8, 2023 at 3:59 pm

      Yes, or if you have comments in your code.

Leave a Comment