Merge pull request #899 from tenght/master

翻译完成
This commit is contained in:
Xingyu.Wang 2014-04-26 21:51:08 +08:00
commit 026e43bfaf
2 changed files with 161 additions and 162 deletions

View File

@ -1,162 +0,0 @@
+ translating ---------------------by tenght
+
Easy Arduino: Two Projects To Help You Get Started
================================================================================
> Many Arduino projects are based on just two simple programs.
Meet Arduino, the tiny microcontroller thats good at doing what your computer cant.
The computers we use every day are powerful, but theyre terrible at knowing whats going on around them. Your laptop isnt exactly equipped to sense light or moisture, for example. Arduino, on the other hand, is specifically designed to stay keyed in to the outside world. Its equipped with a board full of inputs and outputs for sensors to simplify communication.
The Arduino was developed by Massimo Banzi and his cofounders out of Ivrea, Italy, who named the device [after his favorite bar][1]. Banzi wanted his design students to have a cheap, easy solution for prototyping hardware solutions. Since Arduino's release in 2005, it has gone from a teaching tool to a DIY project for makers all over the world. [Multiple models][2] exist now—the more advanced Arduino Due, the souped-up Mega and the tinier Nano.
Regardless of which Arduino model you buy, the utility of the microcontroller comes out when you use it for "[Internet Of Things][3]" projects—whether you want to connect to the real world, the cloud, or both, Arduino makes it easy. In this primer, were going to highlight two ultra-basic projects in order to show just how simple Arduino can be.
### Getting Started With Arduino ###
Before we can fully understand what Arduino is, its important to realize what it isnt. An Arduino is not a computer (the way [Raspberry Pi][4] is). It cant be programmed independently; it needs to be plugged into a computer. It is not especially powerful either—the Arduino Uno has [32 KG of memory][5], while the average Macbook has about 8 gigs.
So in order to work with Arduino, you cant just buy the microcontroller and be done with it. For the following projects, heres what youll need:
### Hardware ###
- Arduino Uno. “Uno” means one in Italian, but this isnt the first Arduino ever built, just the most recent iteration of the most basic Arduino microcontroller. These projects will work with almost any model, but this is the one I used.
- Type B USB cable. I havent seen one of these old-style USB ports in years, but you might remember them from older electronic devices. [They cost about $5-10][6].
- LED light. A tiny light-emitting pin we can stick directly on the Arduino; [these also tend to be pretty cheap][7].
### Software ###
- [Arduino IDE][8], which stands for “integrated development environment,” is free open-source software for writing “sketches,” which is what Arduino users call programs.
When you open Arduino IDE for the first time, you need to establish the port where the software ought to expect the Arduino to show up. Go to “Tools,” then “Serial Port.”
- On [OS X][9], the serial port should be something like “/dev/tty.usbmodem” for the Uno, though it may be different for other types of Arduino boards. Mine is “/dev/tty.usbmodem1421.”
- On [Windows][10], the serial port should be COM3 or higher, as COM1 and COM2 are usually reserved for other hardware. In order to know for sure, you can unplug the Arduino and reopen the IDE menu. The entry that no longer appears is what your Arduino was.
In general, you shouldn't ever worry about unplugging your Arduino board. We know better than to do that with fully-functional computers, including Raspberry Pi, because they could be running an important OS task in the background. But Arduino is just a microcontroller; its definitely not writing anything to memory unless youre actively telling it to do so.
### Arduino Hardware Sketch: Blink An LED ###
In this first project, well overview the most basic way for Arduino to produce physical output, in this case, a blinking LED light.
Were going to write a very simple sketch to get an LED to strobe—or as my friend cleverly told me when I proudly showed him [my iteration][11], “a very tiny rave.” This program is based on the open-source [Blink sketch][12], which can be found on Arduino's official site.
First, set up the hardware by sticking the LEDs two prongs into GND and pin 13 on the Arduino.
![](http://readwrite.com/files/pin13andground.jpg)
One of the prongs is slightly shorter than the other—that shorter prong is the negative lead, and therefore the one that goes in the “ground,” or GND input/output.
![](http://readwrite.com/files/led_leads.jpg)
*Notice the shorter lead goes in GND.*
Now lets move onto writing the program. First, lets name it. Arduino will ignore anything on a line following two forward slashes, so this is a great way to leave notes for yourself:
// Program 1: Making an LED blink on and off
When hardwares involved, we need to tell the Arduino where it should expect a signal among its 14 different input/output pins. In this case, we stuck the LED in pin 13. This is how we name the LED stuck in pin 13, where “LED” is just the name I gave the variable:
int LED = 13;
Every Arduino sketch contains two components: void setup() and void loop(). These are the [bare minimum][13] for any Arduino program, without which it wont function.
In **void setup()**, we tell Arduino to initialize the pin as an output:
void setup() {
pinMode(LED, OUTPUT);
}
In **void loop()**, we tell Arduino to “write” a value to pin 13. Since we want it to blink, were going to ask it to write a sequence of four different commands in a row. As you can guess by the word “loop,” we expect this to continue until we turn the Arduino off.
In the following code, LED stands for the same variable we assigned before. "HIGH" tells the Arduino to deliver five volts of power to the LED, while "LOW" delivers zero volts. Telling it to delay for “1000” pauses the program between blinks for a full second.
void loop() {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}
When you put it all together, heres what the entire sketch should look like (again, Arduino ignores anything after two forward slashes, so those are just notes for yourself):
// Program 1: Making an LED blink on and off
int led = 13; // name the LED in pin 13
void setup() {
pinMode(LED, OUTPUT); // tell Arduino the pin in question is an output
}
void loop() {
digitalWrite(LED, HIGH); // deliver 5V to LED
delay(1000); // wait a second
digitalWrite(LED, LOW); // deliver 0V to LED
delay(1000); // wait a second
}
Press the checkmark to verify that your code is bug-free, and then press the play button. If your Arduino is plugged in, it should begin running your blinking light sketch.
### Arduino Software Sketch: Print To Computer ###
Lets now switch gears and try out the most basic project for demonstrating how Arduino produces a digital output.
Were going to set up the Arduino to “print,” or display information on your computer screen. This sketch is based on Paul Bianchis [Arduino printing tutorial][14].
Theres no hardware in this case, so we can just jump right into the program. Once again, its going to take place in two parts: **void setup()** and **void loop()**.
In **void setup()**, were going to open up a line of communication between Arduino and your computer, specifically at 9600 bits per second. If you use another number, you may end up getting gibberish printed to your computer instead of words.
void setup() {
Serial.begin(9600);
}
The **void loop()** section is going to look a lot like the one in our LED sketch. Were going to give it two separate commands, telling it to pause for a full second in between them. [Println][15] is the Arduino language command to get something to display on your computer. **Note the name "Println" uses a lowercase "L," not an uppercase "i"!**
void loop() {
Serial.println(“hello”);
delay(1000);
Serial.println(“world.”);
delay(1000);
}
Put it all together with a title and some comments, and it should look like this:
// Program 2: Make Arduino Print “Hello World” to Computer
void setup() {
Serial.begin(9600); // open a 9600 baud communication line to computer
}
void loop() {
Serial.println(“Hello”); // write the word “Hello”
delay(1000); // wait a second
Serial.println(“World”); // write the word “World”
delay(1000); // wait a second
}
Verify your code and run it. Nothing will appear until you click on “Serial Monitor” at the upper righthand corner of the Arduino IDE window. You should see a steady stream of “Hello world,” over and over again until you unplug Arduino. You'll see it's not perfect: mine always starts with a bit of gibberish but soon rights itself.
![](http://readwrite.com/files/Screen%20Shot%202014-04-21%20at%209.28.18%20AM.png)
These two Arduino projects are both extremely basic, but by executing them you can already begin to see the potential Arduino has to offer as a device that can communicate with sensors and write results to your computer. And if you combine these two projects—for example, you could connect a thermometer to your Arduino and tell it to write the temperature to your laptop—you'll soon realize the possibilities for Arduino are virtually endless.
--------------------------------------------------------------------------------
via: http://readwrite.com/2014/04/21/easy-arduino-projects-basics-tutorials-diy-hardware#feed=/hack
译者:[译者ID](https://github.com/译者ID) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://spectrum.ieee.org/geek-life/hands-on/the-making-of-arduino/0
[2]:http://arduino.cc/en/main/boards
[3]:http://en.wikipedia.org/wiki/Internet_of_Things
[4]:http://readwrite.com/2014/01/20/raspberry-pi-everything-you-need-to-know
[5]:http://arduino.cc/en/Main/arduinoBoardUno
[6]:https://www.google.com/search?q=type+b+usb&espvd=210&es_sm=91&source=univ&tbm=shop&tbo=u&sa=X&ei=2iVVU9DKDIbLsATni4LwDg&ved=0CCgQsxg&biw=1436&bih=658
[7]:https://www.google.com/search?es_sm=91&biw=1436&bih=658&tbm=shop&q=LED+light&oq=LED+light&gs_l=serp.3..0l10.65005.66134.0.66303.9.9.0.0.0.0.141.697.5j3.8.0.ehm_loc%2Chmss2%3Dfalse%2Chmnts%3D50000...0...1.1.41.serp..4.5.359.V7CTGdfZBFU
[8]:http://arduino.cc/en/main/software#toc1
[9]:http://arduino.cc/en/guide/macOSX#toc8
[10]:http://arduino.cc/en/guide/windows#toc8
[11]:http://instagram.com/p/mp6Gl7q3lU/
[12]:http://arduino.cc/en/Tutorial/Blink
[13]:http://arduino.cc/en/Tutorial/BareMinimum
[14]:http://quarkstream.wordpress.com/2009/12/09/arduino-1-writing-and-uploading-sketches/
[15]:http://arduino.cc/en/Serial/Println

View File

@ -0,0 +1,161 @@
+ translated ---------------------by tenght
+
Easy Arduino: 两个项目来帮助你开始
================================================================================
> 许多Arduino的项目是基于两个简单的程序。
能所你的电脑所不能的单片机那就是Arduino。
我们每天使用的计算机是强大的但他们根本不了解身边发生了什么事。比如说你的笔记本电脑也不能感到光或水。另外Arduino对外专门设计成键控的。它有一个用于简化传感器通信的输入、输出板子。
Arduino是由Massimo Banzi和他的意大利搭档Ivrea开发的并由Ivrea[他最喜欢的酒吧][1]命名。Banzi希望他的设计学生们有一个原型硬件的廉价容易的解决方案。自2005年Arduino的发布它已经从一个教学工具发展成为世界各地制造商的DIY项目。现在有了[多模型][2]——更先进的Arduino Due大马力微纳米。
无论你购买哪个Arduino模型当你用它来做“[互联网][3]”项目时单片机的实用性便体现出来了——Arduino可以很容易让你连接到真实世界云端或两者。本书中我们将突出显示两个超基项目以便展示Arduino可以多简单。
### 开始使用Arduino ###
在我们能够完全理解Arduino是什么之前知道它不是什么是很重要的。Arduino不是电脑跟卡片电脑[Raspberry Pi][4]不一样。它不能被独立编程需要被插入到计算机中去。它不是特别强大或Arduino Uno有[32 KB的内存][5] 而苹果笔记本平均有8G。
所以为了使用Arduino你不能只买单片机。以下的项目你需要的是
### 硬件 ###
- Arduino Uno. “Uno”在意大利语中是一的意思但这并不是有史以来第一个Arduino只是最基本的Arduino单片机的最新迭代。这些项目可以工作在任何一个模型上但这是我用这一个。
- B型USB线。我已经很多年没见过这些旧式的USB端口了但你可能会通过旧的电子设备记住他们。[他们的成本约5-10美元][6]。
- LED灯。可以直接粘在Arduino上的一个微小、发光的引脚[这些也往往是相当便宜的][7]。
### 软件 ###
- [Arduino IDE][8]意思是“集成开发环境”是一个免费开源软件用于“素描”也就是Arduino用户所说的程序。
当你第一次打开Arduino IDE你需要建立软件所使用的端口点击“工具”——>“串口”。
- 在[OS X][9]上对于Uno串口就像“/dev/tty.usbmodem”但其他类型的Arduino板可能不同。我的是“/dev/tty.usbmodem1421”。
- 在[Windows][10]串口是COM3或更高因为COM1和COM2通常保留给其他硬件。为确定端口号你可以拔掉Arduino并重新打开IDE菜单。哪个口子不再出现哪个就是你的Arduino。
一般来说你不用担心拔出你的Arduino电路板。我们知道功能齐全的电脑不会这样做包括Raspberry Pi因为他们可以在后台运行一个重要的操作系统任务。但是Arduino就是一个单片机除非你主动让它写内存否则它肯定是不会的。
### Arduino 硬件初步: 闪烁一个LED ###
在这第一个项目中我们将概述Arduino产生物理输出的最基本方式在这个实例中闪烁一个LED灯。
我们要去写一个非常简单的程序使得LED频闪——或者是当我自豪地向他展现了[我的迭代][11]时,我的朋友巧妙的跟我讲的,“一个非常小的狂欢”。本程序是基于开源项目[Blink sketch][12]它可以在Arduino的官网找到。
首先设置硬件将LED的两个叉分别粘到Arduino的GND和引脚13上。
![](http://readwrite.com/files/pin13andground.jpg)
一个叉略短于另外一个叉——短的这个时负极头因此这个接“地”或是GND输入/输出。
![](http://readwrite.com/files/led_leads.jpg)
*注意短的接GND.*
现在让我们开始写程序。首先让我们给它命名。Arduino将忽略行内双斜杠后边的内容所以这是为自己写注释的好方法
// Program 1: Making an LED blink on and off
当包含硬件时我们需要告诉Arduino在它的14跟不同的输入/输出引脚中哪儿会有信号。在这种情况下我们固定13引脚的LED。这是我们的如何命名固定在引脚13的LED其中“LED”只是我给的变量名
int LED = 13;
每一个Arduino程序由两部分组成void setup() 和 void loop()。这是能够运行的[最小的][13]Arduino程序。
在 **void setup()**中, 我们告诉Arduino初始化引脚为输出:
void setup() {
pinMode(LED, OUTPUT);
}
在**void loop()**中我们告诉Arduino“写”一个值到引脚13中。因为我想让它闪烁在每排中我们要使它写四个不同命令的一个序列。你能猜出的词“loop直到把Arduino关掉前我们希望它继续。
在下面的代码中LED代表我们分配了同一个变量。”HIGH”告诉Arduino给LED提供五伏的电源而“LOW”提供零伏。在闪烁时使得它延迟“1000”毫秒每一整秒
void loop() {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}
当把它们放在一起时整个程序如下再次声明Arduino忽略行内双斜杠后边的内容所以那只是你自己的注释
// 程序 1: 使一个LED闪烁开和关
int led = 13; // name the LED in pin 13
void setup() {
pinMode(LED, OUTPUT); // tell Arduino the pin in question is an output
}
void loop() {
digitalWrite(LED, HIGH); // deliver 5V to LED
delay(1000); // wait a second
digitalWrite(LED, LOW); // deliver 0V to LED
delay(1000); // wait a second
}
点击checkmark来验证你的代码是没有错误的然后按play键。如果你的Arduino已经插入它应该开始运行你的闪烁程序了。
### Arduino 软件程序: 打印到电脑上 ###
现在让我们来控制开关并尝试演示Arduino是如何产生数字输出的最基本工程。
我们将要设置Arduino “打印”或是在您的计算机屏幕上显示信息。这个程序基于Paul Bianchi的[Arduino printing tutorial][14]。
在这个实例中没有硬件,这样我们就可以直接跳到程序。再次,这将发生在两个部分: **void setup()****void loop()**
在**void setup()**中我们要打开一个Arduino和计算机之间通信的线程特别是在9600 b/s。如果你使用另一个号码你可能会在计算机上打印出乱码而不是文字。
void setup() {
Serial.begin(9600);
}
在**void loop()** 中这部分很像我们的一个LED程序。我们要给它两条独立的指令告诉它在他们之间每一整秒中断。[Println][15]是Arduino输出到你的电脑显示的命令。**注意"Println"使用的是小写的"L,"而不是大写的"i"!**
void loop() {
Serial.println(“hello”);
delay(1000);
Serial.println(“world.”);
delay(1000);
}
将标题和注释等放到一起,如下:
// 程序 2: 使Arduino打印 “Hello World”到电脑上
void setup() {
Serial.begin(9600); // open a 9600 baud communication line to computer
}
void loop() {
Serial.println(“Hello”); // write the word “Hello”
delay(1000); // wait a second
Serial.println(“World”); // write the word “World”
delay(1000); // wait a second
}
验证你的代码并运行。什么都不会显示直到你点击Arduino IDE窗口右上角的“Serial Monitor”。你应该看到一个不停输出的“Hello World”一遍又一遍直到你拔掉Arduino。你会看到它的不完美我总是有点乱码但很快它自己就好了。
![](http://readwrite.com/files/Screen%20Shot%202014-04-21%20at%209.28.18%20AM.png)
这两个Arduino项目都非常基本但是通过执行他们你已经开始看到可以与传感器通信和写结果到您的计算机的Arduino的潜力。如果你把这两个项目整合——例如你可以连接一个温度计到你的Arduino并告诉它将温度写到你的笔记本电脑你会很快实现Arduino的可能性几乎是无止境的。
--------------------------------------------------------------------------------
via: http://readwrite.com/2014/04/21/easy-arduino-projects-basics-tutorials-diy-hardware#feed=/hack
译者:[tenght](https://github.com/tenght) 校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创翻译,[Linux中国](http://linux.cn/) 荣誉推出
[1]:http://spectrum.ieee.org/geek-life/hands-on/the-making-of-arduino/0
[2]:http://arduino.cc/en/main/boards
[3]:http://en.wikipedia.org/wiki/Internet_of_Things
[4]:http://readwrite.com/2014/01/20/raspberry-pi-everything-you-need-to-know
[5]:http://arduino.cc/en/Main/arduinoBoardUno
[6]:https://www.google.com/search?q=type+b+usb&espvd=210&es_sm=91&source=univ&tbm=shop&tbo=u&sa=X&ei=2iVVU9DKDIbLsATni4LwDg&ved=0CCgQsxg&biw=1436&bih=658
[7]:https://www.google.com/search?es_sm=91&biw=1436&bih=658&tbm=shop&q=LED+light&oq=LED+light&gs_l=serp.3..0l10.65005.66134.0.66303.9.9.0.0.0.0.141.697.5j3.8.0.ehm_loc%2Chmss2%3Dfalse%2Chmnts%3D50000...0...1.1.41.serp..4.5.359.V7CTGdfZBFU
[8]:http://arduino.cc/en/main/software#toc1
[9]:http://arduino.cc/en/guide/macOSX#toc8
[10]:http://arduino.cc/en/guide/windows#toc8
[11]:http://instagram.com/p/mp6Gl7q3lU/
[12]:http://arduino.cc/en/Tutorial/Blink
[13]:http://arduino.cc/en/Tutorial/BareMinimum
[14]:http://quarkstream.wordpress.com/2009/12/09/arduino-1-writing-and-uploading-sketches/
[15]:http://arduino.cc/en/Serial/Println