Variable in called function redeclaring pin 0 and 1 due to its reading value

Viewed 31

I have written a Arduino code for Accident prevention system on road turning by using signals and tested in tinker CAD simulation, the output of pin 0 and 1 got affected when the function for buzzer pin get called and if I removed the function from code then code runs well.

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); 
  }
}

please provide me any solution over this.

1 Answers

You did not mention which board you use. On an Arduino Uno for example, pins 0 and 1 are connected to the hardware serial interface, which is connected to the USB controller to communicate with the computer to upload sketches and byserial monitor for example. In function_relay you call Serial.println which also is serial communication which interferes with these pins.

I suggest not to use pins 0 and 1. If you're out of pins, on an UNO, you can also use the analog pins (labeled A0-A5) - they can be used as digital pins (D14-D19) - see Arduino UNO pinout diagram.

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.

See: https://www.arduino.cc/reference/en/language/functions/communication/serial/

Related