如何组合这两个 for 循环语句?
How can I combine these two for loop statements?
您好,我正在使用 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;
}
}
我认为针对此要求的更好算法方法是将 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);
}
}
您好,我正在使用 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;
}
}
我认为针对此要求的更好算法方法是将 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);
}
}