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 4: Solve Math Problems

Arithmetic operators are useful for doing calculations in your sketch.  In this activity, we’ll focus on the basics: assignment (=), addition (+), subtraction (–), multiplication (*), division(/), and modulus (%, the remainder of a division calculation).

  • Open up the Arduino Language Reference, and take a look at the list of Arithmetic Operators.

The next example sketch, SimpleMath, adds the variables a and b together and stores the result in c.  It also displays the result in the Serial Monitor.

Notice that c is now declared as an int, not a char variable type.  Another point, int c = a + b uses the assignment operator (=) to copy the result of the addition operation that adds a to b.  The figure below shows the expected result of 89 + 42 = 131 in the Serial Monitor.

  • Enter, save, and upload SimpleMath to your Arduino.
  • Check the result in the Serial Monitor. Is it correct?
// Robotics with the BOE Shield - SimpleMath

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

  int a = 89;
  int b = 42;
  int c = a + b;
 
  Serial.print("a + b = ");
  Serial.println(c);
}

void loop()
{
  // Empty, no repeating code.
}
Fit your variables to the result values you need to store. 
This will use less memory so you can write larger sketches that will execute more efficiently.

If you need to work with decimal point values, use float.

If you are using integer values (counting numbers),  choose byte, int, or long.

If your results will always be an unsigned number from 0 to 255, use byte.

If your results will not exceed –32,768 to 32,767, an int variable can store your value.

If you need a larger range of values, try a long variable instead.  It can store values from  ‑2,147,483,648 to 2,147,483,647.

Your Turn – Experiment with Other Arithmetic Operators

You still have –, *, /, and % to try out!

  • Replace the addition (+) operator with the subtraction (–) operator and re-upload the sketch to the Arduino.  (You’ll want to replace both instances of + in the sketch.)
  • Upload and verify the result in the Serial Monitor.
  • Repeat for the multiplication ( *), division (/) and modulus ( % ) operators.

 


Printer-friendly version
Global vs.Local Variables
Prev
Floating Point Math
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress