【问题标题】:How to read pot values to arduino serial monitor only when you move the pot?如何仅在移动锅时将锅值读取到arduino串行监视器?
【发布时间】:2014-04-07 12:39:16
【问题描述】:

此代码用于读取电位器并将值打印到 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
 }

【问题讨论】:

  • 你是不是只想在锅移动的时候写到serrial?
  • 是的,我只想检查我的显示器中的值

标签: arduino


【解决方案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
 }

您需要使用一个变量来保存旧值并将其与新读数进行比较。 如果它们不同,则打印新值并更新旧值

【讨论】:

    猜你喜欢
    • 2017-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-12
    • 1970-01-01
    相关资源
    最近更新 更多