0

I'm having an issue trying to use a normal non-member function of my main sketch file as a callback. I can't get the function pointer to invoke from a function other than it was saved. More info below;

MotorEncoder.h

typedef void (*cb)(int16_t,int16_t);
class MotorEncoder
  ...
  private:
    cb _motor_callback;

Program.ino

cb encoder_cb = NULL;
...
encoder_cb = function_that_accepts_2_int16
DC_Encoder_1.move(motor_pos, speed, motor_id, encoder_cb);

EDIT: I want to edit the description as to not confuse anyone. The code with the callbacks above is all correct. The issue was I was working with open-source software and another developer was setting the selected serial-port connection back to the bluetooth channel in-correctly. The callback was executed, I was connected to the ATMega2560 via serial connection to a raspberry pi and was looking for the callback to show there, not monitoring bluetooth so I didn't see any data come across. Document your code.

3
  • You need to post code, not describe it. Please read How do I ask a good question?. Please post a Minimal, Complete, and Verifiable example.
    – Nick Gammon
    Jan 17, 2017 at 4:56
  • I can't get the function pointer to invoke from a function other than it was saved. - I don't understand that sentence.
    – Nick Gammon
    Jan 17, 2017 at 6:27
  • I'll mock up a simple example when I have time later tonight / this week. But just to try and explain when I say that I mean I can use the function pointer inside of the "move" function and see it being called in my Program.ino file. I recieve the function pointer and can immediately use it to call back into Program.ino Program.ino calls this call later inside of it's loop function, say to function: "Function1". "Function1" checks some variable and if true then attempts the callback but I don't see any code execute back in my Program.ino file. Jan 17, 2017 at 16:55

1 Answer 1

1

This answer contains a few debugging guidelines, as I don't know what the problem is and don't see how one could tell without a good deal more of information.

I suggest creating a Minimal, Complete, and Verifiable example (MCaVe) of your code, not just snippets, so that other people can compile and run the code, thus being able to observe what happens in more detail and perhaps with added debugging output.

I'm presuming your callback is triggered as a consequence of an interrupt occurring. I suggest you write the MCaVe to depend on a pin-change interrupt, rather than a motor-controller interrupt, to allow testing on a system consisting of no more than an Arduino with a button, a serial interface, and perhaps a few LED indicators. Pressing the button would change pin voltage and cause an interrupt; your ISR would turn on an LED to signal it handled the interrupt, then trigger the callback. The callback routine would turn on another LED as its signal. The main loop would read the pin register bits for the two LEDs, record and report the times they came on, and turn them off after a second or two. If you get a simple setup like this to work, the technique probably would carry over to your motor-controller case; or if not, probably being a smaller program and setup should be easier to debug and figure out why not.

3
  • I'll give that a go, and post a simple example back here if I'm not successful. I am using an interrupt but it simply sets a volatile variable. The motor class is then called from the Program.ino loop function, checks the variables and should perform the callback if needed. I was trying to fix a larger firmware system for a group of people. I'll mock something up more simple first and see if the problem exists, Jan 17, 2017 at 17:02
  • @BruceBannor, ok; and I'd recommend posting the simple example, successful or not, as it will allow others to look for problems or improvements. +1 Jan 17, 2017 at 17:24
  • I finally had some time today to look at everything again, and I wanted to provide anyone who stumbles on this an update. I'm going to mark this as the accepted answer. Quick context; I'm working on an open-source firmware for the MakeBlock MegaPi, fixing a bunch of the calls and api's that I found broken for others in the community. Through some debugging and using the serial monitor I found where they were always setting the serial-port back to the bluetooth connection. I'll edit my answer as I'll run out of characters here. The advice here is great for any general issue though. Jan 21, 2017 at 20:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.