Arduino,如果没有LED“卡住”

时间:2013-05-07 00:21:00

标签: arduino

所以代码没有正常工作,有两个LED不会关闭“高亮”问题。当我运行程序的其他部分时。我想在其他部分关闭它们。 :)

包括

byte ledPin[] = {8, 9, 10, 11, 12, 13}; //--------------------------------.
int ledDelay;                           // Del 1
int direction = 1;
int currentLED = 0;
unsigned long changeTime;
int potPin = 0; 


Servo myservo;  // create servo object to control a servo 

int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin 
int va;

void setup() 
{ 

  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, INPUT);
  myservo.attach(3); // attaches the servo on pin 9 to the servo object 
  Serial.begin(9600);

  for (int x=0; x<6; x++) {
  pinMode(ledPin[x], OUTPUT); }
  changeTime = millis();
} 

void loop() {
   int  on = digitalRead(6);
if (on == HIGH)
{
  myservo.attach(3);
// Here is the problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      if va < 523)
{
  digitalWrite(5, HIGH);
}
else if (va > 555)
{
   digitalWrite(4, HIGH);
}

else
{
   digitalWrite(4, LOW);
   digitalWrite(5, LOW);
}
// Here is the problem!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  digitalWrite(8, LOW);
  digitalWrite(9, LOW);
  digitalWrite(10, LOW);
  digitalWrite(11, LOW);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);
  va = analogRead(potPin);            // reads the value of the potentiometer (value between 0 and 1023) 
  val = map(va, 0, 1023, 0, 179);     // scale it to use it with the servo (value between 0 and 180) 
  myservo.write(val);                  // sets the servo position according to the scaled value 
  delay(1);                           // waits for the servo to get there 

}
else
{
  myservo.detach();
  digitalWrite(5, LOW);
  digitalWrite(4, LOW);
  ledDelay = analogRead(potPin) / 4;
  if ((millis() - changeTime) > ledDelay) 
  {
  changeLED();
  changeTime = millis();
  }
}
}

  void changeLED() {
   for (int x=0; x<6; x++)
   {
   digitalWrite(ledPin[x], LOW);
   }
   digitalWrite(ledPin[currentLED], HIGH);
   currentLED += direction;
  if (currentLED == 6) {direction = -1;}
  if (currentLED == 0) {direction = 1;}
  }

提前谢谢!

1 个答案:

答案 0 :(得分:2)

在草图的末尾,您有以下行:

if (currentLED == 6) { direction = -1; }

我认为,如果没有实际运行该程序,问题就在这里。在上一行中,您已将一个值添加到currentLED的值中,并且您正在检查是否已离开ledPin数组的末尾。您可以更改方向,但不要将currentLED位置重置为ledPin范围内的内容。

下次changeLED被调用时,它会尝试调用digitalWrite(ledPin[currentLED], HIGH);,但currentLED的值为6,这超出了ledPin数组。 Arduino可能会在这一点上感到不安。

我认为您只需要更改语句以检查currentLED == 5而不是6。这意味着,下次调用changeLED时,将打开最后一个LED,currentLED的值将递减(direction == -1),并将其保留在ledPin内范围。