Recently I uploaded individual codes for relay's and Servo motor seperately, it all worked fine. But when I combined these two codes and dumped in to the loop there is a delay in functioning of the relay's but the Servo motor is working fine.

I have to control these devices through Bluetooth HC-05 module. Could you please help me with the code?

simple LED test
#include <Servo.h>
char val;         // variable to receive data from the serial port
int ledpin = 2; // LED connected to pin 2 (on-board LED)
Servo myservo;
int pos = 0;    // variable to store the servo position
int motor = 0;

void setup()
  pinMode(ledpin = 2, OUTPUT); // pin 2 (on-board LED) as OUTPUT
  pinMode(ledpin = 3, OUTPUT); // pin 3 (on-board LED) as OUTPUT
  myservo.attach(9);  // attaches the servo on pin 9 to the servo object
  Serial.print("Arduino control Servo Motor Connected OK");
  Serial.begin(9600);       // start serial communication at 115200bps
  digitalWrite(ledpin = 2 ,HIGH);
  digitalWrite(ledpin = 3 ,HIGH);


void loop()

      if( Serial.read() == 'a' )    // if 'a' was received led 2 is switched off
    digitalWrite(ledpin = 2, HIGH);    // turn Off pin 2


  if( Serial.read() == 'A' )               // if 'A' was received led 2 on
    digitalWrite(ledpin = 2, LOW);  // turn ON pin 2


  if( Serial.read() == 'b' )        // if 'b' was received led 3 is switched off
    digitalWrite(ledpin = 3, HIGH);    // turn Off pin 3


  if( Serial.read() == 'B' )               // if 'B' was received led 3 on
    digitalWrite(ledpin = 3, LOW);  // turn ON pin 3


  if (Serial.read() == '\n')

   motor = Serial.parseInt();
    pos = Serial.parseInt();
      Serial.print("Data Response : ");
      Serial.print(motor, DEC);
      Serial.print(pos, DEC);



1 Answer 1


A couple of things stand out in your code.

  1. Whenever you perform a digital write you assign a value to ledpin:

    digitalWrite(ledpin = 3, LOW);

    This is not necessary as you do not do anything with the ledpin variable. Instead, just have the digital I/O pin number:

    digitalWrite(3, LOW);

    Or simply have 2 variables, one for pin 2 and another for pin 3. Eg.

    redledpin = 2;
    greenledpin = 3;

    This also applies to the pinMode function calls in the setup function.

  2. Each one of your if( Serial.read() == xx) statements will read a serial character off of the serial buffer. Instead store the read character in a local variable:

    readChar = Serial.read()
    if( readchar == 'a')
    if( readchar == 'A')
    if( readchar == 'b')
    if( readchar == 'B')
  3. I would encase all of the code inside loop() with:

    if ( Serial.available() )
  4. The delay function is blocking and this will cause issues. I suggest deleting the delay altogether.

  5. Due to the fact that you are sending commands to turn digital outputs on and off as well as sending an integer for motor and another for pos, I suggest that you define a packet structure and make your code follow that (with the necessary error handling). As the code currently stands you will miss information, depending on what character it sees first on the serial buffer.

    I suggest something like:

    A, B, 142, 090

    Starting the packet off with a know character is a good idea. So is having a timeout period - in case only a portion of your packet is received.

  • 2
    This will assign the value of 3 to ledpin and because the operation was successful it will evaluate to true. No it won't. It will evaluate to 3.
    – Nick Gammon
    Commented Mar 27, 2016 at 22:21
  • Which works because true is any value different from 0. If you try to write ledpin = 0 that will evaluate as false
    – frarugi87
    Commented Mar 29, 2016 at 9:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.