Search code examples
javaandroidsleepthread-sleep

wrong timing despite use of sleep


I'm working with a IOIO Android development board. I'm trying to control 2 pins and an led and swtich them on off at intervals times. So far, it connects fine and the led and pins are turning on and off but not with the right timing. I assume the problem is with the sleep() function. Any help or guidance would be appreciated.

                 class IOIOThread extends Thread {
        private IOIO ioio_;
        private boolean abort_ = false;


        @Override
        public void run() {
            super.run();
            while (true) {
                synchronized (this) {
                    if (abort_) {
                        break;
                    }
                    ioio_ = IOIOFactory.create();
                }
                try {
                    setText(R.string.wait_ioio);
                    ioio_.waitForConnect();
                    setText(R.string.ioio_connected);

                    while (true) {

                        while((button_.isChecked())==true){

                            DigitalOutput led = ioio_.openDigitalOutput(0,   false);;
                            DigitalOutput S1 = ioio_.openDigitalOutput(35,   false);
                            DigitalOutput S2 = ioio_.openDigitalOutput(36,  false);
                            DigitalOutput S3 = ioio_.openDigitalOutput(37,   false);
                            DigitalOutput S4 = ioio_.openDigitalOutput(38,   false);

                            holdTime=30000;

                            for (currentTime=0; currentTime<=holdTime;)
                            {
                                led.write(true);
                                S1.write(true);
                                S2.write(true);
                                sleep(4000);
                                led.write(false);
                                S1.write(false);
                                S2.write(false);
                                sleep(2000);
                                led.write(true);
                                S3.write(true);
                                S4.write(true);
                                sleep(4000);
                                led.write(false);
                                S3.write(false);
                                S4.write(false);
                                sleep(4000);
                                currentTime = currentTime+14000;

                        }}}}

                catch (ConnectionLostException e) {
                } catch (Exception e) {
                    Log.e("HelloIOIOPower", "Unexpected exception caught", e);
                    ioio_.disconnect();
                    break;
                } finally {
                    try {
                        ioio_.waitForDisconnect();
                    } catch (InterruptedException e) {
                    }
                }
            }
        }

Solution

  • Sleep() is not realtime - it just puts your thread on hold for at least specified aqmount of milliseconds. After that it is available for scheduling and will be executed - sometime.