View Single Post
  #6   Spotlight this post!  
Unread 02-03-2005, 19:38
ace123's Avatar
ace123 ace123 is offline
Registered User
AKA: Patrick Horn
FRC #0008 (Paly Robotics - http://robotics.paly.net/)
Team Role: Programmer
 
Join Date: Feb 2005
Rookie Year: 2004
Location: Palo Alto, CA
Posts: 50
ace123 has a spectacular aura aboutace123 has a spectacular aura about
Send a message via AIM to ace123
Re: Static Variables

declare the variable(s) you want to use outside of a function (outside any brackets).
Then, move the extern line(s) into a .h file (user_routines.h)
The extern lines are optional, and are only needed if you want to use them in more than one file (user_routines.c and user_routines_fast.c for autonomous)

After that, you can read and write to them from any function:

user_routines.c
Code:
#include "user_routines.h"
...
...
/* Only declare and initialize global variables *once*.*/
int x=0;
int y=0;
void func1(void) {
    x=1;
}
void func2(void) {
    y=x;
}
void Default_Routine(void) {
    /* caution: do not declare the global variables (or variables with the same name) inside a function again. This may cause strange behaviour if done by accident, and will not produce an error or even a warning in most cases.*/
    // int x; <-- NO COMPILE ERROR! 
    pwm01=y+127;
    pwm02=x+127;
}
user_routines.h
Code:
...
...
/* The extern allows *global variables* declared in one file to be usable in any other file. */
extern int x;
extern int y;
void func1(void);
void func2(void);
void Default_Routine(void);
__________________
-Patrick Horn, Paly Robotics

Check out the space simulator called Vega Strike, modelled after the space simulator games Elite and Wing Commander. It's Open Source too!
If you have ever played Wing Commander, or especially Privateer, and had a feeling of nostalga derived from the you will enjoy these two Vega Strike mods: Privateer Gemini Gold and Privateer Remake!
I'm working on adding multiplayer support this year...