超时后Arduino adalight退出功能

时间:2015-11-26 15:32:46

标签: java function arduino philips-hue

我正在使用arduino和ledstrip进行项目。我在同一条游戏中将飞利浦流光溢彩和飞利浦色调相结合。我使用按钮在Hue和流光溢彩之间切换,为此我使用adalight代码。

当流光溢彩功能没有从处理中得到响应时,我可以从Hue(color();)切换到流光溢彩功能,但不能从adalight返回到color功能。它似乎保持在流光溢彩功能中。当它没有得到响应时,下面的代码开始工作:

// If no data received for an extended time, turn off all LEDs.
              if((t - lastByteTime) > serialTimeout) {
                memset(leds, 0,  NUM_LEDS * sizeof(struct CRGB)); //filling Led array by zeroes
                FastLED.show();
                lastByteTime = t; // Reset counter
              }

我尝试过很多东西,但似乎没什么用。 所以我的问题是,如何退出环境光功能并返回颜色功能?

void loop() {
  if(state == 0){
    color();
  }

  if(state == 1){
     ambilight(); 
  }
}

void color(){
  for (i=0; i< strip.numPixels(); i++){
    strip.setPixelColor(i, 255,0,0);
    }
  strip.show();
}    

void ambilight(){
      button();
      uint8_t
        buffer[256],
      indexIn       = 0,
      indexOut      = 0,
      mode          = 0,
      hi, lo, chk, i, spiFlag;
      int16_t
        bytesBuffered = 0,
      hold          = 0,
      c;
      int32_t
        bytesRemaining;
      unsigned long
        startTime,
      lastByteTime,
      lastAckTime,
      t;
      int32_t outPos = 0;

      startTime    = micros();
      lastByteTime = lastAckTime = millis();

      for(;;) {

        // Implementation is a simple finite-state machine.
        // Regardless of mode, check for serial input each time:
        t = millis();
        if((bytesBuffered < 256) && ((c = Serial.read()) >= 0)) {
          buffer[indexIn++] = c;
          bytesBuffered++;
          lastByteTime = lastAckTime = t; // Reset timeout counters
        } 
        else {
          // No data received.  If this persists, send an ACK packet
          // to host once every second to alert it to our presence.
          if((t - lastAckTime) > 1000) {
            Serial.print("Ada\n"); // Send ACK string to host
            lastAckTime = t; // Reset counter
          }
          // If no data received for an extended time, turn off all LEDs.
          if((t - lastByteTime) > serialTimeout) {
            memset(leds, 0,  NUM_LEDS * sizeof(struct CRGB)); //filling Led array by zeroes
            FastLED.show();
            lastByteTime = t; // Reset counter
          }
        }

        if(mode == 0){

          // In header-seeking mode.  Is there enough data to check?
          if(bytesBuffered >= HEADERSIZE) {
            // Indeed.  Check for a 'magic word' match.
            for(i=0; (i<MAGICSIZE) && (buffer[indexOut++] == magic[i++]););
            if(i == MAGICSIZE) {
              // Magic word matches.  Now how about the checksum?
              hi  = buffer[indexOut++];
              lo  = buffer[indexOut++];
              chk = buffer[indexOut++];
              if(chk == (hi ^ lo ^ 0x55)) {
                // Checksum looks valid.  Get 16-bit LED count, add 1
                // (# LEDs is always > 0) and multiply by 3 for R,G,B.
                bytesRemaining = 3L * (256L * (long)hi + (long)lo + 1L);
                bytesBuffered -= 3;
                outPos = 0;
                memset(leds, 0,  NUM_LEDS * sizeof(struct CRGB));
                mode           = 1; // Proceed to latch wait mode
              } 
              else {
                // Checksum didn't match; search resumes after magic word.
                indexOut  -= 3; // Rewind
              }
            } // else no header match.  Resume at first mismatched byte.
            bytesBuffered -= i;
          }
        } 

       if (mode == 1){
          if(bytesRemaining > 0) {
            if(bytesBuffered > 0) {
              if (outPos < sizeof(leds))
                ledsRaw[outPos++] = buffer[indexOut++];   // Issue next byte
              bytesBuffered--;
              bytesRemaining--;
            }
            // If serial buffer is threatening to underrun, start
            // introducing progressively longer pauses to allow more
            // data to arrive (up to a point).
          } 
          else {
            // End of data -- issue latch:
            startTime  = micros();
            mode       = 0; // Begin next header search
            FastLED.show();
          }
        }

      } // end for(;;)
    }

0 个答案:

没有答案