2015-04-02 2 views
0

Я пытаюсь сделать фруктовый пианино с моим Arduino, и сейчас я просто использую краску на бумаге. Тем не менее, когда я нажимаю кнопку, я получаю несколько выходных строк. Как я могу это исправить. Вот видео того, что происходит (извините за качество): https://www.youtube.com/watch?v=zr2mQMRO6IYArduino: При нажатии кнопки возвращает только один выход

Код:

#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 
} 

ответ

0

Arduino работать с петлей, и каждый раз, когда он идет в петлю весь код выполняется, и это поэтому вы получаете многократный вывод на экран.

Что вам нужно сделать, это флаг, когда кнопка нажата, как

buttonOnePressed = true; 

и до, если вы получите в состояние нажатой кнопки, но переменная buttonOnePressed верно, вы просто ничего не делать, и это позволит избежать нескольких выходов. Но убедитесь, что вы установили переменную в ноль, когда библиотека обнаруживает удаление пальца, иначе вы не сможете повторно нажать ее снова.

Смежные вопросы