Arduino学习(一)

Arduino IDE自然是从官网下载的。
pinMode(pin, mode):设置引脚。第一个参数表示设置的引脚,第二个参数表示将要把引脚设置成的模式。
digitalWrite(pin, value):输出信号。第一个参数为引脚,第二个参数为输出模式。HIGH即为高电平,LOW即为低电平。

setup():该函数中的代码只会运行一次。
loop():该函数中的代码会不断重复运行。

数字I/O的使用:
1.数字信号:
数字信号是以0、1表示的不连续信号。高电平为数字信号1,低电平为数字信号0。Arduino上每一个带有数字编号的引脚都是数字引脚,包括写有“A”编号的模拟输入引脚。使用这些引脚可以完成输入/输出数字
的功能。
使用引脚前,先用pinMode()将引脚设置为某种模式:
INPUT 输入模式;OUTPUT 输出模式;INPUT_PULLUP 输入上拉模式
配置模式之后,用digitalWrite函数使该引脚输出高电平或低电平。
除了输出,数字引脚还可以用digitalRead()读取外部输入的数字信号,其形式为:digitalRead(pin);
其中参数为引脚编号。
当Arduino以5V供电时,会将范围为-0.5~1.5V的输入电压作为低电平识别,范围在3~5.5的输入电压作为高电平识别。所以即使输入电压不太准,Arduino也可以正常识别。
输出的低电平是0,高电平是当前Arduino板的工作电压。
Arduino核心库中,OUTPUT被定义为1,INPUT被定义为0,HIGH是1,LOW是0,可以使用数字代替这些定义。

delay(time):毫秒延时函数。time即为毫秒数。

2.
流水灯程序。

void setup() {
// put your setup code here, to run once:
for (int i=2; i<8; i++) pinMode(i,OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
for (int i=2; i<8; i++)
{
digitalWrite(i,1);
delay(1000);
digitalWrite(i,0);
}
for (int i=7; i>1; i--)
{
digitalWrite(i,1);
delay(1000);
digitalWrite(i,0);
}
}

3.
按键控制LED程序。
连接大电阻是为了确保2号引脚一直是低电平。这个电阻被称为下拉电阻。

/*
Button

Turns on and off a light emitting diode(LED) connected to digital pin 13,
when pressing a pushbutton attached to pin 2.

The circuit:
- LED attached from pin 13 to ground
- pushbutton attached to pin 2 from +5V
- 10K resistor attached to pin 2 from ground

- Note: on most Arduinos there is already an LED on the board
attached to pin 13.

created 2005
by DojoDave <http://www.0j0.org>
modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);

// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}

倘若去掉下拉电阻,将按键一端接2号引脚, 一端接GND,将
pinMode(buttonPin,INPUT); 改为 pinMode(buttonPin,INPUT_PULLUP); 就可以使用引脚内部的上拉电阻。这个电阻也很大,一般为20~50kΩ。它也可以稳定电平,并可以稳定在高电平。


如下程序可实现按一下按键灯开,再按一下灯灭。

int buttonPin=2;
int ledPin=13;
boolean ledstate=false;//初始时候灯是灭的

void setup() {
// put your setup code here, to run once:
pinMode(ledPin,OUTPUT);
pinMode(buttonPin,INPUT_PULLUP);

}

void loop() {
// put your main code here, to run repeatedly:
while (digitalRead(buttonPin)==HIGH)
{
if (ledstate==true)
{
digitalWrite(ledPin,LOW);
ledstate=!ledstate;
}
else
{
digitalWrite(ledPin,HIGH);
ledstate=!ledstate;
}
}
delay(500);
}

人体感应灯
使用继电器模块与数字传感器。
1.继电器模块。使用电磁继电器,加压会使继电器中产生电流,继而产生磁力,将开关吸向另一侧。
2.人体热释电红外传感器。无人在其检测范围内运动时,模块保持输出低电平,有人再其检测范围内运动时,模块输出一个高电平脉冲信号。
具体代码思路简单,只是用到了新函数:
Serial.begin(int num); 打开串口,参数是传输速率,有几个值可供选择,通常使用9600。
Serial.println(format string); 输出信息,表现形式和printf差不多。

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/Shymuel/p/9437765.html