| 
 | 
 
这节主要是设计一个APP,连接蓝牙模块,通过按钮控制继电器的打开和关闭。 
 
一、硬件部分 
![]()  
二、板子程序 
串口接收字符,并输出控制继电器 
 
 
- // 引脚定义
 
 - const int ledPin1 =  5;// the number of the LED pin
 
 - const int ledPin2 =  6;
 
 - const int ledPin3 =  3;
 
 - const int bluePin =  6;// the number of the LED pin
 
 - const int greenPin =  5;
 
 - const int redPin =  3;
 
 - const int beepPin =  15;
 
 - const int relayPin =  14;
 
 - const int keyPin1 =  2;
 
 - const int keyPin2 =  4;
 
 - const int keyPin3 =  7;
 
 - const int bluetoothPin = 13;
 
  
- // 变量定义
 
  
- int inByte=0;   //接收参数
 
  
- #define TRUE 1
 
 - #define FALSE 0
 
 - void setup() 
 
 - {
 
 -   // 配置输出引脚
 
 -   pinMode(ledPin1, OUTPUT);
 
 -   pinMode(ledPin2, OUTPUT);
 
 -   pinMode(ledPin3, OUTPUT);
 
 -   pinMode(beepPin, OUTPUT);
 
 -   pinMode(relayPin, OUTPUT);
 
 -   // 配置输入引脚
 
 -   pinMode(keyPin1, INPUT);
 
 -   pinMode(keyPin2, INPUT);
 
 -   pinMode(keyPin3, INPUT);
 
 -   pinMode(bluetoothPin, INPUT);
 
 -   // 配置串口
 
 -   Serial.begin(9600);
 
  
- }
 
 - void loop() { 
 
 -   if(Serial.available()) {
 
 -   inByte = Serial.read();
 
 -   
 
 -     if(inByte == 'H'){ digitalWrite(relayPin, HIGH);}
 
 -     if(inByte == 'L'){ digitalWrite(relayPin, LOW);} 
 
 -     
 
 -   } }
 
 
  复制代码 三、app inventor 
3.1界面 
 
 
 
 
3.2程序逻辑块 
 
 
 |   
 
评分
- 
查看全部评分
 
 
 
 
 
 |