arduino编程语言
How can you write programs for your Arduino board?
您如何为Arduino开发板编写程序?
Arduino, natively, supports a language that we call the Arduino Programming Language, or Arduino Language.
Arduino本身支持一种称为Arduino编程语言或Arduino语言的语言。
This language is based upon the Wiring development platform, which in turn is based upon Processing, which if you are not familiar with, is what p5.js is based upon. It’s a long history of projects building upon other projects, in a very Open Source way. The Arduino IDE is based upon the Processing IDE, and the Wiring IDE which builds on top of it.
该语言基于Wiring开发平台,而Wiring开发平台又基于Processing ,如果您不熟悉, 它将是p5.js的基础。 以非常开源的方式在其他项目之上构建项目的历史悠久。 Arduino IDE基于处理IDE和在其之上构建的Wiring IDE。
When we work with Arduino we commonly use the Arduino IDE (Integrated Development Environment), a software available for all the major desktop platforms (macOS, Linux, Windows), which gives us 2 things: a programming editor with integrated libraries support, and a way to easily compile and load our Arduino programs to a board connected to the computer.
当我们使用Arduino时,我们通常使用Arduino IDE(集成开发环境),该软件可用于所有主要的台式机平台(macOS,Linux,Windows),它为我们提供了2项功能:具有集成库支持的编程编辑器和轻松地将Arduino程序编译并加载到与计算机连接的板上的方法。
The Arduino Programming Language is basically a framework built on top of C++. You can argue that it’s not a real programming language in the traditional term, but I think this helps avoiding confusion for beginners.
Arduino编程语言基本上是基于C ++构建的框架。 您可以争辩说它不是传统术语中的真正编程语言,但是我认为这有助于避免使初学者感到困惑。
A program written in the Arduino Programming Language is called sketch. A sketch is normally saved with the .ino
extension (from Arduino
).
用Arduino编程语言编写的程序称为sketch 。 草图通常保存与该.ino
延伸部(从Ardu ino
)。
The main difference from “normal” C or C++ is that you wrap all your code into 2 main functions. You can have more than 2, of course, but any Arduino program must provide at least those 2.
与“普通” C或C ++的主要区别是将所有代码包装到2个主要函数中。 当然,可以有2个以上,但是任何Arduino程序都必须至少提供2个。
One is called setup()
, the other is called loop()
. The first is called once, when the program starts, the second is repeatedly called while your program is running.
一个叫做setup()
,另一个叫做loop()
。 第一个调用一次,程序启动时,第二个在程序运行时重复调用。
We don’t have a main()
function like you are used to in C/C++ as the entry point for a program. Once you compile your sketch, the IDE will make sure the end result is a correct C++ program and will basically add the missing glue by preprocessing it.
我们没有main()
函数,就像您在C / C ++中习惯于将其作为程序的入口点一样。 编译完草图之后,IDE将确保最终结果是正确的C ++程序,并通过预处理将基本上添加缺失的胶水。
Everything else is normal C++ code, and as C++ is a superset of C, any valid C is also valid Arduino code.
其他所有内容都是普通的C ++代码,并且由于C ++是C的超集,因此任何有效的C也是有效的Arduino代码。
One difference that might cause you troubles is that while you can spawn your program over multiple files, those files must all be in the same folder. Might be a deal breaking limitation if your program will grow very large, but at that point it will be easy to move to a native C++ setup, which is possible.
可能引起麻烦的一个不同之处是,尽管可以在多个文件上生成程序,但这些文件必须全部位于同一文件夹中。 如果您的程序会变得很大,可能会成为一个突破性的限制,但是到那时,可以轻松地迁移到本机C ++设置。
Part of the Arduino Programming Language is the built-in libraries that allow you to easily integrate with the functionality provided by the Arduino board.
Arduino编程语言的一部分是内置库,可让您轻松地与Arduino开发板提供的功能集成。
Your first Arduino program will surely involve making a led turn on the light, and then turn off. To do so, you will use the pinMode()
, delay()
and digitalWrite()