-
Notifications
You must be signed in to change notification settings - Fork 1
/
输入输出练习.ino
122 lines (71 loc) · 1.3 KB
/
输入输出练习.ino
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
String comdata = "";
void setup()
{
Serial.begin(9600);
for(int i = 2; i <= 13; i++) pinMode(i, OUTPUT);
}
void loop()
{
while (Serial.available() > 0)
{
comdata += int(Serial.read()) - '0';
delay(2);
}
if(comdata.length() > 0)
{
for(int i = 0; i < comdata.length(); i++)
{
if(comdata[i]=='0'||comdata[i]=='1')
{
digitalWrite(i + 2, comdata[i] - '0');
Serial.print("Pin ");
Serial.print(i + 2);
Serial.print(" is ");
Serial.println(comdata[i]);
//System.out.println(messageID);
}
}
comdata = "";
}
}
//接收代码:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3,2);
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
pinMode(9,OUTPUT);
}
void loop() {
while(mySerial.available() > 0){
char c = mySerial.read();
Serial.write(c);
}
}
//发送代码:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(3,2);
bool a=0;
int b=0;
void setup() {
mySerial.begin (9600);
Serial.begin(9600);
pinMode(9,OUTPUT);
}
void loop() {
if(a==0)
{
digitalWrite(9,HIGH);
Serial.println ("pin9 is HIGH");
mySerial.println("pin9 is HIGH");
}
else
{
digitalWrite(9,LOW);
Serial.println ("pin9 is LOW");
mySerial.println("pin9 is LOW");
}
b++;
a=b%2;
delay(1000);
}