Main Site Documentation

Accessing variables


#1

I have one routine that defines an LED & sets it. Another routine needs to make an LED adjustment, but the name (redled) is not recognized there (a scope problem). I know I can define it outside of both routines, but then it seems EVERY control, ports, etc would have to be defined at “the top” of the program—is that the proper way to go about it? most books do not address this, they only show one routine…I need to control leds, ports, etc from several routines.

        public static void ControlPanelSetup()
        {  //  code
            // more code
            // more code here
            OutputPort redLED = new OutputPort((Cpu.Pin)FEZ_Pin.Digital.LED, false);
            // more code
            redLED.Write(true);  //turn led on
            //more code
            //more code
        }

        public static void AdjustLeds()
        {
            //code
            //code
            redled.Write(false);  // turn off led ERROR HERE redled unknown 
            // more code
        }

#2

your assumption is correct about making variables available globally. in your case, they would also have to be static.


#3

So every pin that is monitored or written by more than one routine must be defined at the top of the program using static.

I thought that was true, but just checking my sanity. It was never really stated clearly.