视频和音频会在不符合条件时继续重启

时间:2016-07-25 23:50:51

标签: javascript jquery arduino arduino-uno

功能性:

我已经使用Arduino来感知用户接近度。当感测到接近时,arduino将检测并返回“1”。延迟时间为4秒:意味着之前返回4“1”,播放的视频将重新启动并伴随视频的音频。

当用户离开时,arduino将检测并返回“0”。将有3秒的延迟:意味着在视频的音频静音之前3“0”,而视频将继续播放。同时,为了增加用户体验,即使当用户在那里但是碰巧烦躁以致arduino检测到并返回“0”时,带有音频的视频假设继续而不运行停止音频功能,除非返回3“0”s。

问题:

我成功实现了以下目标:

1。)当arduino返回4“1”时,视频会重播音频 2.)音频静音,但当arduino返回3“0”时,视频将继续播放

然而,这是我目前面临的问题:

1。)当arduino在3“0”s的条件之前至少返回“0”或2“0”时,我无法获得带有音频的视频继续播放。带有音频的视频将被中断,当用户仍在附近时,视频将以音频重新开始。

这不是我正在寻找的行为。 正确的行为流程:

  • 感知到用户接近:arduino返回4“1”,视频将重新启动音频。当用户屏幕小部件时,arduino返回临时“0”,带有音频的视频仍将继续播放。但是,当用户离开时,arduino返回3“0”s,音频将静音,而视频将继续播放

代码:

function getFeedback() {
  //To call on ajac getFeedBack
  ajax_getArduinoFeedback("flash.do", "formType=getArduinoFeedback");
}

//To stop interrupt method and reset fields to original
function stop_interrupt() {
  isInterrupt = false;
  triggerFeedback = "0";

  setTimeout(function() {
    $("#M_Video").jPlayer("mute", true);
  }, 3000);
  //console.log("stop_interrupt triggerFeedback: " + triggerFeedback );          
}

//Interrupt method to call on refresh method
function show_interrupt(flag) {
  //Set Timeout for flag to be equal to "1"
  isInterrupt = true;
  isPlay = false;
  triggerFeedback = flag;

  if (flag == "1") {

    Flag = setTimeout(function() {


      if (triggerFeedback == "1") {
        //Display digital alert

        console.log("4 seconds later");
        console.log("flag: " + triggerFeedback);

        //Clear Timeout value of flag="1"  when current flag
        clearTimeout(Flag);

        //Clear Timeout value of flag ="0"
        clearTimeout(Absent);

        if (isPlay == false) {
          isPlay = true;
          $('#smell_background').fadeIn({
            duration: slideDuration,
            queue: false,
            complete: function() {

              $('#smell_text').fadeIn({
                duration: slideDuration,
                queue: false
              });
              //Smell-Image to fadeOut after delay
              setTimeout(function() {
                $('#smell_text').fadeOut({
                  duration: slideDuration,
                  queue: false
                });
                setTimeout(function() {
                  $('#smell_background').fadeOut({
                    duration: slideDuration,
                    queue: false
                  });
                }, 900);
              }, 4000);
            }
          });

          setTimeout(function() {
            //Restart video

            $("#M_Video").hide();

            $("#M_Video").jPlayer("setMedia", {
              m4v: "lib/video/video_03.mp4"
            }).jPlayer("play");
            $("#M_Video").jPlayer("mute", false);

            setTimeout(function() {
              $("#M_Video").fadeIn();
            }, 2500);
            console.log("videoList:" + videoList[videoIndex]);
          }, 5000);
        }
      }
    }, 2000);
  } else if (flag == "0") {

    isPlay = false;
    clearTimeout(Flag);
    console.log(isPlay + "1");

    Absent = setTimeout(function() {
      if (triggerFeedback == "0") {
        clearTimeout(Flag);
        isPlay = false;
        console.log("flag: " + triggerFeedback);

        console.log(isPlay + "2");
        if (isPlay == false) {
          console.log(isPlay + "3");
          stop_interrupt();
        }
      }
    }, 5000);

  }
}

//Method call when data from Arduino is "1"
function interrupt(flag) {
  console.log("isInterrupt : " + flag);
  //location.reload();
  if (isInterrupt == false) {

    if (triggerFeedback != flag) {
      if (triggerFeedback == "1" && flag == "0") {
        //stop_interrupt();
        show_interrupt(flag);
      } else {
        if (flag != "0") {
          //console.log("1");
          show_interrupt(flag);
        }
      }
    } else {
      //console.log("2");
      show_interrupt(flag);
    }
  } else {
    if (triggerFeedback != flag) {
      if (triggerFeedback == "1" && flag == "0") {
        //stop_interrupt();
        show_interrupt(flag);
      } else {
        if (flag != "0") {
          //console.log("3");
          show_interrupt(flag);
        }
      }
    }
  }
}
<body oncontextmenu="return false">

  <div id="M_Start" align="center" class="M">

    <!--Video Div-->
    <div id="M_Video" style="position:absolute; z-index:99; left:-80px; top:0px;"></div>
    <!--Alert background-->
    <img id="smell_background" class="Smell_Cask" src="lib/img/Smell_Background.png" />

    <!--Alert Text-->
    <img id="smell_text" class="Smell_Cask_Text" src="lib/img/Smell_Text.png" />

  </div>

</body>

// Arduino代码:

const int trigPin = 8;
const int echoPin = 9; 
//Motor-Fan connected to arduino pin number
const  int FanPin = 5;

long duration;
int distance;

void setup() 
{
pinMode (FanPin , OUTPUT) ; // Set pinMode for FanPin as OUTPUT, display  
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT);
Serial.begin(9600); // Open serial port to communicate with the Ultrasaonic Sensor
}

void loop() 
{

digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

if(distance < 20)
{
  //delay a time of 2 seconds  before writing Print of '1' and set Motor fan to HIGH
  delay(1000);

  // Write a pin of HIGH
  Serial.println("1");
  //Set motor-fan to operate
  digitalWrite (FanPin, HIGH);
}
else
{
  Serial.println("0");
  //Set motor-fan to stop
  digitalWrite (FanPin, LOW);
}
delay(100);
}

我做错了什么或错过了什么?请帮忙。

1 个答案:

答案 0 :(得分:0)

请记住,中断函数需要非常短。我还想要一些澄清和一些当前的代码。但是,我认为你需要更短的中断功能。

中断应该只根据传感器值设置一个全局值。