为什么我的if语句在我的程序不工作?

  • 本文关键字:程序 工作 if 语句 arduino
  • 更新时间 :
  • 英文 :


我正试图制作一个程序,所以当2个开关被翻转时,它们会点亮两个4引脚led,但只有当两个开关都被翻转时。我尝试着创造一个很酷的《BattleBots》竞技场,并认为拥有这样的内容作为开始序列将会很酷!如果你发现任何额外的错误,请随时纠正我。

int buttonPin = 13;
int buttonPin2 = 3;
int counter = 0;
int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the  blue LED
int greenpin =9;// select the pin for the green LED
int redpin2 = 7; //select the pin for the 2 red LED
int bluepin2 =6; // select the pin for the 2 blue LED
int greenpin2 =5;// select the pin for the 2 green LED

int pinButton = 13; //the pin where we connect the button
int pinButton2 = 3; //the pin where we connect the button
int val;

void setup() {
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(redpin2, OUTPUT);
pinMode(bluepin2, OUTPUT);
pinMode(greenpin2, OUTPUT);
Serial.begin(9600);
}

void loop() 
{ int buttonState;
buttonState = digitalRead(buttonPin);
int buttonState2;
buttonState2 = digitalRead(buttonPin2);
}
int main()

if (buttonState == LOW && buttonState2 == LOW);
analogWrite(11, 0);
analogWrite(10, 225);
analogWrite(9,  0);
delay(5); 
analogWrite(7, 0);
analogWrite(6, 225);
analogWrite(5, 0);
delay(5); 

}else{
analogWrite(11, 225);
analogWrite(10, 0);
analogWrite(9,  0);
delay(5); 
analogWrite(7, 0);
analogWrite(6, 0);
analogWrite(5, 225);
delay(5);
}

你的代码似乎是从几个来源剪切和粘贴的,这是它应该看起来像什么,我在代码中添加了注释:

const int buttonPin = 13;
const int buttonPin2 = 3;
const int redpin = 11; //select the pin for the red LED
const int bluepin = 10; // select the pin for the  blue LED
const int greenpin = 9; // select the pin for the green LED
const int redpin2 = 7; //select the pin for the 2 red LED
const int bluepin2 = 6; // select the pin for the 2 blue LED
const int greenpin2 = 5; // select the pin for the 2 green LED
/*
Unused variables removed:
int counter = 0;
int pinButton = 13;
int pinButton2 = 3;
int val;
*/

void setup() {
pinMode(buttonPin, INPUT);
pinMode(redpin, OUTPUT);
/*
Depending on your hardware you may want to pull-up the button pins:
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
*/

pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(redpin2, OUTPUT);
pinMode(bluepin2, OUTPUT);
pinMode(greenpin2, OUTPUT);
Serial.begin(9600);
}
void loop() {
int buttonState;
buttonState = digitalRead(buttonPin);
int buttonState2;
buttonState2 = digitalRead(buttonPin2);

/*
main() function had several issues, it was a function returning an integer without 
any return statement and it is never called by your code. Looks like it should be 
part of loop() like so:
*/

if (buttonState == LOW && buttonState2 == LOW) {
analogWrite(11, 0);
analogWrite(10, 225);
analogWrite(9,  0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 225);
analogWrite(5, 0);
delay(5);
} else {
analogWrite(11, 225);
analogWrite(10, 0);
analogWrite(9,  0);
delay(5);
analogWrite(7, 0);
analogWrite(6, 0);
analogWrite(5, 225);
delay(5);
}
}

相关内容

最新更新