Go Down

Topic: Branch To Subroutine (Read 1 time) previous topic - next topic


May 06, 2012, 05:43 pm Last Edit: May 06, 2012, 08:30 pm by njdurkin Reason: 1
I need to branch to a subroutine.

Code: [Select]
if (resetP == 1) {
 resetP = 0;

The subroutine is positionReset. I have it as:

void positionReset() {

and it is placed at the end after all my coding. My sketch is attached. I don't think it branches.


May 06, 2012, 05:57 pm Last Edit: May 06, 2012, 06:02 pm by Msquare Reason: 1
Subroutines are called, not branched to.

There is nothing in your description as to why it should not be called.

Looking inthe attached code, I am missing a few #includes (or the other *.cpp in your directory) to get it to compile. That way I could see if you have mismatched a bracket or two.

You only go into your reset code if the interrupt on pin 0 triggers. Which never happens as I mention further down.


Code: [Select]

else if (mval != 1,2,4,8,16,32,64,128) {
 Errorflag = 1;

This is almost certainly NOT doing what you think it is doing.

Since you have already checked mval against those values you don't need to check again.  Change it to:
Code: [Select]

else {
 Errorflag = 1;
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


On a standard Arduino pin 0 will not work as an interrupt, only pin 2 and 3. So your interrupt is never activated, never setting resetP to 1, thus never satisfying the if to call your subroutine.


On a standard Arduino pin 0 will not work as an interrupt, only pin 2 and 3. So your interrupt is never activated, never setting resetP to 1, thus never satisfying the if to call your subroutine.

Interrupt 0 is on pin 2.  attachInterrupt() uses the interrupt number, not the pin number.

The code uses Serial3 so it's running on an Arduino Mega of some kind.  That means interrupts 0-5 are available from pins 2, 3, 21, 20, 19, and 18 respectively.
Send Bitcoin tips to: 1G2qoGwMRXx8az71DVP1E81jShxtbSh5Hp


May 06, 2012, 08:30 pm Last Edit: May 07, 2012, 12:33 am by njdurkin Reason: 1
Thank you both for the input. I changed that last else if to just an else like you suggested. And yes, it is interrupt 0 which is pin 2 I believe.

Also, there is no additional coding. The only thing you are missing is possibly an additional tab which simply holds some temporary information for me. It is all commented out.

Can you link me to a good subroutine tutorial. I have used a microconroller before but it was much different than this. If I wanted to do what I was doing in the old micro I used, I would compare two values, so in this case I would compare the value of resetP to 1. The controller would subtract the two values and if the solution was 0, it would set a zero flag. A command placed after it would look at that flag, and if it was set, it would branch to the subroutine you defined. Run that code. Then branch back to the main program where you branched from. This is what I wanted to do.

Originally, I had all the code in the ISR but I figured I'd better get all that code out of there because it probably wasn't a good idea. Idk. But I thought when that interrupt is triggered, it branches to the ISR, runs it, and then branches back to the main program. Can't I do the same thing but instead of triggering it with an interrupt, just trigger it by condition?


You don't need to do anything to have it return from the main program, at the end of the function, it will automatically return.


From what I saw, as long as your interrupt routine is being called it should work.

From the way you describe you previous code, I feel you may have been using low level flags in the CPU to 'jump' to routines (I used to do that in assembler years ago).

You can improve readability and maintainability of your code by using a switch statement in positionReset():

switch (mval)
    case 1: PositionA=100; break;
    case 2: PositionA=200; break;
    case 128: PositionB=800; break;
    default: Errorflag = 1; break;
Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com


Thanks for the tip. That will surely clean things up a bit. And it works just as good! (:

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131