Arduino digitalwrite不能像它那样工作

时间:2015-03-18 20:31:00

标签: flash arduino arduino-uno electronics

所以我的小项目有问题,我正在制作雾灯转向信号模块。具体而言 - 当其中一个转向灯闪烁时,侧雾灯开启(不闪烁)。如果雾灯亮,则转向信号灯的对侧雾灯将熄灭。

关于代码: 一切都工作得很好,除了引脚5让继电器快速打开和关闭,如果引脚12为高电平或低电平仍然闪烁,则无关紧要。 仅当引脚5处于HIGH位置时才会出现闪烁。

自己编码:

int MiglaK = 9;       //Right foglight
int MiglaL = 8;       //Left fog light
int PagrK = 4;        //Right turn signal
int PagrL = 5;        //Left turn signal
int Miglinieki = 12;  //Fog ON
int val = 0;

void setup() {
pinMode(MiglaK, OUTPUT);
pinMode(MiglaL, OUTPUT);
pinMode(PagrK, INPUT);
pinMode(PagrL, INPUT);
pinMode(Miglinieki, INPUT);
}

void loop() {
//**********Fogs are OFF**********

val = digitalRead(Miglinieki);
if (val == LOW)
{
  //****Hazzard lights****
  if (digitalRead(PagrL) == HIGH && digitalRead(PagrK) == HIGH)
        {
         digitalWrite(MiglaL,LOW);
         digitalWrite(MiglaK,LOW);
         delay(700); 
        }
  //****Left turn signal****
  if (digitalRead(PagrL) == HIGH)
        {
         digitalWrite(MiglaL, HIGH);
         delay(700);
        }
  //****Right turn signal****
  if (digitalRead(PagrK) == HIGH)
        {
         digitalWrite(MiglaK, HIGH);
         delay(700);
        }
  else
        {
         digitalWrite(MiglaL,LOW);
         digitalWrite(MiglaK,LOW);
         delay(100);
        }

}

//**********Fog are ON**********

if (val == HIGH)
{
  //****Hazzard lights****
  if (digitalRead(PagrL) == HIGH && digitalRead(PagrK) == HIGH)
        {
         digitalWrite(MiglaL,HIGH);
         digitalWrite(MiglaK,HIGH);
         delay(700); 
        }
  //****Left turn signal****
  if (digitalRead(PagrL) == HIGH)
        {
         digitalWrite(MiglaK, LOW);
         delay(700);
        }
  //****Right turn signal****
  if (digitalRead(PagrK) == HIGH)
        {
         digitalWrite(MiglaL, LOW);
         delay(700);
        }
  else
        {
         digitalWrite(MiglaL,HIGH);
         digitalWrite(MiglaK,HIGH);
         delay(100);
        }        
}
}

1 个答案:

答案 0 :(得分:1)

考虑

的用法
if(/*Condition 1*/){
//Some code
} else if (/*Condition 2*/){
//Some other code
} else{
//And a bit more code
}

而不是

if(/*Condition 1*/){
//Some code
}
if (/*Condition 2*/){
//Some other code
} else{
//And a bit more code
}

关键在于,通过跟踪代码,它检查引脚#5然后打开信号等待一段时间然后它检查引脚#4并且此检查评估为假。因此,它会关闭两个信号并等待一下。然后在下一个周期中,它会一次又一次地做同样的事情。这就是眨眼发生的地方。试试这段代码:

int MiglaK = 9;       //Right foglight
int MiglaL = 8;       //Left fog light
int PagrK = 4;        //Right turn signal
int PagrL = 5;        //Left turn signal
int Miglinieki = 12;  //Fog ON
int val = 0;

void setup() {
pinMode(MiglaK, OUTPUT);
pinMode(MiglaL, OUTPUT);
pinMode(PagrK, INPUT);
pinMode(PagrL, INPUT);
pinMode(Miglinieki, INPUT);
}

void loop() {
//**********Fogs are OFF**********

val = digitalRead(Miglinieki);
if (val == LOW)
{
  //****Hazzard lights****
  if (digitalRead(PagrL) == HIGH && digitalRead(PagrK) == HIGH)
        {
         digitalWrite(MiglaL,LOW);
         digitalWrite(MiglaK,LOW);
         delay(700); 
        }
  //****Left turn signal****
  else if (digitalRead(PagrL) == HIGH)
        {
         digitalWrite(MiglaL, HIGH);
         delay(700);
        }
  //****Right turn signal****
  else if (digitalRead(PagrK) == HIGH)
        {
         digitalWrite(MiglaK, HIGH);
         delay(700);
        }
  else
        {
         digitalWrite(MiglaL,LOW);
         digitalWrite(MiglaK,LOW);
         delay(100);
        }

}

//**********Fog are ON**********

if (val == HIGH)
{
  //****Hazzard lights****
  if (digitalRead(PagrL) == HIGH && digitalRead(PagrK) == HIGH)
        {
         digitalWrite(MiglaL,HIGH);
         digitalWrite(MiglaK,HIGH);
         delay(700); 
        }
  //****Left turn signal****
  else if (digitalRead(PagrL) == HIGH)
        {
         digitalWrite(MiglaK, LOW);
         delay(700);
        }
  //****Right turn signal****
  else if (digitalRead(PagrK) == HIGH)
        {
         digitalWrite(MiglaL, LOW);
         delay(700);
        }
  else
        {
         digitalWrite(MiglaL,HIGH);
         digitalWrite(MiglaK,HIGH);
         delay(100);
        }        
}
}