本帖最后由 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);
- }
- }
复制代码 |