Arduino digitalwrite 无法正常工作
Arduino digitalwrite not working like it should
所以我的小项目有问题,我正在制作雾灯转向灯 mod。具体来说 - 当其中一个转向信号灯闪烁时,则该侧雾灯打开(不闪烁)。如果雾灯打开,则转向信号灯对面的雾灯将关闭。
关于代码:
一切正常,除了引脚 5 使继电器可以非常快速地打开和关闭,而且引脚 12 是高电平还是低电平都没有关系,它仍在闪烁。
仅当引脚 5 处于高位时才会闪烁。
自己编码:
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);
}
}
}
想想
的用法
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
}
要点是,通过跟踪您的代码,它会检查 pin#5,然后打开信号,稍等片刻,然后检查 pin#4,此检查的计算结果为 false。因此,它关闭两个信号并稍等片刻。然后在下一个循环中,它一次又一次地做同样的事情。这就是闪烁发生的地方。试试这个代码:
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);
}
}
}
所以我的小项目有问题,我正在制作雾灯转向灯 mod。具体来说 - 当其中一个转向信号灯闪烁时,则该侧雾灯打开(不闪烁)。如果雾灯打开,则转向信号灯对面的雾灯将关闭。
关于代码: 一切正常,除了引脚 5 使继电器可以非常快速地打开和关闭,而且引脚 12 是高电平还是低电平都没有关系,它仍在闪烁。 仅当引脚 5 处于高位时才会闪烁。
自己编码:
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);
}
}
}
想想
的用法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
}
要点是,通过跟踪您的代码,它会检查 pin#5,然后打开信号,稍等片刻,然后检查 pin#4,此检查的计算结果为 false。因此,它关闭两个信号并稍等片刻。然后在下一个循环中,它一次又一次地做同样的事情。这就是闪烁发生的地方。试试这个代码:
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);
}
}
}