0

我对整个编码很陌生。并且花了很长时间试图让它工作,但看起来它应该也能正常工作。计划是让 ledPin 在按下按钮时亮起,而 lightPin 每按下 8 次就会亮起。

这是一些工程学院的工作。如您所知,我没有选择计算机科学是有原因的!

const int buttonPin = 2; // the number of the pushbutton pin
const int lightPin = 3
const int ledPin = 13; // the number of the LED pin
int count = 0; // Count the button presses

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

void setup()
{
// initialize the LED pin as an output:
    pinMode(ledPin, OUTPUT);
// 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)
    {
// turn LED on:   
        digitalWrite(ledPin, HIGH);
        count++; // add 1 to the count
        if (count >= 8)
        {
            digitalWrite(lightPin, HIGH);
            delay(1000)
            digitalWrite(lightPin, LOW);
            count = 0;
        }
    }
    else
    {
// turn LED off:
        digitalWrite(ledPin, LOW);
    }
}

每次按下按钮时,ledPin 都会亮起。每按 8 次,lightPin 就会亮起。但目前每按一次按钮都会亮起,而且它们的持续时间似乎与延迟有关。

4

0 回答 0