我在草图中有一个问题,有12个LED和4个按钮,其中按住按钮可以关闭某些LED。我用一个LED和一个按钮做了一个更基本的草图来展示我的问题,但这个基本草图完美无缺!所以我想知道为什么我的基本示例(草图1)工作得很好但是LED在我更复杂的例子(草图2)中闪烁,好吗?
SKETCH 1(效果很好):
//sets up button and LED connections:
const int led_1 = 4;
const int but_1 = 8;
//sets up variable butState that will be changed later
int butState;
void setup() {
//sets pin modes:
pinMode(led_1, OUTPUT);
pinMode(but_1, INPUT);
}
void loop() {
//variable that reads the button state:
butState = digitalRead(but_1);
//if the button state is high, sets LED state low. Opposite if button state is low:
if (butState == HIGH) {
delay(200);
digitalWrite(led_1, LOW);
}
else {
delay(200);
digitalWrite(led_1, HIGH);
}
}
SKETCH 2(LED闪烁):
//turns all lights on. turns in1 lights off when button 1 pressed.
//turns in2 lights off when second button is pressed.
//turns out lights off when third button is pressed
//turns all lights off when fourth button pressed
//result (output) lights:
const int res_8 = A5;
const int res_4 = A4;
const int res_2 = A3;
const int res_0 = A2;
//in1 lights:
const int in1_4 = 4;
const int in1_2 = 3;
const int in1_0 = 2;
//in2 lights:
const int in2_4 = 7;
const int in2_2 = 6;
const int in2_0 = 5;
//input buttons:
const int b_4 = 8;
const int b_2 = 9;
const int b_0 = 10;
const int b_enter = 11;
// set up switch states
int b4State = 0;
int b2State = 0;
int b0State = 0;
int benterState = 0;
void setup() {
// set up pins
pinMode(res_8, OUTPUT);
pinMode(res_4, OUTPUT);
pinMode(res_2, OUTPUT);
pinMode(res_0, OUTPUT);
pinMode(in1_4, OUTPUT);
pinMode(in1_2, OUTPUT);
pinMode(in1_0, OUTPUT);
pinMode(in2_4, OUTPUT);
pinMode(in2_2, OUTPUT);
pinMode(in2_0, OUTPUT);
pinMode(b_4, INPUT);
pinMode(b_2, INPUT);
pinMode(b_0, INPUT);
pinMode(b_enter, INPUT);
}
void loop() {
//reads the state of button inputs
b4State = digitalRead(b_4);
b2State = digitalRead(b_2);
b0State = digitalRead(b_0);
benterState = digitalRead(b_enter);
//checks button 1
if (b4State == HIGH) {
delay(1000);
digitalWrite(in1_4, LOW);
digitalWrite(in1_2, LOW);
digitalWrite(in1_0, LOW);
}
else {
delay(1000);
digitalWrite(in1_4, HIGH);
digitalWrite(in1_2, HIGH);
digitalWrite(in1_0, HIGH);
}
//checks button 2
if (b2State == HIGH) {
delay(1000);
digitalWrite(in2_4, LOW);
digitalWrite(in2_2, LOW);
digitalWrite(in2_0, LOW);
}
else {
delay(1000);
digitalWrite(in2_4, HIGH);
digitalWrite(in2_2, HIGH);
digitalWrite(in2_0, HIGH);
}
//checks button 3
if (b0State == HIGH) {
delay(1000);
analogWrite(res_8, 0);
analogWrite(res_4, 0);
analogWrite(res_2, 0);
analogWrite(res_0, 0);
}
else {
delay(1000);
analogWrite(res_8, 1023);
analogWrite(res_4, 1023);
analogWrite(res_2, 1023);
analogWrite(res_0, 1023);
}
//checks enter button
if (benterState == HIGH) {
delay(1000);
digitalWrite(in1_4, LOW);
digitalWrite(in1_2, LOW);
digitalWrite(in1_0, LOW);
digitalWrite(in2_4, LOW);
digitalWrite(in2_2, LOW);
digitalWrite(in2_0, LOW);
analogWrite(res_8, 0);
analogWrite(res_4, 0);
analogWrite(res_2, 0);
analogWrite(res_0, 0);
}
else {
delay(1000);
analogWrite(res_8, 1023);
analogWrite(res_4, 1023);
analogWrite(res_2, 1023);
analogWrite(res_0, 1023);
digitalWrite(in1_4, HIGH);
digitalWrite(in1_2, HIGH);
digitalWrite(in1_0, HIGH);
digitalWrite(in2_4, HIGH);
digitalWrite(in2_2, HIGH);
digitalWrite(in2_0, HIGH);
}
// //delays
// delay(250);
}
感谢您的帮助!
答案 0 :(得分:0)
问题出在你的考试中。
//checks button 1
if (b4State == HIGH) {
// turn LEDs on for 1 second
}
else {
/* turn LEDs off 1 sec */ // button 4 released -> LEDs OFF,
/* even if button 2 is pushed! */ // even when button 2 is pushed
}
//checks button 2
if (b2State == HIGH)
{
// turn LEDs on 1 second
}
else
{
// turn LEDs off 1 sec -
// even when button 4 is pushed!
}
你想要的逻辑:
//checks button 1
if (b4State == HIGH)
{
delay(1000);
digitalWrite(in1_4, LOW);
digitalWrite(in1_2, LOW);
digitalWrite(in1_0, LOW);
}
else if (b2State == HIGH) // button 4 is not pushed... check button 2
{
delay(1000);
digitalWrite(in2_4, LOW);
digitalWrite(in2_2, LOW);
digitalWrite(in2_0, LOW);
}
else
{
delay(1000); // only turn off the LED when both buttons
digitalWrite(in2_4, HIGH); // are released
digitalWrite(in2_2, HIGH);
digitalWrite(in2_0, HIGH);
}
按下按钮2和4会在您发布的代码中产生相同的效果,因此测试可以简化为简单的if / else,但我认为您可能计划在应用中添加更多代码。