Arduino Syntax Reference Guide
Arduino Syntax Reference Guide
Interrupts allow an Arduino program to respond immediately to specific events, bypassing the default linear execution order. The attachInterrupt function configures these by linking a hardware event to a function. It requires parameters for interrupt number (0 for pin 2, 1 for pin 3), the function to execute, and the triggering mode (LOW, CHANGE, RISING, or FALLING).
Variable scope in Arduino determines the parts of a program where a variable can be accessed. Declaring variables globally makes them accessible throughout the entire sketch, whereas local declarations within functions like setup or loop limit accessibility to those specific blocks, affecting how variables retain or lose state across function calls .
Comments in Arduino sketches do not affect the execution of the code as they are ignored by the compiler. Despite this, comments are useful for providing explanations and clarifications for the benefit of other people who might read or use the code, fostering a better understanding and collaborative development practices .
Internal pull-up resistors in Arduino are created by writing a digital pin HIGH in the setup function (digitalWrite(12, HIGH)). This technique is used to avoid the need for external pull-up resistors, reducing circuit complexity and ensuring reliable pin state definition in switch or button input scenarios .
Serial communication in Arduino facilitates data transmission over a communication port by initializing with Serial.begin in the setup function, often with a baud rate of 9600. This establishes the communication speed, allowing the program to send and receive data smoothly to connected devices like computers or other microcontrollers .
In Arduino, assigning values to a variable is done using the equals sign '=', whereas comparing values uses '=='. Assignment changes the value stored in a variable, while comparison is used in conditional statements to evaluate if two values are equal, affecting the program's flow based on this evaluation .
The loop function continuously executes the code within it after the setup function has completed. Unlike setup, which runs once at the start, loop runs repeatedly, enabling the Arduino to perform repetitive tasks, like checking sensor statuses or updating outputs .
For loops initialize a variable, define a condition, and increment automatically within a single line, making them suited for fixed iteration counts. While loops repeat the code as long as a condition is true and require separate initialization and incrementation, allowing for more dynamic exit conditions like user input .
If-else statements in Arduino guide the program flow by executing a block of code if a specified condition is true, and an alternate block if the condition is false. The syntax requires the condition in parentheses, followed by the action in curly braces, e.g., if (this is true) { do this; } else { do this; } .
Setting up a digital pin as an output is done in the setup function using the pinMode function, e.g., pinMode(13, OUTPUT). This configuration is essential as it determines how the pin operates, enabling it to control an external device such as an LED or relay by sending signals .