Software reset in Arduino


To avoid runaway tasks in MRLComm if MRL is closed, this code could be exected when MRLComm detects that the contact with MRL has been lost.

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

resetFunc();  // Call the reset 
It will then end up in the initial setup() and reinitiate. So all pins will be reste to their initial state and the communicaiton will go into it's initial state.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Mats's picture

PID in MRLComm

I like the idea of adding to PID to MRLComm. Libraries already exist, so no need to do the math again.

It would also be nice to be abe to get the analog values published, or an event triggred when the servo has reached the desired position.  

Today we can only tell the servos where to go, but don't know when they have reached the desired position. 

kwatters's picture

PID / and motor reset

Hi Mats,

  I think adding the pid functionality to MRLComm will be pretty straight forward.  The math isn't actually all that difficult.  It's a very well documented equation / system, and as you point out, there are already libraries for it.

  I have a question about your "reset" function.  I tried adding it into MRLComm, but I don't seem to be able to get it to invoke when the serial port is closed.  I was hoping that I could check  if (!Serial)  and invoke reset, but it doesn't seem to notice when the port is closed.

  Ultimately, the goal is that if MRL shuts down (unexpectedly) that the motor controllers should stop moving.  As it is now, if the motor is moving at "1.0"  (full steam ahead)  and MRL closes, the motor will keep going at full power forward.  This isn't quite what we want i believe. 

  On a side note,  there's another advantage to having the PID + motor + arduino + potentiometer system.  If the potentiometer is wired backwards you can fix it in software!  :) 

  Currently, there's an issue when using Type2PWM motor controllers that it pretty quickly will overrun the arduino and you'll start getting errored out messages back from it.  So, before adding any additional logic into the MRLComm,  I have been working on making it so that every message that is sent to the arduino must return an acknowledgement message.  

  I have it working for the servo case, and it seems to work well, it also allowed me to move the Arduino to 115.2kbps  (doubling the speed)  (I'm not sure what the max speed is for the serial port, but I suspect (by reading some forums) that we could go much faster than the 115.2kbps.

  Anyway,  I'm hoping to make more progress now on getting the motor control messages working without the need for a delay between sending them.









Mats's picture

Doh, if (!Serial) no worky on Uno and Mega

Hi Kevin

if (!Serial) only works on Leonardo boards. All other will return true.

So some other method is needed to figure out if the serial communication is alive or not. Pehaps a heartbeat message and a timeout every second or something similar.

I found a post on max communicaton speed, and the conclusion is that you can specify up to 2000kbps, but the Arduino will never be able to push more than 500kbs thru, unless we rewrite the serial library

That post also has a link to an AVR library that does CRC calculation. It may be needed of we turn up communication speed.



Mats's picture

MRLComm Setup()

Hi Kevin

I know that you are working hard on Ardino/Serial and MRLComm so I don't want to change and push anything in them now, so I write a little here about what I have tested and some code that you may want to use in MRLComm.c

The idea behind the changes I suggest and is that Arduino will always execute as long as it has power, but MRL can be started and stopped many times. So the idea is that MRLComm should stay in the setup() until it receives the first MAGIC_NUMBER from MRL on the serial port. It should also have something to indicate that it's waiting in the setup(). So I use the LED on pin13 to blink while MRLComm is waiting in the setup(). The LED on pin 13 can still be used for other purposes from MRL. It only blinks while MRL is disconnected.

int ledPin = 13;
int ledLoop = 0;
void setup() {
  pinMode(ledPin,OUTPUT); // Enable LED on PIN 13 
  while (!Serial){}; // Wait for the USB port to be avaliable on 32u4 based boards.
  // TODO: higher port speeds?
  Serial.begin(115200);        // connect to the serial port
  Serial.flush(); // Flush anything already in the write buffer
  // Wait until the first MAGIC_NUMBER has become availabe on the serial port
  while (Serial.peek() != MAGIC_NUMBER){  
    newByte =; // Throw away anything that isn't a MAGIC_NUMBER
    ledLoop++; // Will turn around at 32767 to -32768
    // Flash the LED on pin 13 while waiting in setup()
    if (ledLoop > 0){
      digitalWrite(ledPin, HIGH);
    else { 
      digitalWrite(ledPin, LOW);
  digitalWrite(ledPin, LOW);   
I notice that MRLComm is reset when the serial port opens, so that may be one initiation problem. The serial port needs to be open for about a second before any other commnication can start. For some reason MRLComm also gets reset sometimes for no reason. I guess that Windows is doing something strange,because it's not a problem I have on the Raspberry or when I power the Arduino with a USB powerbank on the USB port.