ocrobot:alpha:kitone:tutorial05
差别
这里会显示出您选择的修订版和当前版本之间的差别。
| 两侧同时换到之前的修订记录前一修订版后一修订版 | 前一修订版 | ||
| ocrobot:alpha:kitone:tutorial05 [2019/07/15 02:21] – [练习题代码] 董凯萍 | ocrobot:alpha:kitone:tutorial05 [2025/10/11 02:55] (当前版本) – 外部编辑 127.0.0.1 | ||
|---|---|---|---|
| 行 1: | 行 1: | ||
| + | ~~NOTOC~~ | ||
| + | ====== Digital Read Serial(读取数字口输入) ====== | ||
| + | <WRAP left round info 100%> | ||
| + | 这个例子教你怎样用控制器的USB端口,通过串口监视开关状态。 | ||
| + | </ | ||
| + | |||
| + | <WRAP left round box 100%> | ||
| + | ===== ALPHA MEGA328-U核心 ===== | ||
| + | ==== 硬件 ==== | ||
| + | * [[ocrobot: | ||
| + | * [[ocrobot: | ||
| + | * [[ocrobot: | ||
| + | ==== 搭建电路 ==== | ||
| + | |||
| + | - ALPHA MEGA328-U模块插入并行扩展版1号槽位。 | ||
| + | - ALPHA 微动开关模块插入并行扩展板2号槽位。 | ||
| + | - USB线连接计算机与ALPHA MEGA328-U。 | ||
| + | {{ocrobot: | ||
| + | ==== 代码 ==== | ||
| + | <code cpp> | ||
| + | | ||
| + | /* | ||
| + | DigitalReadSerial | ||
| + | 读取数字接口D15的输入,并将结果显示在串口监视器 | ||
| + | */ | ||
| + | |||
| + | int pushButton = 15;// 数字口15 连接一个按钮开关,给它命名. | ||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(9600); | ||
| + | pinMode(pushButton, | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | int buttonState = digitalRead(pushButton); | ||
| + | Serial.println(buttonState); | ||
| + | delay(1); | ||
| + | } | ||
| + | </ | ||
| + | </ | ||
| + | <WRAP left round tip 100%> | ||
| + | 这个例子中我们使用了两个新的函数:[[reference: | ||
| + | \\ [[reference: | ||
| + | 输出数据如果结尾不需要换行符,可以使用[[reference: | ||
| + | </ | ||
| + | |||
| + | <WRAP left round help 100%> | ||
| + | 微动开关模块中,接开关的3个引脚分别为D15,D16,D17。如何把三个按钮的状态,都通过串口输出? | ||
| + | </ | ||
| + | ==== 练习题代码 ==== | ||
| + | <code cpp> | ||
| + | /* | ||
| + | DigitalReadSerial | ||
| + | 读取数字接口D15、D16、D17的输入,并将结果显示在串口监视器 | ||
| + | */ | ||
| + | |||
| + | |||
| + | int Button1 = 15; // | ||
| + | int Button2 = 16; | ||
| + | int Button3 = 17; | ||
| + | |||
| + | int buttonState1=LOW; | ||
| + | int buttonState2=LOW; | ||
| + | int buttonState3=LOW; | ||
| + | |||
| + | void setup() { | ||
| + | Serial.begin(9600);// | ||
| + | | ||
| + | pinMode(Button1, | ||
| + | pinMode(Button2, | ||
| + | pinMode(Button3, | ||
| + | } | ||
| + | |||
| + | void loop() { | ||
| + | if (digitalRead(Button1)!=buttonState1) | ||
| + | { | ||
| + | | ||
| + | { Serial.println(Button1); | ||
| + | } | ||
| + | } | ||
| + | if (digitalRead(Button2)!=buttonState2) | ||
| + | { buttonState2 = digitalRead(Button2); | ||
| + | | ||
| + | { Serial.println(Button2); | ||
| + | } | ||
| + | } | ||
| + | if (digitalRead(Button3)!=buttonState3) | ||
| + | { buttonState3 = digitalRead(Button3); | ||
| + | | ||
| + | { Serial.println(Button3); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | [[ocrobot: | ||
