|
1 |
| -// variable to store the data from the serial port |
2 |
| -int cmd = 0; |
3 |
| - |
4 |
| -// command arguments |
5 |
| -int cmd_arg[2]; |
6 |
| - |
7 |
| - |
8 |
| -void setup() { |
9 |
| - // connect to the serial port |
10 |
| - Serial.begin(115200); |
11 |
| - // confirm ready state |
12 |
| - |
13 |
| - while(Serial.available()<1) |
14 |
| - { |
15 |
| - // get number of output pins and convert to int |
16 |
| - cmd = int(readData()) - 48; |
17 |
| - for(int i=0; i<cmd; i++) |
18 |
| - { |
19 |
| - cmd_arg[0] = int(readData()) - 48; |
20 |
| - pinMode(cmd_arg[0], OUTPUT); |
21 |
| - } |
22 |
| - break; |
23 |
| - } |
24 |
| -} |
25 |
| - |
26 |
| -void loop() |
27 |
| -{ |
28 |
| - |
29 |
| - askCmd(); |
30 |
| - |
31 |
| - if(Serial.available()>0) |
32 |
| - { |
33 |
| - cmd = int(Serial.read()) - 48; |
34 |
| - |
35 |
| - if(cmd==0) //set digital low |
36 |
| - { |
37 |
| - cmd_arg[0] = int(readData()) - 48; |
38 |
| - digitalWrite(cmd_arg[0],LOW); |
39 |
| - } |
40 |
| - |
41 |
| - if(cmd==1) //set digital high |
42 |
| - { |
43 |
| - cmd_arg[0] = int(readData()) - 48; |
44 |
| - digitalWrite(cmd_arg[0],HIGH); |
45 |
| - } |
46 |
| - |
47 |
| - if(cmd==2) //get digital value |
48 |
| - { |
49 |
| - cmd_arg[0] = int(readData()) - 48; |
50 |
| - cmd_arg[0] = digitalRead(cmd_arg[0]); |
51 |
| - Serial.println(cmd_arg[0]); |
52 |
| - } |
53 |
| - |
54 |
| - if(cmd==3) // set analog value |
55 |
| - { |
56 |
| - Serial.println("I'm in the right place"); |
57 |
| - cmd_arg[0] = int(readData()) - 48; |
58 |
| - cmd_arg[1] = readHexValue(); |
59 |
| - analogWrite(cmd_arg[0],cmd_arg[1]); |
60 |
| - } |
61 |
| - |
62 |
| - if(cmd==4) //read analog value |
63 |
| - { |
64 |
| - cmd_arg[0] = int(readData()) - 48; |
65 |
| - cmd_arg[0] = analogRead(cmd_arg[0]); |
66 |
| - Serial.println(cmd_arg[0]); |
67 |
| - } |
68 |
| - } |
69 |
| -} |
70 |
| - |
71 |
| -char readData() |
72 |
| -{ |
73 |
| - askData(); |
74 |
| - |
75 |
| - while(1) |
76 |
| - { |
77 |
| - if(Serial.available()>0) |
78 |
| - { |
79 |
| - return Serial.read(); |
80 |
| - } |
81 |
| - } |
82 |
| -} |
83 |
| - |
84 |
| - |
85 |
| -//read hex value from serial and convert to integer |
86 |
| -int readHexValue() |
87 |
| -{ |
88 |
| - int strval[2]; |
89 |
| - int converted_str; |
90 |
| - |
91 |
| - while(1) |
92 |
| - { |
93 |
| - if(Serial.available()>0) |
94 |
| - { |
95 |
| - strval[0] = convert_hex_to_int(Serial.read()); |
96 |
| - break; |
97 |
| - } |
98 |
| - } |
99 |
| - |
100 |
| - askData(); |
101 |
| - |
102 |
| - while(1) |
103 |
| - { |
104 |
| - if(Serial.available()>0) |
105 |
| - { |
106 |
| - strval[1] = convert_hex_to_int(Serial.read()); |
107 |
| - break; |
108 |
| - } |
109 |
| - } |
110 |
| - |
111 |
| - converted_str = (strval[0]*16) + strval[1]; |
112 |
| - return converted_str; |
113 |
| -} |
114 |
| - |
115 |
| -int convert_hex_to_int(char c) |
116 |
| -{ |
117 |
| - return (c <= '9') ? c-'0' : c-'a'+10; |
118 |
| -} |
119 |
| - |
120 |
| -void askData() |
121 |
| -{ |
122 |
| - Serial.println("?"); |
123 |
| -} |
124 |
| - |
125 |
| -void askCmd() |
126 |
| -{ |
127 |
| - askData(); |
128 |
| - while(Serial.available()<=0) |
129 |
| - {} |
130 |
| -} |
131 |
| - |
| 1 | +// variable to store the data from the serial port |
| 2 | +int cmd = 0; |
| 3 | + |
| 4 | +// command arguments |
| 5 | +int cmd_arg[2]; |
| 6 | + |
| 7 | +int serialStatus = 0; |
| 8 | + |
| 9 | +void setup() { |
| 10 | + // connect to the serial port |
| 11 | + Serial.begin(115200); |
| 12 | + setupPins(); |
| 13 | + serialStatus = 1; |
| 14 | +} |
| 15 | + |
| 16 | +void loop() |
| 17 | +{ |
| 18 | + |
| 19 | + if(serialStatus==0) |
| 20 | + { |
| 21 | + Serial.flush(); |
| 22 | + setupPins(); |
| 23 | + } |
| 24 | + askCmd(); |
| 25 | + |
| 26 | + { |
| 27 | + if(Serial.available()>0) |
| 28 | + { |
| 29 | + cmd = int(Serial.read()) - 48; |
| 30 | + |
| 31 | + if(cmd==0) //set digital low |
| 32 | + { |
| 33 | + cmd_arg[0] = int(readData()) - 48; |
| 34 | + digitalWrite(cmd_arg[0],LOW); |
| 35 | + } |
| 36 | + |
| 37 | + if(cmd==1) //set digital high |
| 38 | + { |
| 39 | + cmd_arg[0] = int(readData()) - 48; |
| 40 | + digitalWrite(cmd_arg[0],HIGH); |
| 41 | + } |
| 42 | + |
| 43 | + if(cmd==2) //get digital value |
| 44 | + { |
| 45 | + cmd_arg[0] = int(readData()) - 48; |
| 46 | + cmd_arg[0] = digitalRead(cmd_arg[0]); |
| 47 | + Serial.println(cmd_arg[0]); |
| 48 | + } |
| 49 | + |
| 50 | + if(cmd==3) // set analog value |
| 51 | + { |
| 52 | + Serial.println("I'm in the right place"); |
| 53 | + cmd_arg[0] = int(readData()) - 48; |
| 54 | + cmd_arg[1] = readHexValue(); |
| 55 | + analogWrite(cmd_arg[0],cmd_arg[1]); |
| 56 | + } |
| 57 | + |
| 58 | + if(cmd==4) //read analog value |
| 59 | + { |
| 60 | + cmd_arg[0] = int(readData()) - 48; |
| 61 | + cmd_arg[0] = analogRead(cmd_arg[0]); |
| 62 | + Serial.println(cmd_arg[0]); |
| 63 | + } |
| 64 | + |
| 65 | + if(cmd==5) |
| 66 | + { |
| 67 | + serialStatus = 0; |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +char readData() |
| 74 | +{ |
| 75 | + askData(); |
| 76 | + |
| 77 | + while(1) |
| 78 | + { |
| 79 | + if(Serial.available()>0) |
| 80 | + { |
| 81 | + return Serial.read(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | + |
| 87 | +//read hex value from serial and convert to integer |
| 88 | +int readHexValue() |
| 89 | +{ |
| 90 | + int strval[2]; |
| 91 | + int converted_str; |
| 92 | + |
| 93 | + while(1) |
| 94 | + { |
| 95 | + if(Serial.available()>0) |
| 96 | + { |
| 97 | + strval[0] = convert_hex_to_int(Serial.read()); |
| 98 | + break; |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + askData(); |
| 103 | + |
| 104 | + while(1) |
| 105 | + { |
| 106 | + if(Serial.available()>0) |
| 107 | + { |
| 108 | + strval[1] = convert_hex_to_int(Serial.read()); |
| 109 | + break; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + converted_str = (strval[0]*16) + strval[1]; |
| 114 | + return converted_str; |
| 115 | +} |
| 116 | + |
| 117 | + |
| 118 | +int convert_hex_to_int(char c) |
| 119 | +{ |
| 120 | + return (c <= '9') ? c-'0' : c-'a'+10; |
| 121 | +} |
| 122 | + |
| 123 | + |
| 124 | +void askData() |
| 125 | +{ |
| 126 | + Serial.println("?"); |
| 127 | +} |
| 128 | + |
| 129 | + |
| 130 | +void askCmd() |
| 131 | +{ |
| 132 | + askData(); |
| 133 | + while(Serial.available()<=0) |
| 134 | + {} |
| 135 | +} |
| 136 | + |
| 137 | + |
| 138 | +void setupPins() |
| 139 | +{ |
| 140 | + while(Serial.available()<1) |
| 141 | + { |
| 142 | + // get number of output pins and convert to int |
| 143 | + cmd = int(readData()) - 48; |
| 144 | + for(int i=0; i<cmd; i++) |
| 145 | + { |
| 146 | + cmd_arg[0] = int(readData()) - 48; |
| 147 | + pinMode(cmd_arg[0], OUTPUT); |
| 148 | + } |
| 149 | + break; |
| 150 | + } |
| 151 | +} |
0 commit comments