【问题标题】:Arduino: When pressing a button return only one outputArduino:按下按钮时仅返回一个输出
【发布时间】:2015-06-07 17:00:02
【问题描述】:

我正在尝试用我的 Arduino 制作一架水果钢琴,而现在我只是在纸上使用颜料。仍然当我按下按钮时,我得到了多行输出。我该如何解决。 这是正在发生的事情的视频(抱歉质量问题):https://www.youtube.com/watch?v=zr2mQMRO6IY

代码:

#include <CapacitiveSensor.h>


/* DIY Piano

   - DIY Piano with Arduino. This is a very simple project.
   - ONLY needed some very cheap materials,
     e.g. pencil, A4 paper, paper clip, speaker, male-to-male jumper, breadboard and Arduino Uno board.
   - Of course the Arduino Uno board is not cheap, but it is reusable for other projects too. So it is worth to own one.

   - This project using the concept of capasitive sensor, so no switch/button is required for it.
   - Here is the link of capacitive sensor. For better understanding, please visit the page.
     http://playground.arduino.cc//Main/CapacitiveSensor?from=Main.CapSense

   - To watch the Demo of This Project:
     http://www.youtube.com/watch?v=X4QNT5hOHLs&list=TLlm-tAV1gEzDF4VV39VdjCSLNlfe0tTpU


   Created by:
   Oh Hai Seng || Junny Oh    07 November 2013
*/



// Import the CapacitiveSensor Library.
#include <CapacitiveSensor.h>


// Name the pin as led. 
//#define speaker 11


// Set the Send Pin & Receive Pin
CapacitiveSensor   cs_2_4 = CapacitiveSensor(2,4);        // 10M resistor between pins 4 & 6, pin 6 is sensor pin, add a wire and or foil


void setup()                    
{
  cs_2_4.set_CS_AutocaL_Millis(0xFFFFFFFF);     // turn off autocalibrate on channel 1 - just as an example

  // Arduino start communicate with computer.
  Serial.begin(9600);
}

void loop()                    
{
  // Set a timer.
  long start = millis();

  // Set the sensitivity of the sensors.
  long total2 =  cs_2_4.capacitiveSensor(60);


  // When hand is touched the sensor, the speaker will produce a tone.
  // I set a threshold for it, so that the sensor won't be too sensitive.
  if (total2 > 590){ i++; Serial.println( i + " Touched!\n");}
  delay(0);                             // arbitrary delay to limit data to serial port 
}

【问题讨论】:

    标签: button arduino hardware arduino-uno arduino-ide


    【解决方案1】:

    Arduino 使用循环,每次进入循环时都会执行整个代码,这就是您在屏幕上获得乘法输出的原因。

    你需要做的是在按下按钮时进行标记

    buttonOnePressed = true; 
    

    直到你进入按下按钮但变量 buttonOnePressed 为真的情况,你什么都不做,这将避免多个输出。 但是请确保在库检测到手指被移除时将变量设置为零,否则您将无法再次按下它。

    【讨论】:

      猜你喜欢
      • 2020-08-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-11
      • 1970-01-01
      相关资源
      最近更新 更多