Automaticlly reboot/reset when connection with smartphone is lost

Hi

I control few buttons with a smartphone using http://remotexy.com interface.
I need a code to automatically reboot/reset Arduino when the connection between HC-05(06) Bluetooth module and android is lost.

Code:

/*
– New project –

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,5,0,0,0,60,0,8,13,0,
1,0,40,23,21,21,2,31,60,0,
1,0,74,22,22,22,2,31,62,0,
1,0,57,3,21,21,12,31,70,0,
1,0,6,23,21,21,35,31,66,0,
2,0,6,3,22,11,133,26,31,31,
79,78,0,79,70,70,0 };

// this structure defines all the variables of your control interface
struct {

// input variable
uint8_t button_1; // =1 if button pressed, else =0
uint8_t button_2; // =1 if button pressed, else =0
uint8_t button_3; // =1 if button pressed, else =0
uint8_t button_4; // =1 if button pressed, else =0
uint8_t switch_1; // =1 if switch ON and =0 if OFF

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

} RemoteXY;
#pragma pack(pop)

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

#define PIN_BUTTON_1 8
#define PIN_BUTTON_2 6
#define PIN_BUTTON_3 7
#define PIN_BUTTON_4 9
#define PIN_SWITCH_1 13

void setup()
{
RemoteXY_Init ();

pinMode (PIN_BUTTON_1, OUTPUT);
pinMode (PIN_BUTTON_2, OUTPUT);
pinMode (PIN_BUTTON_3, OUTPUT);
pinMode (PIN_BUTTON_4, OUTPUT);
pinMode (PIN_SWITCH_1, OUTPUT);

// TODO you setup code

}

void loop()
{
RemoteXY_Handler ();

digitalWrite(PIN_BUTTON_1, (RemoteXY.button_1==0)?LOW:HIGH);
digitalWrite(PIN_BUTTON_2, (RemoteXY.button_2==0)?LOW:HIGH);
digitalWrite(PIN_BUTTON_3, (RemoteXY.button_3==0)?LOW:HIGH);
digitalWrite(PIN_BUTTON_4, (RemoteXY.button_4==0)?LOW:HIGH);
digitalWrite(PIN_SWITCH_1, (RemoteXY.switch_1==0)?LOW:HIGH);

// TODO you loop code
// use the RemoteXY structure for data transfer

}

you could create/call a software reset routine when disconnection is detected; example of such routines below:

void software_Reset() // Restarts program from beginning { asm volatile (" jmp 0"); }

OR

void(* resetFunc) (void) = 0;//declare reset function at address 0

Why do you need a drastic reset of everything? Can't you just exit the connection state to go to the initial state?

I control my car with remote control (smartphone). When the receiver and transmitter lose the connection the car continues to go. I want it when lost connection to stop. The easiest way is to restart Arduino to the default setup.

SilvioC: I control my car with remote control (smartphone). When the receiver and transmitter lose the connection the car continues to go. I want it when lost connection to stop. The easiest way is to restart Arduino to the default setup.

When you stop a car at a red light, do you put the transmission in "Park" and turn off the engine?

aarg: Quote from: SilvioC on Today at 02:31 pm

I control my car with remote control (smartphone). When the receiver and transmitter lose the connection the car continues to go. I want it when lost connection to stop. The easiest way is to restart Arduino to the default setup.

Quote from: aarg on Today at 02:55 pm

When you stop a car at a red light, do you put the transmission in "Park" and turn off the engine?

no quite the same thing as putting in "park"...

Since its an RC car what I would suggest is to implement a fail safe where if connection is lost the car stops and re-attempts to establish connection.

But I agree with aarg; software reset is a bit of and extreme in this instance... an if-else statement in the right place would do just fine

Since its an RC car what I would suggest is to implement a fail safe where if connection is lost the car stops and re-attempts to establish connection.

I would agree with you there but there's no reason you have to reset everything to do that.