| 1 | [wiki:wade/embedded/arduino 回到 Arduino] |
| 2 | |
| 3 | = Source code = |
| 4 | |
| 5 | {{{ |
| 6 | #!c |
| 7 | |
| 8 | /* |
| 9 | * 使用 1 個 PIN 判斷 16 個 KEY 的 INPUT |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #define key_pin 0 // define key sensor pin |
| 14 | #define key_val_size 11 // define key value's size |
| 15 | int key_val[key_val_size] = {0}; // store key sensor value |
| 16 | byte index = 0; // key sensor array's index |
| 17 | byte button_up_count = 0; |
| 18 | boolean is_button_down = 0; |
| 19 | |
| 20 | void setup() |
| 21 | { |
| 22 | Serial.begin(9600); |
| 23 | } |
| 24 | |
| 25 | void loop() |
| 26 | { |
| 27 | if(analogRead(key_pin) > 215 && analogRead(key_pin) < 610 && index < key_val_size) |
| 28 | { |
| 29 | is_button_down = 1; |
| 30 | key_val[index++] = analogRead(key_pin); |
| 31 | } |
| 32 | else if(analogRead(key_pin) == 0) |
| 33 | { |
| 34 | button_up_count++; |
| 35 | if (button_up_count > 5 && is_button_down == 1) |
| 36 | { |
| 37 | index = 0; |
| 38 | button_up_count = 0; |
| 39 | is_button_down = 0; |
| 40 | switch ( detectKey() ) |
| 41 | { |
| 42 | case 1: |
| 43 | Serial.println("key 1"); |
| 44 | break; |
| 45 | case 2: |
| 46 | Serial.println("key 2"); |
| 47 | break; |
| 48 | case 3: |
| 49 | Serial.println("key 3"); |
| 50 | break; |
| 51 | case 4: |
| 52 | Serial.println("key 4"); |
| 53 | break; |
| 54 | case 5: |
| 55 | Serial.println("key 5"); |
| 56 | break; |
| 57 | case 6: |
| 58 | Serial.println("key 6"); |
| 59 | break; |
| 60 | case 7: |
| 61 | Serial.println("key 7"); |
| 62 | break; |
| 63 | case 8: |
| 64 | Serial.println("key 8"); |
| 65 | break; |
| 66 | case 9: |
| 67 | Serial.println("key 9"); |
| 68 | break; |
| 69 | case 10: |
| 70 | Serial.println("key 10"); |
| 71 | break; |
| 72 | case 11: |
| 73 | Serial.println("key 11"); |
| 74 | break; |
| 75 | case 12: |
| 76 | Serial.println("key 12"); |
| 77 | break; |
| 78 | case 13: |
| 79 | Serial.println("key 13"); |
| 80 | break; |
| 81 | case 14: |
| 82 | Serial.println("key 14"); |
| 83 | break; |
| 84 | case 15: |
| 85 | Serial.println("key 15"); |
| 86 | break; |
| 87 | case 16: |
| 88 | Serial.println("key 16"); |
| 89 | break; |
| 90 | default: |
| 91 | break; |
| 92 | } // end of switch |
| 93 | } // end of if |
| 94 | } |
| 95 | |
| 96 | } |
| 97 | |
| 98 | byte detectKey() |
| 99 | { |
| 100 | for (byte i = 0; i < key_val_size; i++) |
| 101 | { |
| 102 | for (byte j = i+1; j < key_val_size; j++) |
| 103 | { |
| 104 | if (key_val[i] > key_val[j]) |
| 105 | { |
| 106 | int temp = key_val[i]; |
| 107 | key_val[i] = key_val[j]; |
| 108 | key_val[j] = temp; |
| 109 | } |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | int average_val = key_val[key_val_size - 2]; |
| 114 | for (byte i = 0; i < key_val_size; i++) |
| 115 | key_val[i] = 0; |
| 116 | if (average_val < 216) |
| 117 | return 0; |
| 118 | else if (average_val < 234) |
| 119 | return 13; |
| 120 | else if (average_val < 247) |
| 121 | return 14; |
| 122 | else if (average_val < 257) |
| 123 | return 15; |
| 124 | else if (average_val < 263) |
| 125 | return 16; |
| 126 | else if (average_val < 275) |
| 127 | return 9; |
| 128 | else if (average_val < 293) |
| 129 | return 10; |
| 130 | else if (average_val < 307) |
| 131 | return 11; |
| 132 | else if (average_val < 326) |
| 133 | return 12; |
| 134 | else if (average_val < 356) |
| 135 | return 5; |
| 136 | else if (average_val < 387) |
| 137 | return 6; |
| 138 | else if (average_val < 412) |
| 139 | return 7; |
| 140 | else if (average_val < 434) |
| 141 | return 8; |
| 142 | else if (average_val < 475) |
| 143 | return 1; |
| 144 | else if (average_val < 530) |
| 145 | return 2; |
| 146 | else if (average_val < 578) |
| 147 | return 3; |
| 148 | else if (average_val < 611) |
| 149 | return 4; |
| 150 | } |
| 151 | |
| 152 | }}} |