An Arduino Sample Project Based on LED and Button

Jan. 25, 2023

本项目采用的LED灯为:Grove - LED Socket Kit - Seeed Wiki,按钮为Grove - Button - Seeed Wiki,实现的功能非常简单:按钮按下时灯亮,松开后灯灭。本文主要是简单记录一下MATLAB对于Arduino数字端口的读写操作。

Arduino IDE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const int buttonPin = 2;     // the number of the pushbutton pin, "D2" port
const int ledPin =  3;      // the number of the LED pin, "D3" port

// 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);
    }
}

MATLAB实现:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
clc, clear, close all

buttonPin = "D2";     
ledPin =  "D3";

a = arduino;

while true
    buttonState = readDigitalPin(a, buttonPin);
    if buttonState == true
        writeDigitalPin(a, ledPin, true);
    else
        writeDigitalPin(a, ledPin, false);
    end
end

其中用到了readDigitalPinwriteDigitalPin函数。

另外需要注意一点,在MATLAB中,并不存在关键字HIGHLOW,需要使用truefalse代替。