此代码用于读取电位器并将值打印到arduino串行监视器 但即使你不移动底池,你也会获得价值。
只有在移动电位计时,我需要更改代码才能获取值?
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}
答案 0 :(得分:1)
int oldValue = 0;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
if (sensorValue != oldValue){
Serial.println(sensorValue);
oldValue = sensorValue;
}
delay(1); // delay in between reads for stability
}
您需要使用变量来保存旧值并将其与新读数进行比较。 如果它们不同则打印新值并更新旧值