Arduino-Syntax
Arduino syntax is based on C/C++ programming languages. Here are the basics to help you get started with writing Arduino code (also known as sketches):
Basic Structure of an Arduino Sketch
An Arduino sketch typically consists of two main functions: setup()
and loop()
.
cppvoid setup() {
// Initialization code runs once
}
void loop() {
// Main code runs repeatedly
}
- setup(): This function runs once when the board is powered on or reset. It is used to initialize variables, pin modes, start using libraries, etc.
- loop(): After the
setup()
function, theloop()
function runs repeatedly in a loop. This is where the main logic of the program goes.
Basic Commands
Digital I/O
pinMode(pin, mode): Sets the mode of a specified pin (INPUT, OUTPUT, or INPUT_PULLUP).
cpppinMode(13, OUTPUT); pinMode(2, INPUT);
digitalWrite(pin, value): Sets the specified digital pin to HIGH or LOW.
cppdigitalWrite(13, HIGH); digitalWrite(13, LOW);
digitalRead(pin): Reads the value from a specified digital pin (returns HIGH or LOW).
cppint buttonState = digitalRead(2);
Analog I/O
analogRead(pin): Reads the value from a specified analog pin (returns a value between 0 and 1023).
cppint sensorValue = analogRead(A0);
analogWrite(pin, value): Writes an analog value (PWM wave) to a specified pin (value between 0 and 255).
cpp>analogWrite(9, 128);
Timing
delay(ms): Pauses the program for the amount of time (in milliseconds) specified as parameter.
cppdelay(1000); // Wait for 1 second
millis(): Returns the number of milliseconds since the Arduino board began running the current program.
cppunsigned long currentTime = millis();
Control Structures
If Statement
cppif (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
For Loop
cppfor (initialization; condition; increment) {
// Code to execute in the loop
}
While Loop
cppwhile (condition) {
// Code to execute while the condition is true
}
Do-While Loop
cppdo {
// Code to execute
} while (condition);
Functions
You can define your own functions to encapsulate and reuse code.
cppvoid myFunction() {
// Code for the function
}
int add(int a, int b) {
return a + b;
}
Example Sketch
Here's an example that combines several of these elements to blink an LED when a button is pressed.
cppconst int ledPin = 13;
const int buttonPin = 2;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
Libraries
Arduino has many built-in and third-party libraries to extend its functionality. To use a library, include it at the beginning of your sketch.
cpp#include // Include the Servo library
Servo myServo; // Create a servo object
void setup() {
myServo.attach(9); // Attach the servo to pin 9
}
void loop() {
myServo.write(90); // Set the servo to 90 degrees
delay(1000);
myServo.write(0); // Set the servo to 0 degrees
delay(1000);
}
Common Libraries
- Servo: Controls servo motors.
- Wire: Used for I2C communication.
- SPI: Used for SPI communication.
- SoftwareSerial: Allows serial communication on other digital pins.
To summarize, Arduino syntax is straightforward if you're familiar with C/C++. It provides a wide range of
functions and libraries to control hardware and create interactive projects. The combination of
setup()
and loop()
, along with simple functions for I/O, timing, and control
structures, makes it easy to get started and build complex applications.