被调用函数中的变量由于其读取值而重新声明引脚 0 和 1
Variable in called function redeclaring pin 0 and 1 due to its reading value
我已经使用信号编写了一个用于道路转弯事故预防系统的 Arduino 代码,并在 Tinker CAD 模拟中进行了测试,当蜂鸣器引脚的功能被调用时,引脚 0 和 1 的输出受到影响,如果我删除了从代码中运行,然后代码运行良好。
Simulation CKT image
int irSensor1 = 12; // connect ir sensor 1 to arduino pin 12
int irSensor2 = 13; // ir2 to pin 13
int led1 = 8; // conect Led1 to arduino pin 0
int led2 = 1;
int led3 = 2;
int led4 = 4;
int led5 = 7;
int led6 = 0;
//buzzer declear
int relayPin = 6;
int buzzerPin = 5;
void setup()
{
pinMode (irSensor1, INPUT); // sensor pin INPUT
pinMode (irSensor2, INPUT);
pinMode (led1, OUTPUT); // Led pin OUTPUT
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);
pinMode (led5, OUTPUT);
pinMode (led6, OUTPUT);
// buzzer set
pinMode (buzzerPin, OUTPUT);
pinMode (relayPin, INPUT);
Serial.begin(9600);
}
void loop()
{
// buzzer function call
function_relay();
led1,led2,led3,led4,led5,led6 = 0;// initialize all led off
int ir1State = digitalRead (irSensor1);
int ir2State = digitalRead (irSensor2);
// sensor 1 high AND 2 LOW
if (ir1State == 1 && ir2State == 0){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(2);
digitalWrite(led6, HIGH);
}
// SENSOR 2 HIGH AND 1 LOW
else if (ir1State == 0 && ir2State == 1){
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, HIGH);
delay(2);
digitalWrite(led6, LOW);
}
// BOTH SENSOR HIGH
else if (ir1State == 1 && ir2State == 1){
// buzzer function call
function_relay();
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(2);
digitalWrite(led6, HIGH);
//DELAY OF 20000 MS 20 =SEC
delay(20000);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(2);
digitalWrite(led6, HIGH);
// delay 10000 MS = 10 SEC
delay(10000);
}
// both sensor low
else
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led5, LOW);
delay(2);
digitalWrite(led6, LOW);
}
}
// function for relay operation with buzzer
void function_relay(){
int volts = digitalRead(relayPin);
delay(5);
if(volts == 1){
analogWrite(buzzerPin, 60);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
delay(2);
analogWrite(led6, HIGH);
Serial.println(volts);
}
else{
analogWrite(buzzerPin, 0);
}
}
请给我任何解决方案。
你没有提到你用的是哪个板子。例如,在 Arduino Uno 上,引脚 0 和 1 连接到硬件串行接口,该接口连接到 USB 控制器以与计算机通信以上传草图和串行监视器等。在 function_relay 中,您调用 Serial.println 这也是干扰这些引脚的串行通信。
我建议不要使用引脚 0 和 1。如果你没有引脚,在 UNO 上,你也可以使用模拟引脚(标记为 A0-A5)——它们可以用作数字引脚(D14 -D19) - 参见 Arduino UNO 引出线图。
On Uno, Nano, Mini, and Mega, pins 0 and 1 are used for communication with the computer. Connecting anything to these pins can interfere with that communication, including causing failed uploads to the board.
参见:
https://www.arduino.cc/reference/en/language/functions/communication/serial/
我已经使用信号编写了一个用于道路转弯事故预防系统的 Arduino 代码,并在 Tinker CAD 模拟中进行了测试,当蜂鸣器引脚的功能被调用时,引脚 0 和 1 的输出受到影响,如果我删除了从代码中运行,然后代码运行良好。
Simulation CKT image
int irSensor1 = 12; // connect ir sensor 1 to arduino pin 12
int irSensor2 = 13; // ir2 to pin 13
int led1 = 8; // conect Led1 to arduino pin 0
int led2 = 1;
int led3 = 2;
int led4 = 4;
int led5 = 7;
int led6 = 0;
//buzzer declear
int relayPin = 6;
int buzzerPin = 5;
void setup()
{
pinMode (irSensor1, INPUT); // sensor pin INPUT
pinMode (irSensor2, INPUT);
pinMode (led1, OUTPUT); // Led pin OUTPUT
pinMode (led2, OUTPUT);
pinMode (led3, OUTPUT);
pinMode (led4, OUTPUT);
pinMode (led5, OUTPUT);
pinMode (led6, OUTPUT);
// buzzer set
pinMode (buzzerPin, OUTPUT);
pinMode (relayPin, INPUT);
Serial.begin(9600);
}
void loop()
{
// buzzer function call
function_relay();
led1,led2,led3,led4,led5,led6 = 0;// initialize all led off
int ir1State = digitalRead (irSensor1);
int ir2State = digitalRead (irSensor2);
// sensor 1 high AND 2 LOW
if (ir1State == 1 && ir2State == 0){
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(2);
digitalWrite(led6, HIGH);
}
// SENSOR 2 HIGH AND 1 LOW
else if (ir1State == 0 && ir2State == 1){
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, HIGH);
delay(2);
digitalWrite(led6, LOW);
}
// BOTH SENSOR HIGH
else if (ir1State == 1 && ir2State == 1){
// buzzer function call
function_relay();
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, HIGH);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(2);
digitalWrite(led6, HIGH);
//DELAY OF 20000 MS 20 =SEC
delay(20000);
digitalWrite(led1, LOW);
digitalWrite(led2, HIGH);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
digitalWrite(led5, LOW);
delay(2);
digitalWrite(led6, HIGH);
// delay 10000 MS = 10 SEC
delay(10000);
}
// both sensor low
else
{
digitalWrite(led1, HIGH);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, HIGH);
digitalWrite(led5, LOW);
delay(2);
digitalWrite(led6, LOW);
}
}
// function for relay operation with buzzer
void function_relay(){
int volts = digitalRead(relayPin);
delay(5);
if(volts == 1){
analogWrite(buzzerPin, 60);
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
digitalWrite(led5, HIGH);
delay(2);
analogWrite(led6, HIGH);
Serial.println(volts);
}
else{
analogWrite(buzzerPin, 0);
}
}
请给我任何解决方案。
你没有提到你用的是哪个板子。例如,在 Arduino Uno 上,引脚 0 和 1 连接到硬件串行接口,该接口连接到 USB 控制器以与计算机通信以上传草图和串行监视器等。在 function_relay 中,您调用 Serial.println 这也是干扰这些引脚的串行通信。
我建议不要使用引脚 0 和 1。如果你没有引脚,在 UNO 上,你也可以使用模拟引脚(标记为 A0-A5)——它们可以用作数字引脚(D14 -D19) - 参见 Arduino UNO 引出线图。
On Uno, Nano, Mini, and Mega, pins 0 and 1 are used for communication with the computer. Connecting anything to these pins can interfere with that communication, including causing failed uploads to the board.
参见: https://www.arduino.cc/reference/en/language/functions/communication/serial/