Code Basics -Arduino C
|
The Arduino uses a slightly reduced C/C++ programming language. In this recipe, we will remember a few basics of C/C++.
Ensure that you have Arduino IDE Installed on your Laptop/PC.
Here is a simple example of basic Arduino C/C++ manipulating two variables:
// Global Variables
int var1 = 10;
int var2 = 20;
void setup() {
// Only execute once when the Arduino boots
var2 = 5; // var2 becomes 5 once the Arduino boots
}
void loop(){
// Code executes top-down and repeats continuously
if (var1 > var2)
{
// If var1 is greater than var2
var2++; // Increment var2 by 1
}
else
{
// If var1 is NOT greater than var2
var2 = 0; // var2 becomes 0
}
}
When we upload a sketch to the board, the Arduino software first compiles the code. If there is an error in the code, it will write it in the Status Display area and will stop the upload. If no errors are found, it will begin writing the compiled code to the board.
Errors will appear if the board or serial port is not properly selected. When everything is correctly set up, the TX RX LEDs will blink, meaning data is being transferred between the computer and the Arduino board. When the transfer is done, the board will reset and the code will immediately begin executing.
The code is stored in the Arduino board until it is erased or replaced by another code. We can take the board and plug it into a battery or to another computer, and it will still execute this blinking.
Code Basics - Arduino Pins
|
The most important feature of the Arduino is its control over digital input/output (I/O) pins. On each pin, we can set a voltage value of 5 V, representing logic HIGH, or 0 V, representing logic LOW. Also, we can read whether a value of 5 V or 0 V is applied externally. Here we will learn how.
The following code turns a pin HIGH and LOW repeatedly while reading the external voltage applied to another:
void setup() {
// Set pin 2 as a digital Output
pinMode(2, OUTPUT);
// Set pin 3 as a digital Input
pinMode(3, INPUT);
}
void loop(){
// Set pin 2 HIGH
digitalWrite(2, HIGH);
>
// Wait 100 milliseconds
delay(100);
// Set pin 2 LOW
digitalWrite(2, LOW);
// Wait 100 milliseconds
delay(100);
// Read the value of pin 3 and store it in a variable
int pinValue = digitalRead(3);
}
The code sets two pins in output and input mode and then writes and reads from them. Here is the code breakdown:
In setup(), we use the pinMode() function to set pin number 2 as an output. When we set a pin as an output, we can set that pin as either HIGH (5 V) or LOW (0 V). Also, we set pin number 3 as an input. A pin configured as input can read external voltages applied to it. It can read HIGH if the voltage is around 5 V and LOW if the voltage is close or equal to 0 V:
void setup() {
// Set pin 2 as a digital Output
pinMode(2,OUTPUT);
// Set pin 3 as a digital Input
pinMode(3,OUTPUT);
}
In the loop() function, we use the digitalWrite() function to set pin number 2 to HIGH. Then, we wait for 100 milliseconds using the delay() function. This function stops the execution of the code for the specified time, in milliseconds. Thereafter, we set the pin to LOW and wait another 100 milliseconds. In the end, we read the value of pin 3 in a variable:
void loop(){
// Set pin 2 HIGH
digitalWrite(2, HIGH);
// Wait 100 milliseconds
delay(100);
// Set pin 2 LOW
digitalWrite(2, LOW);
// Wait 100 milliseconds
delay(100);
// Read the value of pin 3 and store it in a variable
int pinValue = digitalRead(3);
}
Comments
Post a Comment