Pin High/Low-Need help with the code

I have the following codes to turn Pin 9 & 10 High/Low. It works fine as I send the command/character from serial monitor. However, my problem is that as soon as I start/connect the Arduino to computer, both the Pins go High. I have to send then command from Serial Monitor to make them Low. Can I make both the Pins Low at the beginning? Would appreciate your help!

#include "SoftwareSerial.h"
char val1; // variable to receive data from the serial port
int ledpin = 9; // Arduino LED pin 9 
int pin = 10;  // Arduino LED pin 11

// software serial #1: RX = digital pin 0, TX = digital pin 1
SoftwareSerial portOne(0, 1);

void setup() {
   pinMode(ledpin, OUTPUT); // pin 9 (on-board LED) as OUTPUT
   pinMode(pin, OUTPUT);
   Serial.begin(9600); // start serial communication at 9600bps
   portOne.begin(9600);
   
   
  }

void loop() {
     
   if( portOne.available()>0 ) // if data is available to read
        {
      val1 = Serial.read(); // read it and store it in 'val1'
       }
        
        if(val1 == 'A' ) // if 'A' is received
        {
      digitalWrite(ledpin, HIGH); // turn ON the pin 9
      Serial.println(val1);
        } 
                
        
       if(val1 == 'B')// if 'B is received 
       {
      digitalWrite(ledpin, LOW); // turn it OFF
      Serial.println(val1);
        }
        
        if( val1 == 'C' ) // if 'C' is received
        {
        digitalWrite(pin, HIGH); // turn ON the pin 10
       Serial.println(val1);
      
        }
      if(val1=='D') // if 'D' is received 
      {
      digitalWrite(pin, LOW); // turn it OFF
      Serial.println(val1);
      
      }
    
                
   }

What board? Pins 0 and 1 are usually the hardware serial port.
Why are you trying to run software and hardware serial ports from the same pins?

Edit:
To answer your question, in setup add…

digitalWrite(...., LOW);

Fill in the … blank. :wink:

Arduino UNO. I am using pin 9 & 10. I did add digitalWrite code as you suggested, but as soon as I download the code to Arduino, both the pins go High. Even after I reconnect the Arduino to computer

void setup() {
   pinMode(ledpin, OUTPUT); // pin 9 (on-board LED) as OUTPUT
   pinMode(pin, OUTPUT);
   Serial.begin(9600); // start serial communication at 9600bps
   portOne.begin(9600);
   digitalWrite(ledpin, LOW);
   digitalWrite(pin, LOW);
      
  }

Post your code in full. Stick serial print in a few places and print out digital read pin. Number your serialprints and you will see where it is changed from low to high

Thanks for your help! Below are the full codes. I wrote SerialPrintln code at 4 places. Would appreciate if you suggest at what other places I should write Serial Print code (exactly not clear what you suggested)

 #include "SoftwareSerial.h"
char val1; // variable to receive data from the serial port
int ledpin = 9; // Arduino LED pin 9 
int pin = 10;  // Arduino LED pin 11

// software serial #1: RX = digital pin 0, TX = digital pin 1
SoftwareSerial portOne(0, 1);

void setup() {
   pinMode(ledpin, OUTPUT); // pin 9 (on-board LED) as OUTPUT
   pinMode(pin, OUTPUT);
   Serial.begin(9600); // start serial communication at 9600bps
   portOne.begin(9600);
   digitalWrite(ledpin, LOW);
   digitalWrite(pin, LOW);
      
  }

void loop() {
     
   if( portOne.available()>0 ) // if data is available to read
        {
      val1 = Serial.read(); // read it and store it in 'val1'
       }
        
        if(val1 == 'A' ) // if 'A' is received
        {
      digitalWrite(ledpin, HIGH); // turn ON the pin 9
      Serial.println(val1);
        } 
                
        
       if(val1 == 'B')// if 'B is received 
       {
      digitalWrite(ledpin, LOW); // turn it OFF
      Serial.println(val1);
        }
        
        if( val1 == 'C' ) // if 'C' is received
        {
        digitalWrite(pin, HIGH); // turn ON the pin 10
       Serial.println(val1);
      
        }
      if(val1=='D') // if 'D' is received 
      {
      digitalWrite(pin, LOW); // turn it OFF
      Serial.println(val1);
      
      }
SoftwareSerial portOne(0, 1);

Don't use the hardware Serial pins with SoftwareSerial. What would be the point anyway ? The whole point of SoftwareSerial is to give you an additional serial port on pins of your choosing so that you can still use the hardware Serial port for debugging and/or user input

  if( portOne.available()>0 ) // if data is available to read
        {
      val1 = Serial.read(); // read it and store it in 'val1'
       }

This is particularly daft. If data is available on the (wrongly configured) SoftwareSerial port then read from hardware Serial. That makes no sense even if SoftwareSerial were being used properly

jag68: Arduino UNO. I am using pin 9 & 10.

For the LEDs yes.

For the software serial port you are using 0 and 1...

// software serial #1: RX = digital pin 0, TX = digital pin 1
SoftwareSerial portOne(0, 1)

As UKHeliBob says that's particularly daft as you already have a hardware serial port there.

Just use Serial and ditch the SoftwareSerial portOne definition and references.

I worked it out. The pin 9 & 10 were connected to dual channel SRD-5VFV-SL-C Relay. That relay may be an issue. I changed from Pin HIGH to LOW in the codes, including code in the setup (from LOW to HIGH). Now it is fine. Still do not know why I have to change HIGH to LOW in the code. Below are updated codes:

 #include "SoftwareSerial.h"
char val1; // variable to receive data from the serial port
int ledpin = 9; // Arduino LED pin 9 
int pin = 10;  // Arduino LED pin 11

// software serial #1: RX = digital pin 0, TX = digital pin 1
SoftwareSerial portOne(0, 1);

void setup() {
   pinMode(ledpin, OUTPUT); // pin 9 (on-board LED) as OUTPUT
   pinMode(pin, OUTPUT);
   Serial.begin(9600); // start serial communication at 9600bps
   portOne.begin(9600);
   digitalWrite(ledpin, HIGH);
   digitalWrite(pin, HIGH);
      
  }

void loop() {
     
   if( portOne.available()>0 ) // if data is available to read
        {
      val1 = Serial.read(); // read it and store it in 'val1'
       }
        
        if(val1 == 'A' ) // if 'A' is received
        {
      digitalWrite(ledpin, LOW); // turn ON the pin 9
      Serial.println(val1);
        } 
                
        
       if(val1 == 'B')// if 'B is received 
       {
      digitalWrite(ledpin, HIGH); // turn it OFF
      Serial.println(val1);
        }
        
        if( val1 == 'C' ) // if 'C' is received
        {
        digitalWrite(pin, LOW); // turn ON the pin 10
       Serial.println(val1);
      
        }
      if(val1=='D') // if 'D' is received 
      {
      digitalWrite(pin, HIGH); // turn it OFF
      Serial.println(val1);
      
      }
    
                
   }

Relay boards usually work on reverse logic: LOW = Relay closed HIGH = Relay open

You are right@pcbc. Thanks for your help!

I worked it out.

What about the problem with SoftwareSerial ?

@UKHelibob, I don't have issue with Software serial. Was having issue with the relay (not Pins, as I originally through) was going high at the start.

Thanks to all of you for guiding me (a novice person)!

I don't have issue with Software serial

Are you still using pins 0 and 1 with SoftSerial ?

Yes@UKHeliBob.

Why?

jag68: Yes@UKHeliBob.

Why ?

Have you read the earlier replies pointing out the problems of using pins 0 and 1 with SoftwareSerial and questioning whether you need to use it in the first place ?

I am using a Bluetooth module ZS-040 to send command from processing software.I connected RX of ZS-040 to TX of Arduino and vice-versa. It is working fine

jag68: I am using a Bluetooth module ZS-040 to send command from processing software.I connected RX of ZS-040 to TX of Arduino and vice-versa. It is working fine

OK, have it your own way, but what you have done is wrong even if it does "work"

I can change the serial port from 0 &1 to other pins. What I know if I use other pins, I may need a resistor in line to connect to limit current. I may be wrong.

Use either hardware Serial on pins 0 and 1 or SoftwareSerial on 2 other pins, not a mixture of the two. If you use SoftwareSerial that leaves hardware Serial free for printing debugging information

I may be wrong.

You are wrong