1

Topic: The GUI inside the app disconnects and stops working

I made the robotcar project using the one in the projecthub tutorial:https://create.arduino.cc/projecthub/Re … ;offset=17 and using the joystick I have expected the wheels will but it didn't, it will budge but only just for a very small bit then the app will say "stopped working" or something. I have tried altering the values in the parameter of the wheel function inside the void loop and I made it move but after a while it just stops working again. There are also times where I could just toy with the joystick for about 2mins before it stops working. This is a bit annoying and considering that I already bought the app I hope someone could help me hmm and tell me what could be the problem? The code I used is listed below:

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

/* RemoteXY select connection mode and include library */  
#define REMOTEXY_MODE__SOFTWARESERIAL  
#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  */   
unsigned char RemoteXY_CONF[] =  
  { 3,0,23,0,1,5,5,15,41,11 
  ,43,43,1,2,0,6,5,27,11,5 
  ,79,78,0,79,70,70,0 };  
    
/* this structure defines all the variables of your control interface */   
struct {  

    /* input variable */ 
  signed char joystick_1_x; /* =-100..100 x-coordinate joystick position */ 
  signed char joystick_1_y; /* =-100..100 y-coordinate joystick position */ 
  unsigned char switch_1; /* =1 if switch ON and =0 if OFF */ 

    /* other variable */ 
  unsigned char connect_flag;  /* =1 if wire connected, else =0 */ 

} RemoteXY;  

/////////////////////////////////////////////  
//           END RemoteXY include          //  
/////////////////////////////////////////////  
/* defined the right motor control pins */ 
#define PIN_MOTOR_RIGHT_UP 7 
#define PIN_MOTOR_RIGHT_DN 6 
#define PIN_MOTOR_RIGHT_SPEED 10 

/* defined the left motor control pins */ 
#define PIN_MOTOR_LEFT_UP 5 
#define PIN_MOTOR_LEFT_DN 4 
#define PIN_MOTOR_LEFT_SPEED 9 

/* defined the LED pin */ 
#define PIN_LED 13 


/* defined two arrays with a list of pins for each motor */ 
unsigned char RightMotor[3] =  
  {PIN_MOTOR_RIGHT_UP, PIN_MOTOR_RIGHT_DN, PIN_MOTOR_RIGHT_SPEED}; 
unsigned char LeftMotor[3] =  
  {PIN_MOTOR_LEFT_UP, PIN_MOTOR_LEFT_DN, PIN_MOTOR_LEFT_SPEED}; 

/* 
   speed control of the motor 
   motor - pointer to an array of pins 
   v - motor speed can be set from -100 to 100 
*/ 
void Wheel (unsigned char * motor, int v) 
{ 
  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); 
  } 
} 

void setup() 
{ 
  /* initialization pins */ 
  pinMode (PIN_MOTOR_RIGHT_UP, OUTPUT); 
  pinMode (PIN_MOTOR_RIGHT_DN, OUTPUT); 
  pinMode (PIN_MOTOR_LEFT_UP, OUTPUT); 
  pinMode (PIN_MOTOR_LEFT_DN, OUTPUT); 
  pinMode (PIN_LED, OUTPUT); 

  /* initialization module RemoteXY */ 
  RemoteXY_Init (); 

} 

void loop() 
{ 
  /* event handler module RemoteXY */ 
  RemoteXY_Handler (); 

  /* manage LED pin */ 
  digitalWrite (PIN_LED, (RemoteXY.switch_1==0)?LOW:HIGH); 

  /* manage the right motor */ 
  Wheel (RightMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x); 
  /* manage the left motor */ 
  Wheel (LeftMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x); 
} 

2

Re: The GUI inside the app disconnects and stops working

The actual code I use is this:

// RemoteXY select connection mo.de 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,4,0,2,0,128,0,8,8,0,
  2,0,9,10,18,8,2,26,31,31,
  79,78,0,79,70,70,0,5,16,53,
  13,42,42,2,26,31,4,0,6,26,
  10,30,2,26,129,0,56,4,35,4,
  13,68,105,114,101,99,116,105,111,110,
  97,108,32,67,111,110,116,114,111,108,
  0,129,0,11,5,14,4,13,67,111,
  110,116,114,111,108,0,129,0,5,25,
  12,4,13,83,112,101,101,100,0,65,
  4,30,11,5,5,66,0,28,31,14,
  25,2,26,129,0,20,25,30,4,13,
  68,105,115,116,97,110,99,101,32,71,
  97,117,103,101,0 };
  
// this structure defines all the variables of your control interface 
struct {

    // input variable
  uint8_t switch_1; // =1 if switch ON and =0 if OFF 
  int8_t joystick_1_x; // =-100..100 x-coordinate joystick position 
  int8_t joystick_1_y; // =-100..100 y-coordinate joystick position 
  int8_t slider_1; // =0..100 slider position 

    // output variable
  uint8_t led_1_r; // =0..255 LED Red brightness 
  int8_t level_1; // =0..100 level position 

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

} RemoteXY;
#pragma pack(pop)

int ddist=0, dist=0;

/////////////////////////////////////////////
//           END RemoteXY include          //
/////////////////////////////////////////////
/* defined the right motor control pins */ 
#define PIN_MOTOR_RIGHT_UP 7 
#define PIN_MOTOR_RIGHT_DN 6 
#define PIN_MOTOR_RIGHT_SPEED 10 

/* defined the left motor control pins */ 
#define PIN_MOTOR_LEFT_UP 5 
#define PIN_MOTOR_LEFT_DN 4 
#define PIN_MOTOR_LEFT_SPEED 9 

#define PIN_LED 13
#define TRIG A0
#define ECHO A1

/* defined two arrays with a list of pins for each motor */ 
unsigned char RightMotor[3] =  
  {PIN_MOTOR_RIGHT_UP, PIN_MOTOR_RIGHT_DN, PIN_MOTOR_RIGHT_SPEED}; 
unsigned char LeftMotor[3] =  
  {PIN_MOTOR_LEFT_DN, PIN_MOTOR_LEFT_UP, PIN_MOTOR_LEFT_SPEED}; 

/* 
   speed control of the motor 
   motor - pointer to an array of pins 
   v - motor speed can be set from -100 to 100 
*/ 
void Wheel (unsigned char * motor, int v) 
{ 
  //if (v>100) v=100; 
  //if (v<-100) v=-100; 
  //if (dist>=60) RemoteXY.slider_1=0;
  if (v>0) { 
    digitalWrite(motor[0], HIGH); 
    digitalWrite(motor[1], LOW); 
    analogWrite(motor[2], RemoteXY.slider_1); //v*2.55
  } 
  else if (v<0) { 
    digitalWrite(motor[0], LOW); 
    digitalWrite(motor[1], HIGH); 
    analogWrite(motor[2], RemoteXY.slider_1); //(-v)*2.55
  } 
  else { 
    digitalWrite(motor[0], LOW); 
    digitalWrite(motor[1], LOW); 
    analogWrite(motor[2], 0); 
  } 
} 

void setup() 
{
  /* initialization pins */
  pinMode (PIN_MOTOR_RIGHT_UP, OUTPUT); 
  pinMode (PIN_MOTOR_RIGHT_DN, OUTPUT); 
  pinMode (PIN_MOTOR_LEFT_UP, OUTPUT); 
  pinMode (PIN_MOTOR_LEFT_DN, OUTPUT);
  pinMode(PIN_LED, OUTPUT);
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  /* initialization module RemoteXY */
  RemoteXY_Init(); 
  // TODO you setup code
}

void loop() 
{ 
  /* event handler module RemoteXY*/
  RemoteXY_Handler();

  /* manage LED pin*/
  //digitalWrite(PIN_LED, (RemoteXY.switch_1==0)?LOW:HIGH);

  if (RemoteXY.switch_1==0) {
    /* LED in arduino*/
    digitalWrite(PIN_LED, LOW);
    /* LED in app */
    RemoteXY.led_1_r = 0;
  }
  else {
    /* LED in arduino*/
    digitalWrite(PIN_LED, HIGH);
    /* LED in app */
    RemoteXY.led_1_r = 255;
    /* senses the distance from obstacles */
    getDistance();
    /* manage the right motor */ 
    Wheel (RightMotor, RemoteXY.joystick_1_y - RemoteXY.joystick_1_x); 
    /* manage the left motor */ 
    Wheel (LeftMotor, RemoteXY.joystick_1_y + RemoteXY.joystick_1_x);
  }
  // TODO you loop code
  // use the RemoteXY structure for data transfer
}

void getDistance() 
{
  digitalWrite(TRIG,LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG,HIGH);
  delayMicroseconds(2);
  digitalWrite(TRIG,LOW);
  dist=pulseIn(ECHO,HIGH);
  dist=dist*.0178;
  if(dist>20){
    RemoteXY.level_1 = 0;
  }
  else {
    dist = ((((dist/0.2)-100)*(-1))+10);
    //ddist = dist;
    if (dist>=80) dist+=20;
    RemoteXY.level_1 = dist;
  }
  delay(10);
}

3 (edited by Guillaume 2018-06-16 17:45:49)

Re: The GUI inside the app disconnects and stops working

Welcome smile

I think your problem is caused by those delays. Every loop(), you call getDistance(), and this function will block code for more than 10 ms. This mean that your arduino spend most of its processing power doing nothing but waiting. At least remove delay(10) and use the method shown in Blink Without Delay example if you want to wait a certain time between two calls to getDistance().