简体   繁体   中英

Calling a method multiple times with different values

I'm doing some code to control a locker system, in which I want to define which locker will be opened setting values to X and Y arguments (Imagining that the lockers are arranged as a matrix X,Y ). I've tested the method with a single (X,Y) values and it works fine, but when I tried to use new ones it actually updated the values. Could someone tell me what I'm doing wrong? I'm actually new to java. The code is something like that. It is the first time I'm posting here, so sorry if I did some mistake. Thanks in advance.

public class MainApp extends Application{
...
            LockerControl(3, 0);
            LockerControl(1, 1);
}

  private void LockerControl(int X,int Y) throws Exception {
            LockerControl locker = new LockerControl(); 
            locker.count(X, Y);
            Thread.sleep(10000);   
            locker.reset();
    }

public class LockerControl {

    int counter;
    boolean flagSensor;

    public void count(int X, int Y) throws Exception{

        final GpioController gpio = GpioFactory.getInstance();
        final GpioPinDigitalOutput myLed[] = {
            gpio.provisionDigitalOutputPin(RaspiPin.GPIO_04, "LED_1", PinState.LOW),
            gpio.provisionDigitalOutputPin(RaspiPin.GPIO_05, "LED_2", PinState.LOW),
            gpio.provisionDigitalOutputPin(RaspiPin.GPIO_06, "LED_3", PinState.LOW)};
        PCA9685GpioTester pca9685Gpio = new PCA9685GpioTester(); 
        GpioPinDigitalInput myButton = gpio.provisionDigitalInputPin(RaspiPin.GPIO_02);

        myButton.addListener(new GpioPinListenerDigital() {
            @Override
            public void handleGpioPinDigitalStateChangeEvent(
                    GpioPinDigitalStateChangeEvent event) {
                if (event.getState().isHigh()) {
                    if (!flagSensor) {
                        myButton.addTrigger(new GpioPulseStateTrigger(myLed[1], 1000));
                        ++counter;
                        if (counter == X+1) {
                            flagSensor = true;
                            try {
                                pca9685Gpio.on(X, Y); 
                            } catch (Exception ex) {
                                Logger.getLogger(LockerControl.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    }
                    else {
                        myButton.addTrigger(new GpioPulseStateTrigger(myLed[2], 1000));
                        --counter;
                        if (counter == 0 && flagSensor) {
                            try {
                                pca9685Gpio.off(X, Y);
                            } catch (Exception ex) {
                                Logger.getLogger(LockerControl.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                    }
                } else {
                    myButton.addTrigger(new GpioSetStateTrigger(myLed[0], PinState.LOW));
                    myButton.addTrigger(new GpioSetStateTrigger(myLed[1], PinState.LOW));
                    myButton.addTrigger(new GpioSetStateTrigger(myLed[2], PinState.LOW));                   
                }
            }
        });
    }

    public void reset() {
        counter = 0;
        flagSensor = false;
    }

}

You're using a static int for counter and a static boolean for flagSensor but you've got multiple instances of LockerControl . Static variables are common to all instances of a class (see here ). You can fix this by removing the static modifier.

Note that if it is possible for different threads to modify the individual LockerControl instances, look into concurrency and/or Atomic Integer , Atomic Boolean classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM