尽管使用了睡眠,但时间还是错误

发布于 2024-12-14 18:09:08 字数 2739 浏览 1 评论 0原文

我正在使用 IOIO Android 开发板。我正在尝试控制 2 个引脚和一个 LED,并每隔一段时间将它们打开关闭。到目前为止,它连接良好,LED 和引脚可以打开和关闭,但时机不正确。我认为问题出在 sleep() 函数上。 任何帮助或指导将不胜感激。

                 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) {
                    }
                }
            }
        }

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) {
                    }
                }
            }
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妖妓 2024-12-21 18:09:16

Sleep() 不是实时的 - 它只是将线程搁置至少指定的毫秒数。之后,它可用于调度并将在某个时候执行。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文