【问题标题】:ARDUINO - buttonARDUINO - 按钮
【发布时间】:2016-05-10 11:26:38
【问题描述】:

我有一个带有 arduino UNO 和颜色传感器的程序,女巫向我展示了 RGB 值。 我也有一个按钮,女巫可能会开始读取一个对象(彩色纸),但是当我按下它时,程序会读取 5 次,而不是我想要的一次。 在每种情况下(对于每种颜色),我想为我想在串行监视器中显示的变量添加一个值,但是在读取(或 5 个读数:)))之后,串行监视器只显示我想要的值添加到我的变量中(例如 c=c+2;串行运动:每次按下按钮 2)。

这是我的代码:

// Define pins
const int ledpin = 13;
const int GSR1 = 12;
const int GSR0 = 11;
const int GSG1 = 10;
const int GSG0 = 9;
const int GSB1 = 8;
const int GSB0 = 7;

int redpin = A0;
int greenpin = A1;
int bluepin = A2;


const int buttonPin = 6;

// Sensor read values
int red = 0;
int green = 0;
int blue = 0;

void setup() 
{
  Serial.begin(9600);

  pinMode(buttonPin, INPUT);

  pinMode(ledpin, OUTPUT);
  pinMode(GSR1, OUTPUT);
  pinMode(GSR0, OUTPUT);
  pinMode(GSG1, OUTPUT);
  pinMode(GSG0, OUTPUT);
  pinMode(GSB1, OUTPUT);
  pinMode(GSB0, OUTPUT);

  // Turn on the LED
  digitalWrite(ledpin, HIGH);

  // Set the gain of each sensor
  digitalWrite(GSR1, LOW);
  digitalWrite(GSR0, LOW);
  digitalWrite(GSG1, LOW);
  digitalWrite(GSG0, LOW);
  digitalWrite(GSB1, LOW);
  digitalWrite(GSB0, LOW);
}

void loop()  
{  
   int buttonState;


   buttonState = digitalRead(buttonPin);



   //  Read sensors

   red = analogRead(redpin) * 10;
   green = analogRead(greenpin) * 14;
   blue = analogRead(bluepin) * 17;
   int c=0;
   int br=0;


   if (buttonState == HIGH) 
   {
    if (1200>red && red>1000 && 1950>green && green>1500 && 850>blue && blue>650)
    {
      Serial.print("yellow");
      Serial.print("\n");
      c=2;
    }

    if(c==2)
    {
      br=br+2;
      Serial.print(br);
      Serial.print("\n");
    }
  }
}

【问题讨论】:

  • 确保按钮去抖动。
  • 在你的代码中,你有int br=0; inside loop() 函数。因此,br 在每次迭代中都重置为 0。在函数外部声明 br 看看是否有帮助。

标签: button colors arduino sensors arduino-uno


【解决方案1】:

我或许可以帮助您想象正在发生的事情。第一次执行循环并按下按钮时,buttonState 设置为 HIGH。因为循环比您的 buttonPress 快,所以当您按下按钮时它会执行多次。听起来您真正想要的是打印数据的代码在每次按下按钮时执行一次。为此,您需要跟踪按钮的更改状态。你可以用一个额外的变量来做到这一点。

例如:

//Define tracker as a variable at the top of the sketch.
int tracker = 0;

然后在循环中,只在tracker和buttonState不同时执行:

buttonState = digitalRead(buttonPin);
if (tracker != buttonState)
{
    if (buttonState == HIGH){
        // put all of your print code here
    }

    //then set tracker equal to buttonState
    tracker = buttonState;
}

逻辑是:

-tracker 和 buttonState 从 0 (LOW) 开始。 - 按下按钮时,buttonState = HIGH,tracker = LOW。 buttonState 和 tracker 不相等,因此代码进入第一个 if 子句。 buttonState 为 HIGH,因此代码进入第二个 if 子句(打印数据) 然后将 tracker 设置为 buttonState,即 HIGH

-当循环再次出现时: 如果仍然按下按钮,则 buttonState 和 tracker 仍然相等,因此代码不会进入第一个子句。因此,不会打印任何内容。 如果不再按下按钮,则 buttonState 为 LOW,tracker 为 HIGH。输入第一个 if 子句。 buttonState 为 LOW,因此打印部分不会执行。 tracker 设置为 buttonState,因此它们现在都为 LOW。回到我们开始的地方。

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    a) 引脚 6 上是否有下拉电阻?

    b) 您在结果中只看到数字 2 的事实是因为您在每次调用函数 loop() 时都重置了变量 br。如果你将 br 设为静态,它只会被初始化一次,这就是你想要的。

    c) 您应该检查由 digitalRead(buttonPin) 返回的值中是否存在从 LOW 到 HIGH 的转换,而不仅仅是检查 HIGH 值。您的代码现在的方式是,如果您按住按钮足够长的时间,循环将随时运行,并且每次它都会看到 digitalRead(buttonPin) 返回 HIGH - 这解释了您的“5 次”问题。如果在更改此设置后您仍然看到许多印刷机而不是一个印刷机,那么您的问题可能会出现反跳 - 您可以在谷歌上搜索该词以获得全面的解释甚至示例代码,但简而言之,您应该丢弃太靠近的 LOW 到 HIGH 转换及时。

    【讨论】:

      【解决方案3】:

      你的按钮状态错误试试这个:

      //Your declared pins
      
      ...
      
      
      //Buttons
      
      int button1 = 7;
      
      
      
      
      
      //States for obj and Button (1)
      
      int state1 = HIGH;      // the current state of the output pin
      int reading1;           // the current reading from the input pin
      int previous1 = LOW;    // the previous reading from the input pin
      
      
      
      
      // the follow variables are long's because the time, measured in miliseconds,
      // will quickly become a bigger number than can be stored in an int.
      long time1 = 0;          // the last time the output pin was toggled
      
      long debounce1 = 200;   // the debounce time, increase if the output flickers
      
      
      ...
      
      
      void loop() {
      
        reading1 = digitalRead(button1);
      
      
        // if the input just went from LOW and HIGH and we've waited long enough
        // to ignore any noise on the circuit, toggle the output pin and remember
        // the time
        //Condition 1
        if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce1) {
          if (state1 == HIGH)
            state1 = LOW;
          else
            state1 = HIGH;
      
          time1 = millis();   
        }
      
      
      //put as many variables as you want here connected to state1
      //If you see relay somewhere or I forgot brackets please let me know
      //I just adapted an old project with relays that was similar
      if(state1 == HIGH){
           //YOUR STUFF
      
      }
      
        digitalWrite(yourvariable, state1); //In case you want to turn on something
      
      //you can find a way to nest this "if statements for the button" if you want
        previous1 = reading1;
      
      }
      

      【讨论】:

        猜你喜欢
        • 2017-01-16
        • 1970-01-01
        • 1970-01-01
        • 2011-11-19
        • 1970-01-01
        • 1970-01-01
        • 2022-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多