Skip to content
RoboLearn
Not completed

Variables and data

ProgrammingBeginner25 minBefore this: Arduino C++ basics

What you’ll learn

  • How to create and change variables
  • Common Arduino data types and their limits
  • Scope: where a variable can be used
  • Why const is useful

Variables

A variable is a named box for a value:

int speed = 150; // create it, starting at 150
speed = 200; // change it
speed = speed + 10;

Data types

Type Holds Range on an UNO
bool true / false —
int Whole numbers −32,768 to 32,767
long Bigger whole numbers about ±2.1 billion
unsigned long Big non-negative numbers 0 to about 4.3 billion; used for millis()
float Decimals about 6–7 significant digits
char One character ‘A’, ‘7’
String Text “Hello”

Scope

  • Global variables (outside any function) keep their value and can be used everywhere.
  • Local variables (inside a function) exist only while that function runs and start fresh each time.

Constants

const int MOTOR_PIN = 9; can’t be changed by accident. Use constants for pin numbers and settings, so you only change them in one place.

Finished this lesson?