2016-05-13 2 views
1

ボタンを押したままにしておくと、ある時間のワンショットパルスを作成しようとしています。 reading2について私はいくつかの他の変数、後でその名前を変更する必要があります)プッシュボタンの代わりに1つのショットを使用

const int buttonPin = 10; // the number of the pushbutton pin 
const int ledPin1 = 11;  // the number of the LED pin 
const int ledPin2 = 12; 

// Variables will change: 
int ledState1 = LOW;   // the current state of the output pin 
int ledState2 = LOW; 
int buttonState;    // the current reading from the input pin 
int lastButtonState = HIGH; // the previous reading from the input pin 

// the following variables are long's because the time, measured in miliseconds, 
// will quickly become a bigger number than can be stored in an int. 
long lastDebounceTime1 = 0; // the last time the output pin was toggled 
long debounceDelay1 = 0; // the debounce time; increase if the output flickers 
long lastDebounceTime2 = 0; // the last time the output pin was toggled 
long debounceDelay2 = 300; 

void setup() { 
pinMode(buttonPin, INPUT); 
pinMode(ledPin1, OUTPUT); 
pinMode(ledPin2, OUTPUT); 

// set initial LED state 
digitalWrite(ledPin1, ledState1); 
digitalWrite(ledPin2, ledState2); 
} 

void loop() { 

int reading2 = digitalRead(buttonPin); 

if (reading2 == HIGH){ 
ledState1 = !ledState1; 
digitalWrite(ledPin1, ledState1); 
} 
else if(ledState2 == LOW){ 
ledState1 = LOW; 
digitalWrite(ledPin1, ledState1); 
} 
// check to see if you just pressed the button 
// (i.e. the input went from LOW to HIGH), and you've waited 
// long enough since the last press to ignore any noise: 

// If the switch changed, due to noise or pressing: 
if (reading2 != lastButtonState) { 
// reset the debouncing timer 
lastDebounceTime2 = millis(); 
} 

if ((millis() - lastDebounceTime2) > debounceDelay2) { 
// whatever the reading is at, it's been there for longer 
// than the debounce delay, so take it as the actual current state: 

// if the button state has changed: 
if (reading2 != buttonState) { 
    buttonState = reading2; 

    // only toggle the LED if the new button state is HIGH 
    if (buttonState == HIGH) { 
    ledState2 = !ledState2; 
    } 
    } 
    } 

    // set the LED: 
    digitalWrite(ledPin2, ledState2); 


    lastButtonState = reading2; 
+1

あなたの問題は何ですか? – Ccr

+0

私はArduinoとWanの新機能で、ボタンを1回押すだけで入力を一定時間送ることができました。 – ChrisPlusPlus

答えて

関連する問題