10 years, 4 months ago.

using functions

I am trying to pass a value to a function when an interrupt occurs, I've checked related post's the closest i found mentioned using void name_of_function (void) i have tried that as well, it is returning errors, i've tried having the values to be passed in brackets, it wasn't helpful. Could anyone please point me in the right direction to solve this.

Thanks in advance and happy holidays!!!

  1. include "mbed.h"

InterruptIn right(p14); InterruptIn left(p13); DigitalOut aEnable(p23); DigitalOut bEnable(p26);

PwmOut a1(p21); PwmOut a2(p22); PwmOut b1(p24); PwmOut b2(p25);

int direction = 1;

void drive(void) { if(direction == 1) { a1.write(0.5); a2.write(0); b1.write(0.5); b2.write(0); }

if(direction == 0) {

a1.write(0); a2.write(0.5); b1.write(0); b2.write(0.5); } }

int main() {

bEnable.write(1); aEnable.write(1); while(1) { right.rise(&drive, 1); left.rise(&drive, 0);

} }

5 Answers

10 years, 4 months ago.

It's very difficult to read your code. Use new line after semicolons and post it using the code function (under editing tips)

This is much cleaner:

your program

#include "mbed.h"
InterruptIn right(p14); 
InterruptIn left(p13); 
DigitalOut aEnable(p23); 
DigitalOut bEnable(p26);

PwmOut a1(p21); 
PwmOut a2(p22); 
PwmOut b1(p24); 
PwmOut b2(p25);

int direction = 1;

void drive(void) { 
  if(direction == 1) { 
    a1.write(0.5); 
    a2.write(0); 
    b1.write(0.5); 
    b2.write(0); 
  }
  if(direction == 0) {
    a1.write(0); 
    a2.write(0.5); 
    b1.write(0); 
    b2.write(0.5); 
  } 
}

int main() {
  bEnable.write(1);
  aEnable.write(1); 
  while(1) { 
    right.rise(&drive, 1); 
    left.rise(&drive, 0);
  }
}

It is also difficult to help without you posting your compiler error messages.

One quick tip, I don't think you can use the rise function with any variables or what ever you are trying to do there: right.rise(&drive, 1);

It needs to be right.rise(&drive);

10 years, 4 months ago.

Mainly what Tomas said. If you have your own library you can do it (although it isn't really trivial), but it is not possible with the mbed library interrupts, then you just need to have a function that takes nor arguments and returns no arguments.

10 years, 4 months ago.

If you don't mind using global vars, then set one before the entry and set another when you leave, dirty but easy.

Dave.

10 years, 4 months ago.

In this case you dont even need global vars. There are only two options for the 'direction' variable and each option is lnked to a specific interrupt pin (left or right). Also note that you dont need to re-attach the interrupt again and again in the while loop. That should be done only once before the infinite loop starts...

#include "mbed.h"
InterruptIn right(p14); 
InterruptIn left(p13); 
DigitalOut aEnable(p23); 
DigitalOut bEnable(p26);
 
PwmOut a1(p21); 
PwmOut a2(p22); 
PwmOut b1(p24); 
PwmOut b2(p25);
 
void drive_right(void) { 
    a1.write(0.5); 
    a2.write(0); 
    b1.write(0.5); 
    b2.write(0); 
 }

void drive_left(void) { 
    a1.write(0); 
    a2.write(0.5); 
    b1.write(0); 
    b2.write(0.5); 
}
 
int main() {
  bEnable.write(1);
  aEnable.write(1); 

  right.rise(&drive_right); 
  left.rise(&drive_left);

  while(1) { 
    wait(0.5);
  }
}

Donald K
poster
10 years, 4 months ago.

Thank you guys, you are all legends. your input is very helpful.