📜  if arduino - C++ (1)

📅  最后修改于: 2023-12-03 14:42:03.301000             🧑  作者: Mango

Arduino C++ Programming

Arduino is an open-source electronics platform based on easy-to-use hardware and software. It’s designed to make it easy for anyone to create interactive devices and projects. Arduino programming language is a simplified version of C++.

Syntax

The syntax of the Arduino programming language is similar to C++, but with some differences due to the simplified nature of the language. An Arduino program consists of two functions:

  1. setup() - This function is called once when the Arduino board is powered up or reset. It is used to initialize variables or set pin modes.

  2. loop() - This function is called repeatedly while the Arduino board is powered up or reset. It contains the main code of the program.

Here’s an example:

void setup() {
  // initialize digital pin LED_BUILTIN as an output
  pinMode(LED_BUILTIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}
Functions

Some of the commonly used functions in the Arduino programming language are:

  1. digitalRead() – Reads the value of a digital pin (HIGH or LOW).

  2. digitalWrite() – Writes a value to a digital pin (HIGH or LOW).

  3. analogRead() – Reads the value of an analog pin (0 to 1023).

  4. analogWrite() – Writes a value to an analog pin (0 to 255).

  5. delay() – Delays the program for a specific amount of time in milliseconds.

  6. millis() – Returns the number of milliseconds since the Arduino board was powered up or reset.

  7. Serial.begin() – Initializes the serial communication at a specific baud rate.

  8. Serial.print() – Writes data to the serial port as human-readable ASCII text.

Libraries

Libraries in Arduino are collections of pre-written code that can be imported into your program to simplify complex tasks. For example, the “Wire” library is used for I2C communication, the “Servo” library is used for controlling servo motors, and so on. You can also create your own custom libraries.

Libraries are imported at the beginning of the program using the #include statement. For example:

#include <Wire.h>
Conclusion

Arduino programming is a simplified version of C++, but it’s still a powerful tool for creating interactive projects. With its easy-to-use hardware and software, it’s ideal for hobbyists and beginners who want to learn programming and electronics.