|
| 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 | + |
0 commit comments