Pages: [1]   Go Down
Author Topic: Branch To Subroutine  (Read 688 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need to branch to a subroutine.

Code:
if (resetP == 1) {
  resetP = 0;
  positionReset();
}

The subroutine is positionReset. I have it as:

void positionReset() {
code
}

and it is placed at the end after all my coding. My sketch is attached. I don't think it branches.
« Last Edit: May 06, 2012, 01:30:55 pm by njdurkin » Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 25
Posts: 1133
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: May 06, 2012, 11:02:45 am by Msquare » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 178
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
else {
  Errorflag = 1;
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 25
Posts: 1133
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 178
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: May 06, 2012, 05:33:00 pm by njdurkin » Logged

California
Offline Offline
Faraday Member
**
Karma: 82
Posts: 3123
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 27
Posts: 1178
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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;
etc...
    case 128: PositionB=800; break;
    default: Errorflag = 1; break;
}
Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Jr. Member
**
Karma: 0
Posts: 54
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Pages: [1]   Go Up
Jump to: