Multiple sketch combining issue

I need some guidance on my recent project. it’s basically a robot with ultrasonic sensor(HC-SR04), BLUETOOTH MODULE(HC-05), 8 CHANNEL RELAY MODULE AND ARDUINO UNO. I have already made sketch for all these but facing a lot of difficulty while merging them.

All these codes work well individually but when i try to use the merged sketch, it misbehaves and randomly turns on any relay, servos does not work as per instructions and jitters a lot.

The servos are powered by 7.2 25C li-po battery, relay module by 12v adaptor and ardunio by laptop. All have common ground.

RELAY.INO - PROGRAMMING FOR RELAY MODULE.

OAR.INO - PROGRAMMING FOR OBSTACLE AVOIDING FUNCTION.

MultiservoANDrelay.ino- i tried to combine relay sketch and servo sketch[failed] and didn’t tried to combine OAR.

I NEED TO SUBMIT THIS PROJECT BY COMING WEDNESDAY …PLZ HELP ME…THANKS IN ADVANCE

relay.ino (1.53 KB)

OAR.ino (2.37 KB)

MultiservoANDrelay.ino (2.69 KB)

Did each program work properly by itself? Which program did you use as base? Did you bother to test and debug your first merge or did you just merge all three together? Where does the program begin to fail? How far have you gotten with your debugging?

Paul

Ya, all program work well if run individually. I used Servoandrelay program as base. Now i have successfully combined relay and servo program ( replaced case to if) but still facing difficulties to merge OAR and relay and servo.

I want if i would send ‘a’ via Bluetooth OBSTACLE AVOIDING FUNCTION should begin and should stop when i would send ‘A’.

I tried to put a new if statement and inserted OAR program (added other sketch elements also and they don’t share any pin). When i upload this code arduino doesn’t respond at all.

This is the code which work well and i wanted to add OAR in this.

  #include <SoftwareSerial.h>

  #include <Servo.h> 

  
char val;



Servo myservo1, myservo2, myservo3, myservo4;

int bluetoothTx = 0;
int bluetoothRx = 1;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(12, OUTPUT);
  myservo1.attach(5);
  myservo2.attach(6);
  myservo3.attach(10);
  myservo4.attach(11);
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);


  Serial.println("It's me robot"); 
}

void loop()
{
  //Read from bluetooth and write to usb serial
  if(bluetooth.available()>= 2 )
  {
    unsigned int servopos = bluetooth.read();
    unsigned int servopos1 = bluetooth.read();
    unsigned int realservo = (servopos1 *256) + servopos; 
    Serial.println(realservo); 
    
    if (realservo >= 1000 && realservo <1180){
    int servo1 = realservo;
    servo1 = map(servo1, 1000,1180,0,180);
    myservo1.write(servo1);
         digitalWrite(2, LOW);
         digitalWrite(7, LOW);
         digitalWrite(3, LOW);
         digitalWrite(8, LOW);
         digitalWrite(12, LOW);
    Serial.println("servo 1 ON");
    delay(20);

    }
    
    if (realservo >=2000 && realservo <2180){
      int servo2 = realservo;
      servo2 = map(servo2,2000,2180,0,180);
      myservo2.write(servo2);
          digitalWrite(2, LOW);
          digitalWrite(7, LOW);
          digitalWrite(3, LOW);
          digitalWrite(8, LOW);
          digitalWrite(12, LOW);
      Serial.println("servo 2 On");
      delay(20);
      
    }
    
    if (realservo >=3000 && realservo < 3180){
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180,0,180);
      myservo3.write(servo3);

          digitalWrite(2, LOW);
          digitalWrite(7, LOW);
          digitalWrite(3, LOW);
          digitalWrite(8, LOW);
          digitalWrite(12, LOW);
      Serial.println("servo 3 On");
      delay(20);

    }

        if (realservo >=4000 && realservo < 4180){
      int servo4 = realservo;
      servo4 = map(servo4, 4000, 4180,0,180);
      myservo4.write(servo4);
          digitalWrite(2, LOW);
          digitalWrite(7, LOW);
          digitalWrite(3, LOW);
          digitalWrite(8, LOW);
          digitalWrite(12, LOW);
      Serial.println("servo 4 On");
      delay(20);

    } }
int appdata;


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

  if( val == 'f' )               // if 'a' was received led 2 is switched off
  {
    digitalWrite(2, HIGH);
    digitalWrite(7, HIGH);
    
  }
 
  if( val == 'F' )               // if 'A' was received led 2 on
  {
    digitalWrite(2, LOW);
    digitalWrite(7, LOW);

  }

  if( val == 'b' )              
  {
    digitalWrite(3, HIGH);
    digitalWrite(8, HIGH);
 
  }
 
  if( val == 'B' )            
  {
    digitalWrite(3, LOW);
    digitalWrite(8, LOW);
 
  
  } 

  if( val == 'l' )               // if 'C' was received led 2 on for 1 second
  {
 digitalWrite(2, HIGH);
    digitalWrite(8, HIGH);
 
  }

  if( val == 'L' )               // if 'D' was received led 3 on for 1 second
  {
    digitalWrite(2, LOW);
    digitalWrite(8, LOW);

  }
 
  if( val == 'r' )               // if 'E' was received led 2 on for 5 seconds
  {
    digitalWrite(3, HIGH);
    digitalWrite(7, HIGH);

  }
 
  if( val == 'R' )               // if 'F' was received led 3 on for 5 seconds
  {
    digitalWrite(3, LOW);
    digitalWrite(7, LOW);
 
  }
  
  if( val == 'c' )               // if 'F' was received led 3 on for 5 seconds
  {
    digitalWrite(12, HIGH);
    
 
  }

  if( val == 'C' )               // if 'F' was received led 3 on for 5 seconds
  {
    digitalWrite(12, LOW);
    
 
  }


  }

They are goiging to yap at you for not using code tags,

Like this :

  #include <SoftwareSerial.h>

  #include <Servo.h> 

  
char val;



Servo myservo1, myservo2, myservo3, myservo4;

int bluetoothTx = 0;
int bluetoothRx = 1;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(12, OUTPUT);
  myservo1.attach(5);
  myservo2.attach(6);
  myservo3.attach(10);
  myservo4.attach(11);
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(9600);


  Serial.println("It's me robot"); 
}

void loop()
{
  //Read from bluetooth and write to usb serial
  if(bluetooth.available()>= 2 )
  {
    unsigned int servopos = bluetooth.read();
    unsigned int servopos1 = bluetooth.read();
    unsigned int realservo = (servopos1 *256) + servopos; 
    Serial.println(realservo); 
    
    if (realservo >= 1000 && realservo <1180){
    int servo1 = realservo;
    servo1 = map(servo1, 1000,1180,0,180);
    myservo1.write(servo1);
         digitalWrite(2, LOW);
         digitalWrite(7, LOW);
         digitalWrite(3, LOW);
         digitalWrite(8, LOW);
         digitalWrite(12, LOW);
    Serial.println("servo 1 ON");
    delay(20);

    }
    
    if (realservo >=2000 && realservo <2180){
      int servo2 = realservo;
      servo2 = map(servo2,2000,2180,0,180);
      myservo2.write(servo2);
          digitalWrite(2, LOW);
          digitalWrite(7, LOW);
          digitalWrite(3, LOW);
          digitalWrite(8, LOW);
          digitalWrite(12, LOW);
      Serial.println("servo 2 On");
      delay(20);
      
    }
    
    if (realservo >=3000 && realservo < 3180){
      int servo3 = realservo;
      servo3 = map(servo3, 3000, 3180,0,180);
      myservo3.write(servo3);

          digitalWrite(2, LOW);
          digitalWrite(7, LOW);
          digitalWrite(3, LOW);
          digitalWrite(8, LOW);
          digitalWrite(12, LOW);
      Serial.println("servo 3 On");
      delay(20);

    }

        if (realservo >=4000 && realservo < 4180){
      int servo4 = realservo;
      servo4 = map(servo4, 4000, 4180,0,180);
      myservo4.write(servo4);
          digitalWrite(2, LOW);
          digitalWrite(7, LOW);
          digitalWrite(3, LOW);
          digitalWrite(8, LOW);
          digitalWrite(12, LOW);
      Serial.println("servo 4 On");
      delay(20);

    } }
int appdata;


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

  if( val == 'f' )               // if 'a' was received led 2 is switched off
  {
    digitalWrite(2, HIGH);
    digitalWrite(7, HIGH);
    
  }
 
  if( val == 'F' )               // if 'A' was received led 2 on
  {
    digitalWrite(2, LOW);
    digitalWrite(7, LOW);

  }

  if( val == 'b' )              
  {
    digitalWrite(3, HIGH);
    digitalWrite(8, HIGH);
 
  }
 
  if( val == 'B' )            
  {
    digitalWrite(3, LOW);
    digitalWrite(8, LOW);
 
  
  } 

  if( val == 'l' )               // if 'C' was received led 2 on for 1 second
  {
 digitalWrite(2, HIGH);
    digitalWrite(8, HIGH);
 
  }

  if( val == 'L' )               // if 'D' was received led 3 on for 1 second
  {
    digitalWrite(2, LOW);
    digitalWrite(8, LOW);

  }
 
  if( val == 'r' )               // if 'E' was received led 2 on for 5 seconds
  {
    digitalWrite(3, HIGH);
    digitalWrite(7, HIGH);

  }
 
  if( val == 'R' )               // if 'F' was received led 3 on for 5 seconds
  {
    digitalWrite(3, LOW);
    digitalWrite(7, LOW);
 
  }
  
  if( val == 'c' )               // if 'F' was received led 3 on for 5 seconds
  {
    digitalWrite(12, HIGH);
    
 
  }

  if( val == 'C' )               // if 'F' was received led 3 on for 5 seconds
  {
    digitalWrite(12, LOW);
    
 
  }


  }

First thing I’d try is to get rid of your delay(); calls. They tend to bite you when merging things. Delay over here while you miss something over there kind of thing.

-jim lee

First of all Thanks for the tip.

Tried to use it without without delay() , worked well but still same problem how to setup OAR sketch and merge it with main sketch.

Well, it looks like your missing some bits in the code you posted.

if( Serial.available() )       // if data is available to read
  {
    ;
  }

Looks to me like if Serial.available() is true, you do nothing. And, if its false, you do nothing. Is this what you wanted?

 if( val == 'C' )               // if 'F' was received led 3 on for 5 seconds
  {
    digitalWrite(12, LOW);
    
 
  }

Assuming this turns on LED 3, where do you turn it off? And where/how are you burning off 5 seconds?

-jim lee

int bluetoothTx = 0;
int bluetoothRx = 1;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

You can NOT possibly do hardware serial and software serial on the same set of pins at the same time.

Jimlee PLEASE IGNORE ALL THE COMMENTS as i jave copied my previous code, edited it and forget to remove comments.

Pauls thanks for information but still is there any other way to do that.

All i want is when i would send 'a' to arduino Obstacle avoiding feature will start and when i would send 'm' it should stop and the control of robot will be manual via smartphone.

Already tried to use switch case and if statement but failed.

All i want is when i would send 'a' to arduino Obstacle avoiding feature will start and when i would send 'm' it should stop and the control of robot will be manual via smartphone.

How are you sending this data? From what? If you are connecting the bluetooth device to pins 0 and 1, use Serial to talk to it, and forget about trying to talk to the PC. If you need to also talk to the PC, get the bluetooth device off of the hardware serial pins.

I am trying to talk via Bluetooth device.

I thinking if would put software serial Bluetooth and ultrasonic both in separate case using switch and activate as per wish.
Would this be possible n successful.

You don't need softwareSerial to do serial on pins 0 and 1 since there is a hardware serial port connected to those pins that regular Serial uses!

How do you wire BT and sonar to the same pins and not get both trying to respond to signals that you intend to go to either? At the least, you need more pins to turn each device on and off circuit and more parts to the circuit and more code to manage that kludge.

What are you using all the other pins for that you can't spare pins for each device?

I am connecting sonar to analog input pins and Bluetooth module to 0,1 input /output pin.
After connecting all the device only 1 I/O pin left spare.

I am connecting sonar to analog input pins and Bluetooth module to 0,1 input /output pin.

Those are the hardware serial pins, so do NOT use SoftwareSerial.

Adi01:
I am connecting sonar to analog input pins and Bluetooth module to 0,1 input /output pin.
After connecting all the device only 1 I/O pin left spare.

UNO analog pins can all be used as digital IO pins.

I don't think that you will learn enough to get this working in 2 days. If you had two weeks and wanted to go at it hard then maybe but Adi, you use if() but don't seem to know about else, or arrays.

Can you get an extension? If only a short one you could try the Gigs and Collaborations section and hire someone to duplicate your robot and get the code worked out.

Even if I was at your place, I couldn't be sure to have it all working right in under 24 work hours and I'm experienced at this. To get it mostly right, not so long but mostly isn't enough and debugging is a bear.