- /*
- 翻译:tom
- 时间:2012年11月18日
- IDE版本号:1.01
- 发表地址:www.geek-workshop.com
- 翻译说明: 根据arduino自带的范例进行翻译,可能根据实际的需要略作修改
-
- */
- /*
- Input Pullup Serial
-
- 这个范例演示使用端口过程(pinMode).它读取一个在2号数字端口的数字信号输入和打印结果至串口监视器.
-
- 电路描述:
- *点触式按纽连接2号数字端口至GND.
- *板载LED在数字13号端口
-
- 与pinMode(input)不同之处,这里需要一个上拉电阻.
- 这个下拉电阻20K接到5V. 这样组合的原因是由于读取开关打开时是高电平,开关关闭时是低电平.
- created 14 March 2012
- by Scott Fitzgerald
-
- http://www.arduino.cc/en/Tutorial/InputPullupSerial
-
- 这个范例代码在公共范围(不受版权制约)
- */
- /*
- 电路原理图
-
- */
- void setup(){
- //启动串口连接
- Serial.begin(9600);
- //configure pin2 as an input and enable the internal pull-up resistor
- //配置2号数字端口作为输入模式和使它能接通上拉电阻
- pinMode(2, INPUT_PULLUP);
- pinMode(13, OUTPUT);
- }
- void loop(){
- //读取pushbutton的值到一个变量中(sensorVal)
- int sensorVal = digitalRead(2);
- //打印pushbutton值(串口监视器中)
- Serial.println(sensorVal);
- //记住上拉它的意思是按纽的逻辑是反向的.
- //当它是开的时候,它转向高电平
- //当按下时,它转向低电平.
- //打开13号端口的灯,当按纽按下时,
- //没有按下时则是关
- if (sensorVal == HIGH) {
- digitalWrite(13, LOW);
- }
- else {
- digitalWrite(13, HIGH);
- }
- }
复制代码
|