【问题标题】:How can I combine these two for loop statements?如何结合这两个 for 循环语句?
【发布时间】:2020-02-19 00:46:50
【问题描述】:

您好,我正在使用 Arduino 让一列 4 个 LED 从左到右横穿,然后在 4x16 LED 面板上再次横穿。 目前我正在使用两个 for 循环语句来实现这一点,但我想知道是否有办法将这两个循环组合起来,或者从左到右到左都包含在一个语句中。 谢谢。

int row1 = 0;
int row2 = 0;
int row3 = 0;
int row4 = 0;
int row1r = 0;
int row2r = 0;
int row3r = 0;
int row4r = 0;

void setup() {
  FastLED.addLeds<LED_TYPE, DATA_PIN, COLOR_ORDER>(leds, NUM_LEDS);
  Serial.begin(9600);
  while (! Serial);
}

void loop() {
  // Left to Right
  for (row1 = 0, row2 = 31, row3 = 32, row4 = 63; row1 <= 15 && row2 >= 16 && row3 <= 47 && row4 >= 48; row1++, row2--, row3++, row4--) {
    leds[row1] = CRGB::Blue;
    leds[row2] = CRGB::Blue;
    leds[row3] = CRGB::Blue;
    leds[row4] = CRGB::Blue;
    FastLED.show();
    delay(30);
    leds[row1] = CRGB::Black;
    leds[row2] = CRGB::Black;
    leds[row3] = CRGB::Black;
    leds[row4] = CRGB::Black;
  }
  // Right to Left
  for (row1r = 15, row2r = 16, row3r = 47, row4r = 48; row1r >= 0 && row2r <= 31 && row3r >= 32 && row4r <= 63; row1r--, row2r++, row3r--, row4r++) {
    leds[row1r] = CRGB::Blue;
    leds[row2r] = CRGB::Blue;
    leds[row3r] = CRGB::Blue;
    leds[row4r] = CRGB::Blue;
    FastLED.show();
    delay(30);
    leds[row1r] = CRGB::Black;
    leds[row2r] = CRGB::Black;
    leds[row3r] = CRGB::Black;
    leds[row4r] = CRGB::Black;
  }
}

【问题讨论】:

  • 首先修复两个现有循环,使它们只使用一个循环变量。使用四个同时的循环变量太复杂了,而且令人困惑。因为所有四行都有简单的数学关系,所以在它们之间,使用一个变量并计算剩余三行的地址是微不足道的。一旦你简化了这两个循环,使它们更简单并且只使用一个变量,那么如何组合这两个循环应该是非常明显的。

标签: c++ for-loop arduino led


【解决方案1】:

我认为针对此要求的更好算法方法是将 LED 矩阵视为 16 列的数组,然后定义一个函数,该函数采用列索引并照亮与该列对应的 LED,每个 LED 应定义为变量的函数columnIndex 然后循环遍历 void 循环中的 16 列。

1)

void illuminateColumn(columnIndex)
{
    //illuminate leds[4*columnIndex]
    //illuminate leds[4*columnIndex+1]
    //illuminate leds[4*columnIndex+2]
    //illuminate leds[4*columnIndex+3]

    delay(30)

    //blacken leds[4*columnIndex]
    //blacken leds[4*columnIndex+1]
    //blacken leds[4*columnIndex+2]
    //blacken leds[4*columnIndex+3]

}

void loop()
{
  for(int i=0;i<15;i++)
   {
     illuminateColumn(i);
   }

  for(int j=15;j>0;j--)
   {
     illuminateColumn(i);
   }

}

2)

void illuminateColumn(columnIndex)
{
//illuminate leds[63-columnIndex]
//illuminate leds[47-columnIndex]
//illuminate leds[31-columnIndex]
//illuminate leds[15-columnIndex]

delay(30)

//blacken leds[63-columnIndex]
//blacken leds[47-columnIndex]
//blacken leds[31-columnIndex]
//blacken leds[15-columnIndex]

 }

 void loop()
  {
    for(int i=0;i<15;i++)
     {
       illuminateColumn(i);
     }

    for(int j=15;j>0;j--)
     {
      illuminateColumn(i);
     }

   }

【讨论】:

  • LED 矩阵格式如下:63 62 61 60 59 58 57 56 55 54 53 52 51 50 49 48 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 这会带来什么变化?
  • 编辑了答案以包含您的案例
猜你喜欢
  • 2017-04-26
  • 1970-01-01
  • 2020-03-08
  • 2021-09-18
  • 1970-01-01
  • 1970-01-01
  • 2015-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多