极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 27175|回复: 1

arduino入门实践模块测试

[复制链接]
发表于 2020-12-15 21:22:37 | 显示全部楼层 |阅读模式
本帖最后由 vincy 于 2020-12-15 21:41 编辑

水银开关

材料:
arduino uno
水银开关
杜邦线
数据线
LED灯

水银开关工作原理:
因为重力的关系,水银水珠会向容器中较低的地方流去,如果同时接触到两个电极的话,开关便会将电路闭合,开启开关。


代码:

  1. const int buttonPin = 2;     // the number of the pushbutton pin
  2. const int ledPin =  3;      // the number of the LED pin

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

  5. void setup() {
  6.   // initialize the LED pin as an output:
  7.   pinMode(ledPin, OUTPUT);      
  8.   // initialize the pushbutton pin as an input:
  9.   pinMode(buttonPin, INPUT);     
  10. }

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

  14.   // check if the pushbutton is pressed.
  15.   // if it is, the buttonState is HIGH:
  16.   if (buttonState == HIGH) {     
  17.     // turn LED on:   
  18.     digitalWrite(ledPin, HIGH);  
  19.   }
  20.   else {
  21.     // turn LED off:
  22.     digitalWrite(ledPin, LOW);
  23.   }
  24. }
复制代码
回复

使用道具 举报

 楼主| 发表于 2020-12-15 21:43:58 | 显示全部楼层
倾斜开关

材料:
arduino uno
倾斜开关
led灯
杜邦线
数据线

倾斜开关原理:

垂直悬挂的倾bai斜开关探头在受到外du力作用且偏离垂直位zhi置17度以上时,倾dao斜开关内部的金zhuan属球触点动作,常shu闭触点断开。当外力撤消后,倾斜开关回复到垂直状态,金属球触点复又闭合。

代码:
  1. const int buttonPin = 2;     // the number of the pushbutton pin
  2. const int ledPin =  3;      // the number of the LED pin

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

  5. void setup() {
  6.   // initialize the LED pin as an output:
  7.   pinMode(ledPin, OUTPUT);      
  8.   // initialize the pushbutton pin as an input:
  9.   pinMode(buttonPin, INPUT);     
  10. }

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

  14.   // check if the pushbutton is pressed.
  15.   // if it is, the buttonState is HIGH:
  16.   if (buttonState == HIGH) {     
  17.     // turn LED on:   
  18.     digitalWrite(ledPin, HIGH);  
  19.   }
  20.   else {
  21.     // turn LED off:
  22.     digitalWrite(ledPin, LOW);
  23.   }
  24. }
复制代码

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-4-16 18:53 , Processed in 0.077782 second(s), 17 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表