1 (edited by naved.the.sheikh 2018-07-31 09:08:20)

Topic: Installing IR Transceiver as Obstacle Sensor in Bluetooth Robot Car.

Hey there kind people!

I am stuck into a problem].

I am a very beginner in robotics.
I made Bluetooth Controlled Car using Arduino Uno, HC-05 Bluetooth Module and L298N Motor Driver.
I was controlling it by self customized GUI, powered by RemoteXY app via Bluetooth.

I wanted to go further ahead by installing two (left and right) IR Transceivers as Obstacle Sensors on it. Previously I had installed a Buzzer as Horn for the vehicle. And I tried to write code in a manner that the buzzer would also sound up, when a big obstacle is noticed. I needed my code to work in the way, like:-
1). If any obstruction is noticed on the left side, the car would move to the left side and again begin to take orders from the app i.e. RemoteXY via Bluetooth.
2). Similarly for the Right Side obstruction.
3). If both of the sensors would notice any Obstacle, they'll sound up the buzzer for around 1.5-2.0 seconds and then move slightly backward. Then after, the robot car would move left side and stop for again checking Obstacle. If Obstacle is noticed it will change its direction to the left of the previous state and again check for obstruction. If none of the obstruction is noticed. It will sound the buzzer for 0.5 Seconds and move a slight forward. But, if any Obstacle is noticed it will sound up the buzzer for 5 Seconds.

But my code is not working properly. Though it is compiled easily. But, when it comes to working of the robot. It goes just like garbage. I control/move it using Joystick of RemoteXY. It goes further then notices a hindrance and then stop and change its direction a little bit. Then, it stops then it makes noise of buzzer. Then whole car gets stopped and even if stops taking orders from the app.

Please Please Please Sir/Mam with great, kind and big heart modify my following code in order to get what I need.

Here is my code:-

/* 
   -- 1st Robo Car Project (2) with horn(1) -- 
    
   This source code of graphical user interface 
   has been generated automatically by RemoteXY editor.
   To compile this code using RemoteXY library 2.3.3 or later version 
   download by link http://remotexy.com/en/library/
   To connect using RemoteXY mobile app by link http://remotexy.com/en/download/                   
     - for ANDROID 4.1.1 or later version; 
     - for iOS 1.2.1 or later version; 
     
   This source code is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.    
*/ 

//////////////////////////////////////////////
//        RemoteXY include library          //
//////////////////////////////////////////////

// RemoteXY select connection mode and include library 
#define REMOTEXY_MODE__SOFTSERIAL
#include <SoftwareSerial.h> 

#include <RemoteXY.h> 

// RemoteXY connection settings  
#define REMOTEXY_SERIAL_RX 2 
#define REMOTEXY_SERIAL_TX 3 
#define REMOTEXY_SERIAL_SPEED 9600 


// RemoteXY configurate   
#pragma pack(push, 1) 
uint8_t RemoteXY_CONF[] = 
  { 255,9,0,0,0,154,0,8,228,4,
  5,48,44,26,30,30,0,31,8,1,
  6,0,-84,-142,20,20,0,2,26,2,
  1,2,1,22,11,0,135,26,16,1,
  79,78,0,79,70,70,0,129,0,5,
  24,23,8,0,64,78,97,118,101,100,
  0,131,3,51,1,20,5,1,2,31,
  82,111,98,111,116,32,67,97,114,0,
  131,0,62,5,19,5,2,2,31,67,
  111,110,116,114,111,108,108,101,114,0,
  3,3,83,12,8,22,0,2,26,129,
  0,9,32,13,7,0,16,84,72,69,
  0,129,0,4,39,25,8,0,136,83,
  104,101,105,107,104,0,1,0,-34,-111,
  12,12,1,2,31,88,0,1,4,79,
  44,12,12,0,37,151,240,159,147,162,
  0 }; 
   
// this structure defines all the variables of your control interface 
struct { 

    // input variable
  int8_t joystick_1_x; // =-100..100 x-coordinate joystick position 
  int8_t joystick_1_y; // =-100..100 y-coordinate joystick position 
  uint8_t rgb_1_r; // =0..255 Red color value 
  uint8_t rgb_1_g; // =0..255 Green color value 
  uint8_t rgb_1_b; // =0..255 Blue color value 
  uint8_t switch_1; // =1 if switch ON and =0 if OFF 
  uint8_t select_1; // =0 if select position A, =1 if position B, =2 if position C, ... 
  uint8_t button_1; // =1 if button pressed, else =0 
  uint8_t button_2; // =1 if button pressed, else =0 

    // other variable
  uint8_t connect_flag;  // =1 if wire connected, else =0 

} RemoteXY; 
#pragma pack(pop) 

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////

#define PIN_BUTTON_2 12

//define left ir obstacle sensor

#define left_obstacle_sensor 4

//define right ir obstacle sensor

#define right_obstacle_sensor 5

//define right motor control pins
#define right_motor_A 8
#define right_motor_B 9
#define right_motor_speed 11 //enable pin

//define left motor control pins
#define left_motor_A 6
#define left_motor_B 7
#define left_motor_speed 10 //enable pin

//define two arrays with a list of pins for each motor
uint8_t RightMotor[3] = {right_motor_A, right_motor_B, right_motor_speed};
uint8_t LeftMotor[3] = {left_motor_A, left_motor_B, left_motor_speed};

//speed control of motors
void Wheel (uint8_t * motor, int v) // v = motor speed, motor = pointer to an array of pins 
{
    /*  //left sensor input 
    int left_sensor_reading=digitalRead(3); 
    //Right Sensor Input 
    int right_sensor_reading=digitalRead(4); */ 
   
  if (v > 100) v=100;
  if (v < -100) v=-100;
  if (v > 0){

    digitalWrite (motor [0], HIGH);
    digitalWrite (motor [1], LOW);
    analogWrite (motor [2], v * 2.55);
  }
  else if ( v<0 ){

    digitalWrite (motor [0], LOW);
    digitalWrite (motor [1], HIGH);
    analogWrite (motor [2], (-v) * 2.55);
  }
  else{
    digitalWrite (motor [0], LOW);
    digitalWrite (motor [1], LOW);
    analogWrite (motor [2], 0);
  }
    
    /*if((left_sensor_reading==HIGH)&&(right_sensor_reading==HIGH)) 
    { 
       //Stay still
      digitalWrite(8,LOW); 
      digitalWrite(9,LOW); 
      digitalWrite(6,LOW); 
      digitalWrite(7,LOW); 
      digitalWrite(12,HIGH);
      delay(3000);
               if((left_sensor_reading==HIGH)&&(right_sensor_reading==HIGH)) {
      
      digitalWrite (motor [0], LOW);
    digitalWrite (motor [1], HIGH);
    analogWrite (motor [2], (-v) * 2.55);
            
       delay(1900);
      
     if((left_sensor_reading==LOW)||(right_sensor_reading==LOW)) 
    { digitalWrite(12, LOW);
      digitalWrite (motor [0], LOW);
    digitalWrite (motor [1], LOW);
    analogWrite (motor [2], 0);}}}*/
}


void setup() 
{
  RemoteXY_Init (); 
  
  
    pinMode (PIN_BUTTON_2, OUTPUT);
   
  
  //initialization pins
  pinMode (right_motor_A, OUTPUT);
  pinMode (right_motor_B, OUTPUT);
  pinMode (left_motor_A, OUTPUT);
  pinMode (left_motor_B, OUTPUT);
    
  //intialization of pins for ir sensors
    
  //left sensor output to arduno input 
    
    pinMode(left_obstacle_sensor, INPUT); 
    
   //Right Sensor output to arduino input   
    pinMode(right_obstacle_sensor, INPUT); 
    
  
  
}

void loop() 
{ 
  RemoteXY_Handler ();
  
  
    
  digitalWrite(PIN_BUTTON_2, (RemoteXY.button_2==0)?LOW:HIGH);
   
  //manage the right motor
  Wheel (RightMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x);
  Wheel (LeftMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x);


    

    
     //left sensor input 
    int left_sensor_reading=digitalRead(4); 
    //Right Sensor Input 
    int right_sensor_reading=digitalRead(5); 
    if((left_sensor_reading==HIGH)&&(right_sensor_reading==HIGH)) 
    { 
       //Stay still
      digitalWrite(right_motor_A,LOW); 
      digitalWrite(right_motor_B,LOW); 
      digitalWrite(left_motor_A,LOW); 
      digitalWrite(left_motor_B,LOW); 
      digitalWrite(12,HIGH);
      delay(1500);
      digitalWrite(12, LOW);
        //turns right 
             digitalWrite(right_motor_A,HIGH); 
        digitalWrite(right_motor_B,LOW); 
        digitalWrite(left_motor_A,LOW); 
        digitalWrite(left_motor_B,HIGH); 
        delay(250);
          if((left_sensor_reading==LOW)&&(right_sensor_reading==HIGH)) {
            
              //turns left 
          digitalWrite(right_motor_A,LOW); 
          digitalWrite(right_motor_B,HIGH); 
          digitalWrite(left_motor_A,HIGH); 
          digitalWrite(left_motor_B,LOW); 
          delay(500);
            
        }
        
        else{
               //Stay still
      digitalWrite(right_motor_A,LOW); 
      digitalWrite(right_motor_B,LOW); 
      digitalWrite(left_motor_A,LOW); 
      digitalWrite(left_motor_B,LOW); 
      digitalWrite(12,HIGH);
      delay(1500);
      digitalWrite(12, LOW);
        }
        
        
    } else if((left_sensor_reading==LOW)&&(right_sensor_reading==HIGH)) {
        //turns right 
        digitalWrite(right_motor_A,HIGH); 
        digitalWrite(right_motor_B,LOW); 
        digitalWrite(left_motor_A,LOW); 
        digitalWrite(left_motor_B,HIGH); 
        delay(250);
        
    } else if((left_sensor_reading==HIGH)&&(right_sensor_reading==LOW)) {
          //turns left 
          digitalWrite(right_motor_A,LOW); 
          digitalWrite(right_motor_B,HIGH); 
          digitalWrite(left_motor_A,HIGH); 
          digitalWrite(left_motor_B,LOW); 
          delay(250);
        
    } /*else if((left_sensor_reading==LOW)&&(right_sensor_reading==LOW)) {
          //Stop the Boot
         digitalWrite(8,LOW);
         digitalWrite(6,HIGH); 
         digitalWrite(7,LOW); 
         digitalWrite(9,HIGH);
    } */
    
    
    
    

}

Please Please Please Sir/Mam with great, kind and big heart and  highly enthusiasm  modify a needy beginner's aforementioned code in order to get what I need and I'll ever owe you this. .


P.S.:- Sorry if I had written bad English .

A very big thanks in advance