Mission 30 · Stage 3

Simple Calculator

arithmetic expressions with variables

Add two int variables and print the sum on Serial.

Simple Calculator circuit diagram

See it

Boxes that do math!

Two number boxes get added together, and the answer pops up on the screen.

Every calculator app keeps numbers in boxes before adding them up.

The story

The problem

We want to change two numbers and see the answer update by itself.

Think of it like

It's like two jars of marbles — pour them together and count the total.

Meet the parts

It adds the two numbers and shows the answer.

Arduino

The brain

Loading part…

How it works

1

Two number boxes

numberA and numberB hold the two numbers you want to add — change them in the Code lab.

int numberA = 12;
int numberB = 8;
2

Add them together

sum is a third box that holds the answer of the adding.

sum = numberA + numberB;
3

Show the whole sum

Shows 12 + 8 = 20 on the screen, waits two seconds, then adds again.

Serial.print(numberA);
Serial.print(" + ");
Serial.println(sum);

Then loop back to step 1

Build the circuit

Follow these steps in order. Match the wires to the colors shown.

  1. 1

    Place Arduino

    Place the Arduino (uno) on the breadboard.

    Arduino placed — ready to add!

    Loading part…

Try it

  • The screen shows 12 + 8 = 20 every two seconds.
  • Change numberA or numberB in the Code lab and run again!

Peek at code

The three boxes

int numberA = 12;
int numberB = 8;
int sum = 0;

numberA and numberB are the two numbers to add, and sum holds the answer.

Add them up

  Serial.print(numberA);
  Serial.print(" + ");
  Serial.print(numberB);
  Serial.print(" = ");
  Serial.println(sum);
  delay(2000);
}

Each loop adds the two numbers and shows the whole sum on the screen.

Show full sketch (simple-calculator.ino)
int numberA = 12;
int numberB = 8;
int sum = 0;
void setup() {
  Serial.begin(9600);
  Serial.println("Simple Calculator — change A and B in Code lab");
}
void loop() {
  sum = numberA + numberB;
  Serial.print(numberA);
  Serial.print(" + ");
  Serial.print(numberB);
  Serial.print(" = ");
  Serial.println(sum);
  delay(2000);
}

Quick quiz

Q1. What is a variable?

  • A. A named box that stores a number
  • B. A kind of wire
  • C. Just a wait
Why: Yes! int numberA = 12; makes a box named numberA.

Q2. Why use a sum box instead of just adding once?

  • A. So the answer is saved and can be shown clearly
  • B. Boxes cannot do math
  • C. The screen only prints boxes named sum
Why: Yes! sum saves the answer in a named box.

Code lab — try on your own

  1. Try different math — change int numberA = 12 to int numberA = 20.

    Hint: Line 1.

  2. Change the second number — set int numberB = 8 to int numberB = 15.

    Hint: Line 2.

Code walkthrough

A line-by-line tour of the sketch — the same steps as in Robot Gurukul Studio.

Program overview

Big idea

Every Arduino program has a top part, a setup() part that runs once, and a loop() part that runs again and again.

In this project

We keep two numbers in boxes, add them, and show the answer on the screen.

Tip

Read from the top to the bottom. Tap any word or line if you need help!

int numberA = 12;
int numberB = 8;
int sum = 0;

setup()

Big idea

setup() runs one time when the board turns on.

In this project

It opens the message screen and says hello.

Why here

Things we do only once go inside setup().

void setup() {
  Serial.begin(9600);
  Serial.println("Simple Calculator — change A and B in Code lab");
}

loop()

Big idea

loop() runs again and again, forever.

In this project

This is where the two numbers get added and the answer is shown.

Why here

Things that repeat go inside loop().

void loop() {
  sum = numberA + numberB;
  Serial.print(numberA);
  Serial.print(" + ");
  Serial.print(numberB);
  Serial.print(" = ");
  Serial.println(sum);
  delay(2000);
}

Try this: Change a number inside loop(), then press Run to see what happens.

delay

Big idea

delay means wait. Nothing else happens while it waits.

In this project

It waits two seconds before showing the answer again.

Why here

Right after we show the answer.

  delay(2000);

begin

Big idea

Serial.begin opens a message screen so the board can talk to the computer.

In this project

It lets us print the answer.

Why here

It goes in setup() once, before we print anything.

  Serial.begin(9600);

print

Big idea

Serial.print writes words or a number on the screen and stays on the same line.

In this project

It writes the first number on the same line as the rest of the sum.

Why here

In loop() so the whole sum shows on one line.

  Serial.print(numberA);

println

Big idea

Serial.println writes on the screen and then jumps to a new line.

In this project

It prints a friendly hello when the board turns on.

Why here

So each message gets its own line.

  Serial.println("Simple Calculator — change A and B in Code lab");