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
- Read the value on the left side of
+. - Read the value on the right side of
+. - Add both values and produce one result.
- 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 + intgives an integer result.long + intis promoted to a wider integer type.float + intgives a float result.- Use a suitable type to avoid overflow in big additions.
| Expression | Output Type | Example Result |
|---|---|---|
int + int | int | 200 + 100 = 300 |
long + int | long | 70000L + 100 = 70100 |
float + int | float | 1.5 + 2 = 3.5 |
+ vs += (Comparison)
a = a + b;anda += 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 boardsUse 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
int ledPin = 2;stores LED pin number once for reuse.baseDelayandextraDelayhold separate timing parts.long totalMs = 0;keeps a growing total without early overflow.float voltageAandvoltageBstore decimal values.blinkDelay = baseDelay + extraDelaycalculates one combined delay.totalVoltage = voltageA + voltageBperforms decimal addition.- LED turns ON, waits, turns OFF, then waits again using
blinkDelay. totalMs += blinkDelayadds current cycle delay to running total.Serial.print/printlnshows current computed values in monitor.- 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
- Compiler generates machine instructions for add operation.
- CPU loads left operand and right operand into working registers.
- ALU executes addition in binary form.
- Status flags may change (carry/overflow depending on architecture).
- Result is written back to the destination register/memory variable.
- 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 =+ 5by mistake instead ofx += 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
- Change
baseDelayfrom 300 to 400 and observe newblinkDelay. - Add
int warmup = 100;and computeint cycle = blinkDelay + warmup;. - Create
float avgVoltage = totalVoltage / 2.0;and print it in Serial Monitor. - Replace one
totalMs += blinkDelay;withtotalMs = totalMs + blinkDelay;and compare behavior.
Try it now
Open the simulator workspace and step through + and += behavior line by line.