Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Robotics with the Board of Education Shield for Arduino

Robotics with the Board of Education Shield for Arduino

Activity 3: Store and Retrieve Values

Variables are names you can create for storing, retrieving, and using values in the Arduino microcontroller’s memory.  Here are three example variable declarations from the next sketch:

  int a = 42;
  char c = 'm';
  float root2 = sqrt(2.0);

The declaration int a = 42 creates a variable named a.  The int part tells the Arduino software what type of variable it’s dealing with.  The int type can store integer values ranging from -32,768 to 32,767.  The declaration also assigns a an initial value of 42.  (The initial value is optional, you could instead just declare int a, and then later assign the value 42 to a with a = 42.)

Next, char c = ’m’ declares a variable named c of the type char (which is for storing characters) and then assigns it the value ’m’.

Then, float root2 = sqrt(2.0) declares a variable named root2. The variable type is float, which can hold decimal values. Here, root2 is initialized to the floating-point representation of the square root of two: sqrt(2.0).

Now that your code has stored values to memory, how can it retrieve and use them?  One way is to simply pass each variable to a function’s parameter.  Here are three examples, where the Serial.println(val) function displays the value of the variable inside the parentheses.

One nice thing about variable types is that Serial.println recognizes each type and displays it correctly in the serial monitor.  (Also, the C++ compiler in the Arduino software requires all declared variables to have a type, so you can’t leave it out.)

Example Sketch – StoreRetrieveLocal

  • Use File → New to create a new sketch, and save it as StoreRetrieveLocal.
  • Enter or copy the code below into the Arduino editor.
  • Save the file, then upload it to the Arduino.
  • Open the Serial Monitor and verify that the values display correctly.
// Robotics with the BOE Shield - StoreRetrieveLocal

void setup()
{
  Serial.begin(9600);

  int a = 42;
  char c = 'm';
  float root2 = sqrt(2.0);

  Serial.println(a);
  Serial.println(c);
  Serial.println(root2);
}

void loop()
{
  // Empty, no repeating code.
}

ASCII stands for American Standard Code for Information Exchange.
It’s a common code system for representing computer keys and characters in displays.  For example, both the Arduino and the Serial Monitor use the ASCII code 109 for the letter m.  The declaration char c = ’m’ makes the Arduino store the number 109 in the c variable.  Serial.println(c) makes the Arduino send the number 109 to the Serial Monitor.  When the Serial Monitor receives that 109, it automatically displays the letter m. View ASCII codes 0–127.

See that ’m’ really is 109

There are two ways to prove that the ASCII code for ’m’ really is 109.  First, instead of declaring char c = ’m’, you could use byte c = ’m’.  Then, the println function will print the byte variable’s decimal value instead of the character it represents.  Or, you could leave the char c declaration alone and instead use Serial.println(c, DEC) to display the decimal value c stores.

  • Try both approaches.

So, do you think the letters l, m, n, o, and p would be represented by the ASCII codes 108, 109, 110, 110, 111, and 112?

  • Modify your sketch to find out the decimal ASCII codes for l, m, n, o, p.
  • If you can, go to the ASCII Codes 0-127 page and experiment with other ASCII characters.

Printer-friendly version
Open the Arduino Reference
Prev
Global vs.Local Variables
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress