2017-01-14 4 views
0

Я хочу сделать 5-секундный таймер в arduino, если быть более подробным, у меня есть RGB-светодиод, и я хочу подсвечивать цвет для количества раз, когда я нажимаю кнопку (у меня есть толчок), например, в течение этих 5 секунд светодиод должен оставаться не освещенным в течение тех же 5 секунд, если я нажму кнопку один раз, после окончания таймера (5 секунд) светодиод загорится красным, если я нажму кнопку два раза вел синий или любой другой.arduino 5 секунд таймер

const int buttonPin = 2;  // the number of the pushbutton pin 
 
const int ledbPin = 13;  // the number of the LED pin 
 
const int ledgPin = 12;  // the number of the LED pin 
 
const int ledrPin = 11;  // the number of the LED pin 
 
int count = 0;    // Count the button presses 
 
unsigned long currentTime; 
 
unsigned long loopTime; 
 
// variables will change: 
 
int buttonState = 0;   // variable for reading the pushbutton status 
 

 
void setup() { 
 
    // initialize the LED pin as an output: 
 
    pinMode(ledrPin, OUTPUT); 
 
    pinMode(ledgPin, OUTPUT);  
 

 
    pinMode(ledbPin, OUTPUT);  
 
currentTime = millis(); 
 
    loopTime = currentTime; 
 
    // initialize the pushbutton pin as an input: 
 
    pinMode(buttonPin, INPUT);  
 
} 
 

 
void loop(){ 
 
    // read the state of the pushbutton value: 
 
    buttonState = digitalRead(buttonPin); 
 

 
    // check if the pushbutton is pressed. 
 
    // if it is, the buttonState is HIGH: 
 
    if (buttonState == HIGH) {  
 
    count++;    // add 1 to the count 
 
    if (count >= 1) { 
 
    currentTime = millis(); 
 
     if(currentTime >= (loopTime + 5000)){ 
 
     if (count == 1) { 
 
     digitalWrite(ledrPin, HIGH); 
 
     } 
 
     if (count == 2) { 
 
     digitalWrite(ledgPin, HIGH); 
 
     } 
 
     if (count == 3) { 
 
     digitalWrite(ledbPin, HIGH); 
 
     } 
 
     
 
     } 
 
    } 
 
    } 
 
    }

Это то, что я написал до сих пор:

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

ответ

0

Это потому, что вы делаете оператор if в цикле. Слишком быстрый цикл. Пока вы нажимаете и отпускаете кнопку, она уже выполняет цикл несколько раз. В те несколько раз ваш граф все еще становится все выше и выше. Я бы добавил миллис в инструкцию If из кнопки. Я только что привел пример. Вы можете сделать это по-своему.

void loop(){ 
    // read the state of the pushbutton value: 
    buttonState = digitalRead(buttonPin); 

    // check if the pushbutton is pressed. 
    // if it is, the buttonState is HIGH: 
    if (buttonState == HIGH && currentTime == 500 (didnt look which one your timer is)) {  
    count++;    // add 1 to the count 
    if (count >= 1) { 
    currentTime = millis(); 
     if(currentTime >= (loopTime + 5000)){ 
     if (count == 1) { 
     digitalWrite(ledrPin, HIGH); 
     } 
     if (count == 2) { 
     digitalWrite(ledgPin, HIGH); 
     } 
     if (count == 3) { 
     digitalWrite(ledbPin, HIGH); 
     } 

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