//读分量测试 ---------------- int keyboardPin = 0; //模拟口0 int keyboardValue = 0; // 取得键AD模拟分量 void setup() { //通信波特率 Serial.begin(9600); } void loop() { keyboardValue = analogRead(keyboardPin); // read the pot value Serial.println(keyboardValue); // 串口打印 delay(1000); } --------------------------------------------------------- int keypressed = 0; int keyboardPin = 0; int keyboardValue = 0; void setup(){ Serial.begin(9600); } void loop(){ keyboardValue = analogRead(keyboardPin); //AD分量范围2的10次方 while (keyboardValue < 25){ //监听模拟口 keyboardValue = analogRead(keyboardPin); delay(50); readkeyboard(); } void readkeyboard(){ keyboardValue = analogRead(keyboardPin); if (keyboardValue <25){keypressed = 0;} if ((keyboardValue >25) && (keyboardValue < 67)){keypressed = 1;} if ((keyboardValue >67) && (keyboardValue < 108)){keypressed = 2;} if ((keyboardValue >108) && (keyboardValue < 162)){keypressed = 3;} if ((keyboardValue >162) && (keyboardValue < 253)){keypressed = 4;} if ((keyboardValue >253) && (keyboardValue < 361)){keypressed = 5;} if ((keyboardValue >361) && (keyboardValue < 479)){keypressed = 6;} if ((keyboardValue >479) && (keyboardValue < 619)){keypressed = 7;} if ((keyboardValue >619) && (keyboardValue < 765)){keypressed = 8;} if ((keyboardValue >765) && (keyboardValue < 819)){keypressed = 9;} if ((keyboardValue >819) && (keyboardValue < 889)){keypressed = 0;} if ((keyboardValue >889) && (keyboardValue < 938)){keypressed = 0;} if (keyboardValue >938){keypressed = 0;} while (keyboardValue > 25) { delay (100); keyboardValue = analogRead(keyboardPin); } Serial.println(keypressed); delay(1000); }