从结构数组返回不正确的值-Arduino

时间:2018-11-28 05:09:53

标签: arrays struct arduino

我遇到了一个here问的问题,但是由于该问题和代码有所改变,所以我开始一个新问题

我正在尝试通过函数从结构数组中读取和写入值

void BreezeMapPixels(struct_displayType& mDisplayUnit, int breezeStrength)

我从Serial.print获取的值不是存储在数组中的值。特别是endLed的那个,永远不能为0

串行输出

  

微风模块                                                                     开始8结束0                                                                     微风模块                                                                     开始1结束0                                                                     微风模块                                                                     开始1结束0                                                                     微风模块                                                                     开始4结束7

这些定义为

// Define each display's LED range
  {
    displayUnit[0].startLed=0;
    displayUnit[0].endLed=3;

    displayUnit[1].startLed=4;
    displayUnit[1].endLed=7;

    displayUnit[2].startLed=8;
    displayUnit[2].endLed=11;

    displayUnit[3].startLed=12;
    displayUnit[3].endLed=15;
  }

该程序似乎在一段时间后也变得僵硬。
对于我为什么读错值的任何帮助,将不胜感激。


完整代码

/* **********************************
  Breeze
* *********************************/
// Designed to illuminate a 'map' of pixels, each of which randomly
// sometimes twinkles brighter and then back down to it's base color again.
//



/* **********************************
  Start of Code
* *********************************/
enum { SteadyDim, GettingBrighter, GettingDimmerAgain };


void InitPixelStates(struct_displayType& mDisplayUnit)
{
  for ( uint16_t i = mDisplayUnit.startLed; i <= mDisplayUnit.endLed; i++) {
    leds[i]=BASE_COLOR;
  }
}

void BreezeMapPixels(struct_displayType& mDisplayUnit, int breezeStrength)
{
  Serial.println("Breeze Module");
  Serial.print("Start ");
  Serial.print(mDisplayUnit.startLed);
  Serial.print(" End " );
  Serial.println(mDisplayUnit.endLed);


  for ( uint16_t i = mDisplayUnit.startLed; i <= mDisplayUnit.endLed; i++) {
    Serial.print(" pixel state " );
    Serial.println(mDisplayUnit.pixelState[i] );
    Serial.println(GettingBrighter);
    Serial.println(GettingDimmerAgain);
    Serial.println(SteadyDim);
    if ( mDisplayUnit.pixelState[i] == SteadyDim) {
      // this pixels is currently: SteadyDim
      // so we randomly consider making it start getting brighter
      leds[i]=BASE_COLOR;
      if ( random8() < breezeStrength) {
        mDisplayUnit.pixelState[i] = GettingBrighter;

      }

    } else if ( mDisplayUnit.pixelState[i] == GettingBrighter ) {
      Serial.println("Brighter " + i);
      // this pixels is currently: GettingBrighter
      // so if it's at peak color, switch it to getting dimmer again
      if ( leds[i] >= PEAK_COLOR ) {

        mDisplayUnit.pixelState[i] = GettingDimmerAgain;
      } else {
        // otherwise, just keep brightening it:
        leds[i] += DELTA_COLOR_UP;
      }

    } else { // getting dimmer again
      Serial.println("Dimmer " + i);
      // this pixels is currently: GettingDimmerAgain
      // so if it's back to base color, switch it to steady dim
      if ( leds[i] <= BASE_COLOR ) {

        leds[i] = BASE_COLOR; // reset to exact base color, in case we overshot
        mDisplayUnit.pixelState[i] = SteadyDim;
      } else {
        // otherwise, just keep dimming it down:
        leds[i] -= DELTA_COLOR_DOWN;

      }
    }
  }
}

0 个答案:

没有答案