需要帮忙!使我的Arduino代码工作

时间:2016-04-18 07:36:51

标签: c arduino

我的arduino代码遇到了一些麻烦,我需要帮助才能使它工作,它将被用来控制一些LED灯。变亮,变暗,眨眼,使它们运转。

我在整个脚本中添加了注释,以便您可以阅读问题等等。

/*This script is supposed to make a arduino board control the lights on a lego creation for a school project.
  Button1 should make lights brighter
  Button2 should make ligts shine less
  Button3 should make the light blink
  Button4 is supposed to make the light run after each other.
*/

//Setting up all the lights and buttons
const int lights[] = {9, 8, 7};
int light = 0;

const int button1 = 24;
int buttonstate1 = 0;
const int button2 = 25;
int buttonstate2 = 0;
const int button3 = 26;
int buttonstate3 = 0;
const int button4 = 27;
int buttonstate4 = 0;
int lightStrength = 50;
int blinkToggle = 0;

//Setting up the input's and output's
void setup() {
  pinMode(button1, INPUT);
  pinMode(button2, INPUT);
  pinMode(button3, INPUT);
  pinMode(button4, INPUT);
  pinMode(lights[0], OUTPUT);
  pinMode(lights[1], OUTPUT);
  pinMode(lights[2], OUTPUT);

  //Here i have a problem with make the list take all in one line of code.
  analogWrite(lights[0], lightStrength);
  analogWrite(lights[1], lightStrength);
  analogWrite(lights[2], lightStrength);

}

void loop() {
//checking if the buttons are being pressed
buttonstate1 = digitalRead(button1);
buttonstate2 = digitalRead(button2);
buttonstate3 = digitalRead(button3);
buttonstate1 = digitalRead(button4);

//Make the lights brighter
  while (lightStrength <= 235){
    if (buttonstate1 == HIGH){
      delay(10);
      lightStrength = lightStrength + 1;
      delay(10);
      analogWrite(lights[0], lightStrength);
      analogWrite(lights[1], lightStrength);
      analogWrite(lights[2], lightStrength);
     }
   }

//make the lights darker
  while (lightStrength >= 45){
    if (buttonstate2 == HIGH){
      delay(5);
      lightStrength = lightStrength - 1;
      delay(5);
      analogWrite(lights[0], lightStrength);
      analogWrite(lights[1], lightStrength);
      analogWrite(lights[2], lightStrength);
    }
  }

//Blink the lights
  while(blinkToggle == 1){
    if (buttonstate3 == HIGH){
      delay(100);
      blinkToggle = 0;
    }
  }
  while(blinkToggle == 0){
    if (buttonstate3 == HIGH){
      delay(100);
      blinkToggle = 1;
    }
  }
  while(blinkToggle == 1){
    delay(150);
    analogWrite(lights[0], 0);
    analogWrite(lights[1], 0);
    analogWrite(lights[2], 0);
    delay(150);
    analogWrite(lights[0], lightStrength);
    analogWrite(lights[1], lightStrength);
    analogWrite(lights[2], lightStrength);
  }

//make the lights run (Not completed because the other code didn't work)
  if (buttonstate4 == HIGH){
    delay(100);

  }

}

1 个答案:

答案 0 :(得分:1)

首先,要求&#34;需要帮助&#34;是一般的...你应该问一个更好的问题。例如,我无法完全理解你的意思,问题是什么以及在某些情况下预期的行为是什么(例如,如果按下更多按钮会发生什么?)。

无论如何,这不是评论,而是答案,因为您的代码中存在一些常见错误,也许这些错误足以让您的代码成为世界。

例如,您将所有测试编写为

while (lightStrength <= 235){
    if (buttonstate1 == HIGH){

好吧,如果您的按钮很低,那么lightStrength将不会递增,循环将永远不会到达其结尾,因此程序将卡在那里。我建议你把代码改成

if (buttonstate1 == HIGH){
    while (lightStrength <= 235){

这不会停止你的代码。

然后blinkToggle的测试是错误的。你不应该在一段时间内测试它们,但是使用if(例如if(blinkToggle == 1){)。

按钮4没有实现,我不明白它的行为是什么,所以我不能告诉你如何实现它。

我不理解//Here i have a problem with make the list take all in one line of code.评论。如果要在不重复的情况下写入相同的值,请使用for循环:

byte i;
for (i = 0; i < 3; i++)
    analogWrite(lights[i], lightStrength);

现在,为您的计划提供一些建议。

首先,去抖动。如果您不知道弹跳是什么,请查看on wikipedia或在Google上搜索。无论如何,我发现只要你需要处理按钮,Bounce2库就非常有用。搜索其示例或只是要求查看更多示例。

并且..我建议你改变你在这个程序中的方法。 FSM(有限状态机)方法更适合于此。即使不使用复杂的库,只需简单的C,你就可以制作一个非常可靠的程序