NEED failsafe. Wireless xbee RC controller

newbie.

I made a wireless controller with 2 pro minis and 2 xbees that just send 1 pot signal to power a motor Here's the basic code for the Receiver:

#include <Servo.h>

int servopin = 11;
Servo myservo;

void setup(){
Serial.begin(9600);
myservo.attach(servopin);
}

void loop(){
if (Serial.available()){
int servoAng = Serial.read();
myservo.write(servoAng);
}
}

Now, i have the Transmitting arduino mapped from 0 to 180. However it is a trigger with a spring so 0 is Full brake and 180 is full throttle and netural is in the 60's i believe. Also should know that I am using "serial.WRITE()" so the values aren't actually numbers just to be clear. I need to add a failsafe in that code so that if the wires disconnect/battery dies/out of range the receiver doesn't continue reading the last value (which 80 percent of the time is throttle). Everything I've tried seemed to fail and I'm not sure what exactly i need to add. Something like IF wireless connection is available read the pot ELSE disengage the servopin

How often does the transmitting end send data? The receiver could note the time the last data was received, and if it goes for maybe three to five times the usual transmission interval (or whatever constitutes safe), it could shut down.

PS: How to use this forum - please read - Website and Forum - Arduino Forum

hey! I believe i added a 200ms delay. However, I really don't even know how to go about adding that. Someone was talking about millis() but I don't even know how I'd apply it... any info would help.

thanks

Why don't you give it a shot. Google up some Arduino tutorials.

Believe me. I've googled. this was a last resort. It took me about 3 weeks of trial and error to get the code above working. Mainly because, being the first time using XBee i find out after the first week that one of the 2 wasn't even working. I shouldn't of started my programming interest with wireless communication but i wanted to get this thing completed...

It is impossible to find information on what I'm doing that is easy to understand. Even the arduino tutorial page uses millis for something that isn't going to work for serial.communication.

Id be cool if someone could just add what i need cause nothing works and i've spend 20+ hours on this one thing; which honestly doesn't seem to be that hard.

i just need to add something a long the lines of IF wireless communication is BROKEN then dettach MOTORpin. because there is ALWAYS a serial communication. I'm so lost and have tried everything i could,

#include <Servo.h>

int servopin = 11;
Servo myservo;

void setup(){
  Serial.begin(9600);
  myservo.attach(servopin);
}

void loop(){
if (Serial.available()){
  int servoAng = Serial.read();
  myservo.write(servoAng);
  
  
}
}

Compiles OK but not tested, testing is left as an exercise for the reader :smiley:

#include <Servo.h>

#define FAILSAFE_MS 1000    //stop sketch if no data received in this many milliseconds

int servopin = 11;
Servo myservo;

void setup()
{
    Serial.begin(9600);
    myservo.attach(servopin);
}

void loop()
{
    static unsigned long msLastRX;        //time of most recent reception
    
    if (Serial.available()){
        int servoAng = Serial.read();
        myservo.write(servoAng);
        msLastRX = millis();
    }
    
    if (millis() - msLastRX >= FAILSAFE_MS) {
        //do whatever you need here
        while (1);        //stops the sketch
    }
}

Thank you!! having just the one arduino tutorial example of millis() doesn't help but looking at what you did and the example actually got me to understand it! However, I am now experiencing a whole new problem. I'm not sure but I do not think it is related.

So here's the code:

#include <Servo.h>


int servopin = 11;
Servo myservo;


long FAILSAFE = 1000;


void setup()
{
    Serial.begin(9600);
    myservo.attach(servopin);
}

void loop()
{
    int now = millis();
    unsigned long lastRX;
    
    if (Serial.available()){
        int servoAng = Serial.read();
        myservo.write(servoAng);
        lastRX = now;
    }
     if (now - lastRX > FAILSAFE) {
        myservo.write(80); // servo.detach resulted in sudden break. 80 is neutral.
       
        
    }
}

Everything worked great for 15-20 minutes. Then the TX DOUT LED faded out and coms stopped. I disconnected/reconnected battery and it worked for 10 seconds. disconnected, waited 5 minutes and it worked for 60 seconds. Lights seem to work [forever] when RX side is off. Worst part is when LED fades{coms stops] the Motor REVs real fast followed by the failsafe...

millis() returns an unsigned long data type. So all the related variables need to be unsigned long. So,

unsigned long FAILSAFE = 1000;
    unsigned long now = millis();

Secondly, when the failsafe condition occurs, we want to stop the sketch. The "while (1)" does this, it puts the processor into a tight loop so that it is basically doing nothing and a reset is required to restart:

     if (now - lastRX > FAILSAFE) {
        myservo.write(80); // servo.detach resulted in sudden break. 80 is neutral.
        while (1);         //hang the sketch here, press reset to restart.       
    }

Is the While(1); really needed? Because it means i have to shut of the ESC and the Controller every time connection is lost. Without it i just reconnected the controller side?

Lastly, Is there any reason why new stuff is sucking the life out of my 9v batteries. Before they'd last for days and now it seems a 9v won't last for more than 20 minutes...

sw33n3y:
Is the While(1); really needed? Because it means i have to shut of the ESC and the Controller every time connection is lost. Without it i just reconnected the controller side?

I guess it depends on what you want to happen for the failsafe condition. I assumed everything should just stop, but I can see where you're coming from. If it works to your satisfaction without it, then I guess all is well.

Lastly, Is there any reason why new stuff is sucking the life out of my 9v batteries. Before they'd last for days and now it seems a 9v won't last for more than 20 minutes...

Pretty hard to say without seeing a circuit (not sure what "new stuff" is). Those rectangular 9V batteries don't have much capacity. They're usually at the bottom of the list of desirable power sources. If they're used to power an Arduino board, the first thing that happens is that the voltage regulator wastes nearly 45% of their power as heat.