Operators

Lesson 30: Using + Addition Arithmetic Operator

Learn how + adds values, combines variables and constants, and how it differs from += in Arduino code.

Progress indicator

Lesson 30 of 57

Learning Objectives

  • Understand what the + operator does in Arduino.
  • Use + with variables, constants, and expressions.
  • Apply + with int, long, and float values.
  • Understand the difference between + and +=.
  • Avoid overflow and common type-conversion mistakes when adding values.

Concept Explanation

What is the Addition Operator (+)

The + operator adds two values and returns the result.

In Arduino, it is commonly used for counters, timing math, and sensor calculations.

Addition Operator Syntax

result = valueA + valueB;
result = variable + 10;
result = (a + b) + c;

How + Works

  1. Read the value on the left side of +.
  2. Read the value on the right side of +.
  3. Add both values and produce one result.
  4. Store or use that result in the current statement.

The result type can change based on input types. For example, int + float gives a float result.

Adding Variables and Constants

You can add variable + variable, variable + constant, or constant + constant.

int delayA = 300;
int delayB = 200;
int totalDelay = delayA + delayB;

+ with Different Data Types

  • int + int gives an integer result.
  • long + int is promoted to a wider integer type.
  • float + int gives a float result.
  • Use a suitable type to avoid overflow in big additions.
ExpressionOutput TypeExample Result
int + intint200 + 100 = 300
long + intlong70000L + 100 = 70100
float + intfloat1.5 + 2 = 3.5

+ vs += (Comparison)

  • a = a + b; and a += b; are equivalent.
  • += is shorter and often easier to read for repeated updates.
  • Use + when you want to build a new expression, and += when you want to update the same variable.

When to Use +

  • Building elapsed-time totals
  • Combining sensor values
  • Computing thresholds and offsets
  • Increasing counters in loops

Overflow and Wrap-Around

If the result is bigger than the variable can store, overflow happens. The value wraps to an unexpected number.

int x = 32760;
x = x + 20; // overflow on many boards

Use long or unsigned long when sums can grow large.

Example Code

This sketch uses + for delay math and float addition, then uses += to accumulate total time.

int ledPin = 2;
int baseDelay = 300;
int extraDelay = 200;
long totalMs = 0;
float voltageA = 1.5;
float voltageB = 1.8;

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  int blinkDelay = baseDelay + extraDelay;
  float totalVoltage = voltageA + voltageB;

  digitalWrite(ledPin, HIGH);
  delay(blinkDelay);
  digitalWrite(ledPin, LOW);
  delay(blinkDelay);

  totalMs += blinkDelay;

  Serial.print("blinkDelay = ");
  Serial.println(blinkDelay);
  Serial.print("totalVoltage = ");
  Serial.println(totalVoltage);
  Serial.print("totalMs = ");
  Serial.println(totalMs);
}

Example Code Explanation

  1. int ledPin = 2; stores LED pin number once for reuse.
  2. baseDelay and extraDelay hold separate timing parts.
  3. long totalMs = 0; keeps a growing total without early overflow.
  4. float voltageA and voltageB store decimal values.
  5. blinkDelay = baseDelay + extraDelay calculates one combined delay.
  6. totalVoltage = voltageA + voltageB performs decimal addition.
  7. LED turns ON, waits, turns OFF, then waits again using blinkDelay.
  8. totalMs += blinkDelay adds current cycle delay to running total.
  9. Serial.print/println shows current computed values in monitor.
  10. The loop repeats, so totals and prints keep updating continuously.

Real-life analogy

Think of a school day timer: class time + break time = one full period. After each period, you add that period time to your total day time.

What Happens Inside

  1. Compiler generates machine instructions for add operation.
  2. CPU loads left operand and right operand into working registers.
  3. ALU executes addition in binary form.
  4. Status flags may change (carry/overflow depending on architecture).
  5. Result is written back to the destination register/memory variable.
  6. Next line executes using updated value.

Why this matters for beginners

Understanding this flow helps you debug wrong totals, unexpected negative numbers, and loop counters that behave strangely after many iterations.

Common Mistakes with +

  • Using a too-small data type and causing overflow.
  • Mixing integer math and expecting decimal output without float types.
  • Confusing + for numeric math with text concatenation behavior.
  • Writing x =+ 5 by mistake instead of x += 5.
  • Adding values before variables are initialized to known defaults.

Best Practices for +

  • Choose the correct data type before adding values.
  • Use meaningful names for intermediate math results.
  • Use += for clean repeated accumulation.
  • Print values with Serial while learning math flow.
  • Use temporary variables to make long expressions easy to read.
  • For long-running totals, prefer wider types like long.

Practice Task

  1. Change baseDelay from 300 to 400 and observe new blinkDelay.
  2. Add int warmup = 100; and compute int cycle = blinkDelay + warmup;.
  3. Create float avgVoltage = totalVoltage / 2.0; and print it in Serial Monitor.
  4. Replace one totalMs += blinkDelay; with totalMs = totalMs + blinkDelay; and compare behavior.

Try it now

Open the simulator workspace and step through + and += behavior line by line.

Run in Simulator