This tutorial refers to a product that has reached its end-of-life status.

Arduino Yún Console ASCII Table

Demonstrates printing various formats to the Console.

This example for a Yún device demonstrates how to print to the Console by generating a table of characters and their ASCII values in decimal, hexadecimal, octal, and binary. For more on ASCII, see asciitable.com.

The Console, based on Bridge, enables you to send information from the Yún device to a computer just as you would with the serial monitor, but wirelessly. It creates a secure connection between the Yún device and your computer via SSH.

When your Yún device and computer are on the same network, you can find the Yún device in the Tools>Ports menu item in the Arduino Software (IDE).

Hardware Required

  • Yún board or shield

  • computer and Yún on the same wireless network

Circuit

There is no circuit for this example.

The circuit for this tutorial.
The circuit for this tutorial.

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Code

Include the Console library, which inherits from Bridge.

#include <Console.h>

Create a variable that will hold the value to print out to the Console window. ASCII characters of values 32 and below are invisible, so initialize the variable with a value of 33 (which corresponds to "!" ).

int byte = 33;

In

setup()
, initialize the Bridge and Console, and wait for the port to open. Once a connection has been made, print out a small bit of information describing what is going to follow :

1void setup() {
2
3 Bridge.begin();
4
5 Console.begin();
6
7 while (!Console) {
8
9 ; // wait for Console port to connect.
10
11 }
12
13 Console.println("ASCII Table ~ Character Map");
14}

In

loop()
, you will print the value in a number of different formats.

To see the ASCII value of the variable, you can write the byte with

Console.write()
. The Console interprets all bytes as ASCII characters.
Console.write(thisByte);

Console.print()
prints the value as a string of ASCII encoded decimals by default.
Console.print(thisByte);

Console.print()
and
Console.println()
can also send strings to the Console window that represent hexadecimal, octal, and binary number values with the appropriate modifier.
Console.println()
will add newline and carriage return characters to the string, creating a line break in the Console window.

1Console.print(thisByte, HEX);
2
3Console.print(thisByte, OCT);
4
5Console.println(thisByte, BIN);

In this example, you're only printing out the alphanumeric characters that appear on a USA keyboard, so there's no need to print any values past 126. To make sure all the data gets sent before stopping the sketch, make a call to

Console.flush()
.

1if(thisByte == 126) {
2
3 Console.flush();
4
5 while(true) {
6
7 continue;
8
9 }
10
11 }

If the sketch hasn't printed out all the values, increment

thisByte
before running through the
loop()
again.
thisByte++;

The complete sketch is below :

1/*
2
3 Console ASCII table for YunShield/Yún
4
5 Prints out byte values in all possible formats:
6
7 * as raw binary values
8
9 * as ASCII-encoded decimal, hex, octal, and binary values
10
11 For more on ASCII, see http://www.asciitable.com and http://en.wikipedia.org/wiki/ASCII
12
13 The circuit:
14
15 - YunShield/Yún
16
17 created 2006
18
19 by Nicholas Zambetti
20
21 http://www.zambetti.com
22
23 modified 9 Apr 2012
24
25 by Tom Igoe
26
27 modified 22 May 2013
28
29 by Cristian Maglie
30
31 This example code is in the public domain.
32
33 http://www.arduino.cc/en/Tutorial/ConsoleAsciiTable
34
35 */
36
37#include <Console.h>
38
39void setup() {
40
41 //Initialize Console and wait for port to open:
42
43 Bridge.begin();
44
45 Console.begin();
46
47 // Uncomment the following line to enable buffering:
48
49 // - better transmission speed and efficiency
50
51 // - needs to call Console.flush() to ensure that all
52
53 // transmitted data is sent
54
55 //Console.buffer(64);
56
57 while (!Console) {
58
59 ; // wait for Console port to connect.
60
61 }
62
63 // prints title with ending line break
64
65 Console.println("ASCII Table ~ Character Map");
66}
67
68// first visible ASCIIcharacter '!' is number 33:
69int thisByte = 33;
70// you can also write ASCII characters in single quotes.
71// for example. '!' is the same as 33, so you could also use this:
72//int thisByte = '!';
73
74void loop() {
75
76 // prints value unaltered, i.e. the raw binary version of the
77
78 // byte. The Console monitor interprets all bytes as
79
80 // ASCII, so 33, the first number, will show up as '!'
81
82 Console.write(thisByte);
83
84 Console.print(", dec: ");
85
86 // prints value as string as an ASCII-encoded decimal (base 10).
87
88 // Decimal is the default format for Console.print() and Console.println(),
89
90 // so no modifier is needed:
91
92 Console.print(thisByte);
93
94 // But you can declare the modifier for decimal if you want to.
95
96 //this also works if you uncomment it:
97
98 // Console.print(thisByte, DEC);
99
100 Console.print(", hex: ");
101
102 // prints value as string in hexadecimal (base 16):
103
104 Console.print(thisByte, HEX);
105
106 Console.print(", oct: ");
107
108 // prints value as string in octal (base 8);
109
110 Console.print(thisByte, OCT);
111
112 Console.print(", bin: ");
113
114 // prints value as string in binary (base 2)
115
116 // also prints ending line break:
117
118 Console.println(thisByte, BIN);
119
120 // if printed last visible character '~' or 126, stop:
121
122 if (thisByte == 126) { // you could also use if (thisByte == '~') {
123
124 // ensure the latest bit of data is sent
125
126 Console.flush();
127
128 // This loop loops forever and does nothing
129
130 while (true) {
131
132 continue;
133
134 }
135
136 }
137
138 // go on to the next character
139
140 thisByte++;
141}

Last revision 2016/05/25 by SM

Suggest changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.