0
我目前正在使用Raspberry Pi进行锻炼。Arduino-uno项目
我与实验电路板下面的工作:
- 5个LED的
- 2开关
我下面的代码应该如下方式工作:
随着第一个按钮可以选择下一个LED,第二个按钮可以将选定的LED打开/关闭。当你在最后的LED它给出了一个真或假(或关闭),使用以下的输出:
Circuits 我看不出有什么错我的代码:
//const variables
const int leds[] = {3, 5, 6, 9, 11};
const int buttons[] = {12, 13};
//variables that will change
int buttonState[] = {false, false};
int lastButtonState[] = {false, false};
int values[] = {false, false, false, false};
void setup() {
//init LEDs
for(int i = 0; i < sizeof(leds); i++){
pinMode(leds[i], OUTPUT);
}
//init buttons
for(int i = 0; i < sizeof(buttons); i++){
pinMode(buttons[i], INPUT);
}
}
void loop() {
//fade when game starts
fade();
//start game
start();
//output of game
output();
}
void output(){
bool t1 = !values[0];
bool t2 = t1 && values[1];
bool t3 = values[2] || values[3];
bool Q = !(t2 || t3);
if(!Q){
digitalWrite(leds[4], true);
}else{
digitalWrite(leds[4], false);
}
}
void start(){
//total of leds
int j = 0;
//check if user is not at 5th led
while(j < 4){
//loop through buttons
for(int i = 0; i < 2; i++){
// Read button
buttonState[i] = digitalRead(buttons[i]);
// check button state
if (buttonState[i] != lastButtonState[i]) {
// if the state has changed
if (buttonState[i] == HIGH) {
//check if button 1
if(i == 0){
//select next LED
j++;
}
//else button 2
else{
// if the current state of the 2nd button is HIGH
while(i == 1){
//if current value of led is false, put it true
if(values[j] == false){
//put led on
digitalWrite(leds[j], true);
values[j] = true;
delay(50);
}else{
//put led off
digitalWrite(leds[j], false);
delay(50);
values[j] = false;
}
//go back to button 1?
i = 0;
}
}
//go back to button 1?
i = 0;
}
}
// save the current state as the last state,
// for next time through the loop
lastButtonState[i] = buttonState[i];
// wait a little
delay(50);
}
}
}
void fade(){
//put every led on half-on
for(int i = 0; i < sizeof(leds); i++){
analogWrite(leds[i], 100);
}
}
当然,这不会建立没有警告?你为什么不提到任何问题? pinMode(leds,OUTPUT);'语句必须是错误的,它应该是'pinMode(leds [i],OUTPUT);',毕竟这是循环的重点(和输入相同)。 – unwind
当我验证它,它运行时没有给我错误,但我现在更新它。所以这不再是问题。 所以目前第二个按钮,应该把LED打开/关闭只能关闭LED而不能开启。 – DiceOfDoom
只要你的问题纯粹是关于软件,这里就是主题。但只是为了记录,应该向http://electronics.stackexchange.com/(他们需要除代码之外的一些示意图)询问可能与软件或硬件相关的问题。有关Ardunio的具体问题,请访问http://arduino.stackexchange.com/。还有http://raspberrypi.stackexchange.com/。 – Lundin