Can't copy code from Arduino example pages

Copying code from the example sites does not seem to work.

For example from

If I select this setup code in the code box and try to copy it:

all I get is one line:

void setup() {
  //Initialize serial and wait for port to open:

If I select all the code, I can only copy and paste one line:

}

On that page, the output box has a copy icon and it works:

ASCII Table ~ Character Map
!, dec: 33, hex: 21, oct: 41, bin: 100001
", dec: 34, hex: 22, oct: 42, bin: 100010
#, dec: 35, hex: 23, oct: 43, bin: 100011
$, dec: 36, hex: 24, oct: 44, bin: 100100
%, dec: 37, hex: 25, oct: 45, bin: 100101
&, dec: 38, hex: 26, oct: 46, bin: 100110
', dec: 39, hex: 27, oct: 47, bin: 100111
(, dec: 40, hex: 28, oct: 50, bin: 101000
), dec: 41, hex: 29, oct: 51, bin: 101001
*, dec: 42, hex: 2A, oct: 52, bin: 101010
+, dec: 43, hex: 2B, oct: 53, bin: 101011
,, dec: 44, hex: 2C, oct: 54, bin: 101100
-, dec: 45, hex: 2D, oct: 55, bin: 101101
., dec: 46, hex: 2E, oct: 56, bin: 101110
/, dec: 47, hex: 2F, oct: 57, bin: 101111
0, dec: 48, hex: 30, oct: 60, bin: 110000
1, dec: 49, hex: 31, oct: 61, bin: 110001
2, dec: 50, hex: 32, oct: 62, bin: 110010
3, dec: 51, hex: 33, oct: 63, bin: 110011
4, dec: 52, hex: 34, oct: 64, bin: 110100
5, dec: 53, hex: 35, oct: 65, bin: 110101
6, dec: 54, hex: 36, oct: 66, bin: 110110
7, dec: 55, hex: 37, oct: 67, bin: 110111
8, dec: 56, hex: 38, oct: 70, bin: 111000
9, dec: 57, hex: 39, oct: 71, bin: 111001
:, dec: 58, hex: 3A, oct: 72, bin: 111010
;, dec: 59, hex: 3B, oct: 73, bin: 111011
<, dec: 60, hex: 3C, oct: 74, bin: 111100
=, dec: 61, hex: 3D, oct: 75, bin: 111101
>, dec: 62, hex: 3E, oct: 76, bin: 111110
?, dec: 63, hex: 3F, oct: 77, bin: 111111
@, dec: 64, hex: 40, oct: 100, bin: 1000000
A, dec: 65, hex: 41, oct: 101, bin: 1000001
B, dec: 66, hex: 42, oct: 102, bin: 1000010
C, dec: 67, hex: 43, oct: 103, bin: 1000011
D, dec: 68, hex: 44, oct: 104, bin: 1000100
E, dec: 69, hex: 45, oct: 105, bin: 1000101
...

I would expect that all the code is copied. It's a pain because I like to refer folks to bit from the official tutorials, and I can't copy the code form there.

It would also be nice if the examples had a code-copy-icon like the code chunks in the forum do.

I'm using a Mac with Chrome, in case that is an issue.

It looks like this is not simple text, but something inside of a JavaScript app or something of the sort.

On my system (Linux / Debian sid) using Firefox, the code can be copied by using CTRL-C after highlighting it and it can be pasted using CTRL-V (or CTRL-SHIFT-V in a terminal). The standard paste using the middle mouse button does nothing.

I hope this helps.

1 Like

In IDE look into the Examples menu

2 Likes

It might be; I have no problem copying using Firefox on Windows 10 using ControlC and ControlV. Not a Mac user so no idea how that translates; CMD ?

Yes, all you get is one line but a VERY LONG ONE with all the code. Just add some "Carriage Returns"

Hello DaveX,
that's because the code is embedded differently. Just highlight everything and then copy with ctrl+c, paste with ctrl+v. That should usually work already (For me on Windows. I know you're using macOS). I hate to say it, but the cause is most likely your device and not the website....
With best regards,
cloudDev

Nope. Just the text I pasted in the boxes:

Neither of them is a long line w/o carriage returns.

Nope. crtl-c on the code box, and then ctrl-v here acted as if I hadn't pressed ctrl-c, and ctrl-v pasted the stuff from an older copy&paste.

My normal mac copy-paste command is command-C and command-V and it works for the non-code text on those pages.

Also, if instead of the hotkeys, I try the Chrome edit commands from the window menu, Edit/Copy and Edit/Paste, then the Edit/Copy command appears to not update the clipboard with the code, and the Edit/Paste pastes the text from the immediately previous copy.

What about Highlight/right-click ?

All the other stuff on the page copies and pastes as normal.

I think that the code inside this iframe/scrolling/code box is managed by Javascript and handles highlighting and copy/paste differently than the rest of the page.

<textarea class="ace_text-input" wrap="off" autocorrect="off" autocapitalize="off" spellcheck="false" style="z-index: 100000; text-indent: -226.223px; transform: translate(199px, 116px); height: 1px; width: 1px;" readonly=""></textarea>

Nope. Highlighting + rightClick+Copy is as if I didn't click on Copy. A subsequent paste just gets me the previously copied text.

I noticed that the highlighting in the box is a different color:

versus the blue in:

Oh -- I notice that the yellow did not become un-highlighted when I highlighted the blue area.

I think that whatever code is doing the yellow highlighting in the code box isn't doing normal cut-and-paste and is doing a poor job of recognizing and acting on a copy event.

What if out highlight/copy the entire text on the page ? Can you extract something useful ?
Question may be outdated...

Here's from before to after the code box and halfway through the Output box:

=======================================

Code
The sketch waits for a serial connection in the setup() then prints line by line the ASCII table up to the last printable character. When this is accomplished, it enters an endless loop in a while structure and nothing else happens. Closing and opening the serial monitor window of the Arduino Software (IDE) should reset the board and restart the sketch.

Output
COPY
1 ASCII Table ~ Character Map
2 !, dec: 33, hex: 21, oct: 41, bin: 100001
3 ", dec: 34, hex: 22, oct: 42, bin: 100010
4 #, dec: 35, hex: 23, oct: 43, bin: 100011
5 $, dec: 36, hex: 24, oct: 44, bin: 100100
6 %, dec: 37, hex: 25, oct: 45, bin: 100101
7 &, dec: 38, hex: 26, oct: 46, bin: 100110
8 ', dec: 39, hex: 27, oct: 47, bin: 100111
9 (, dec: 40, hex: 28, oct: 50, bin: 101000
10 ), dec: 41, hex: 29, oct: 51, bin: 101001
11 *, dec: 42, hex: 2A, oct: 52, bin: 101010
12 +, dec: 43, hex: 2B, oct: 53, bin: 101011
13 ,, dec: 44, hex: 2C, oct: 54, bin: 101100
14 -, dec: 45, hex: 2D, oct: 55, bin: 101101
15 ., dec: 46, hex: 2E, oct: 56, bin: 10111

In case all you want is the content of this box, here it is :

/*
  ASCII table

  Prints out byte values in all possible formats:
  - as raw binary values
  - as ASCII-encoded decimal, hex, octal, and binary values

  For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII

  The circuit: No external hardware needed.

  created 2006
  by Nicholas Zambetti <http://www.zambetti.com>
  modified 9 Apr 2012
  by Tom Igoe

  This example code is in the public domain.

  https://www.arduino.cc/en/Tutorial/BuiltInExamples/ASCIITable
*/

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

  // prints title with ending line break
  Serial.println("ASCII Table ~ Character Map");
}

// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33;
// you can also write ASCII characters in single quotes.
// for example, '!' is the same as 33, so you could also use this:
// int thisByte = '!';

void loop() {
  // prints value unaltered, i.e. the raw binary version of the byte.
  // The Serial Monitor interprets all bytes as ASCII, so 33, the first number,
  // will show up as '!'
  Serial.write(thisByte);

  Serial.print(", dec: ");
  // prints value as string as an ASCII-encoded decimal (base 10).
  // Decimal is the default format for Serial.print() and Serial.println(),
  // so no modifier is needed:
  Serial.print(thisByte);
  // But you can declare the modifier for decimal if you want to.
  // this also works if you uncomment it:

  // Serial.print(thisByte, DEC);


  Serial.print(", hex: ");
  // prints value as string in hexadecimal (base 16):
  Serial.print(thisByte, HEX);

  Serial.print(", oct: ");
  // prints value as string in octal (base 8);
  Serial.print(thisByte, OCT);

  Serial.print(", bin: ");
  // prints value as string in binary (base 2) also prints ending line break:
  Serial.println(thisByte, BIN);

  // if printed last visible character '~' or 126, stop:
  if (thisByte == 126) {  // you could also use if (thisByte == '~') {
    // This loop loops forever and does nothing
    while (true) {
      continue;
    }
  }
  // go on to the next character
  thisByte++;
}

Problem not solved but worked around...

My normal workaround is to google for "github arduino examples"

It works reliably, especially details like finding the storage type of like "github arduino analogread".

I don't like the idea of pointing folks at the example code pages for guidance on a particular question if they can't reliably copy the code from the page.

Your Mac browser might have some security thing against javascript (the output of the js code inside the window).

[edit]
This is the HTML embedding the sketch with JS...

<iframe src="https://create.arduino.cc/example/builtin/04.Communication%5CASCIITable/ASCIITable/preview?embed&amp;snippet" style="height:510px;width:100%;margin:10px 0" frameBorder="0"></iframe>

(%5C is the escape character "\")

That link to

has the same issues for me.

I can copy from the code block on what looks like the same javascript editor:

https://ace.c9.io/

However, I do find that if I use chrome to inspect the text within the block and edit out the readonly attribute, I can copy as normal:

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

  // prints title with ending line break
  Serial.println("ASCII Table ~ Character Map");
}

I edited this chunk

<textarea class="ace_text-input" wrap="off" autocorrect="off" autocapitalize="off" spellcheck="false" style="z-index: 100000; text-indent: -58.5059px; transform: translate(122px, 172px);" readonly=""></textarea>

to be:

<textarea class="ace_text-input" wrap="off" autocorrect="off" autocapitalize="off" spellcheck="false" style="z-index: 100000; text-indent: -58.5059px; transform: translate(122px, 172px);" =""></textarea>

So it isn't that the browser is incapable, or that the mac is incapable, or that I'm hitting the wrong keys, is just that some interaction with the way the javascript-based editor is working with the mac in readonly mode prevents reliable cutting and pasting. Without changing the readonly tag, I was getting one line of the proper text, even in the first post.

I blame the programmer for this error, not myself as the operator.

hahha... don't shoot the messenger. Cross-platform internet pages have been an issue due to proprietary functions since everyone had a better idea for a browser than Netscape. Try firefox?

With firefox it works as expected:

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

  // prints title with ending line break
  Serial.println("ASCII Table ~ Character Map");
}

// first visible ASCIIcharacter '!' is number 33:
int thisByte = 33;
// you can also write ASCII characters in single quotes.
// for example, '!' is the same as 33, so you could also use this:
// int thisByte = '!';

The beauty of convention-following programmers.

1 Like