arduino入门实践模块测试
本帖最后由 vincy 于 2020-12-15 21:41 编辑水银开关
材料:
arduino uno
水银开关
杜邦线
数据线
LED灯
水银开关工作原理:
因为重力的关系,水银水珠会向容器中较低的地方流去,如果同时接触到两个电极的话,开关便会将电路闭合,开启开关。
代码:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin =3; // 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);
}
} 倾斜开关
材料:
arduino uno
倾斜开关
led灯
杜邦线
数据线
倾斜开关原理:
垂直悬挂的倾bai斜开关探头在受到外du力作用且偏离垂直位zhi置17度以上时,倾dao斜开关内部的金zhuan属球触点动作,常shu闭触点断开。当外力撤消后,倾斜开关回复到垂直状态,金属球触点复又闭合。
代码:const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin =3; // 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);
}
}
页:
[1]