数组中的 ARDUINO 元素不会被赋值

ARDUINO Element in array won't get assigned a value

// define variables
int redValue;
int greenValue;
int blueValue;

// main loop
void loop() {
#define delayTime 10 // fading time between colors

int colorArray[3] = {redValue, greenValue, blueValue};
colorArray[0] = redValue;
colorArray[1] = greenValue;
colorArray[2] = blueValue;

for (int j = 0; j < 3; j++) {
    for (int i = 0; i < 255; i++) {
        colorArray[j] = i;
        analogWrite(RED, redValue);
        analogWrite(BLUE, blueValue);
        analogWrite(GREEN, greenValue);
    } 
}
while( true ){
  //An empty loop that never ends.
}

}

为什么分配给“redValue”的 colorArray[0] 没有在 for 循环中分配另一个值? 我测试了它并打印了每个结果,发现我打印的所有内容都等于 0,除了 colorArray[0] 的值为 i。看起来 colorArray[0] 就像一个单独的变量一样,不会将整数应用到元素中。

这是我打印的内容:

[Info] Opened the serial port - COM3
RED 0
J VALUE 0
COLOR ARRAY 0

RED 0
J VALUE 0
COLOR ARRAY 1

RED 0
J VALUE 0
COLOR ARRAY 2

RED 0
J VALUE 0
COLOR ARRAY 3

RED 0
J VALUE 0
COLOR ARRAY 4

RED 0
J VALUE 0
COLOR ARRAY 5

RED 0
J VALUE 0
COLOR ARRAY 6

RED 0
J VALUE 0
COLOR ARRAY 7

RED 0
J VALUE 0
COLOR ARRAY 8

RED 0
J VALUE 0
COLOR ARRAY 9

RED 0
J VALUE 0
COLOR ARRAY 10

RED 0
J VALUE 0
COLOR ARRAY 11

RED 0
J VALUE 0
COLOR ARRAY 12

RED 0
J VALUE 0
COLOR ARRAY 13

RED 0
J VALUE 0
COLOR ARRAY 14

RED 0
J VALUE 0
COLOR ARRAY 15

RED 0
J VALUE 0
COLOR ARRAY 16

RED 0
J VALUE 0
COLOR ARRAY 17

RED 0
J VALUE 0
COLOR ARRAY 18

RED 0
J VALUE 0
COLOR ARRAY 19

RED 0
J VALUE 0
COLOR ARRAY 20

RED 0
J VALUE 0
COLOR ARRAY 21

RED 0
J VALUE 0
COLOR ARRAY 22

RED 0
J VALUE 0
COLOR ARRAY 23

RED 0
J VALUE 0
COLOR ARRAY 24

RED 0
J VALUE 0
COLOR ARRAY 25

RED 0
J VALUE 0
COLOR ARRAY 26

RED 0
J VALUE 0
COLOR ARRAY 27

RED 0
J VALUE 0
COLOR ARRAY 28

RED 0
J VALUE 0
COLOR ARRAY 29

RED 0
J VALUE 0
COLOR ARRAY 30

RED 0
J VALUE 0
COLOR ARRAY 31

RED 0
J VALUE 0
COLOR ARRAY 32

RED 0
J VALUE 0
COLOR ARRAY 33

RED 0
J VALUE 0
COLOR ARRAY 34

它一直持续到达到 255 此时 J 值变为 1 并继续循环。

编辑 这是设置

#define RED 3
#define GREEN 5
#define BLUE 6

void setup()
{
Serial.begin(57600); // open the serial port at 9600 bps:
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);
}

您编写值的方式是错误的,您将 i 值分配给数组索引 j 并且从未使用过它。我猜你是怎么想的,如果你修改你分配不同变量的数组,变量也会被修改,但实际上不会。

正确的做法是:

for (int j = 0; j < 3; j++) {
    for (int i = 0; i < 255; i++) {
        colorArray[j] = i;
        analogWrite(RED, colorArray[0]);//0 is the red index
        analogWrite(BLUE, colorArray[2]);//2 is the blue index
        analogWrite(GREEN, colorArray[1]);//1 is the green index 
        //that's how you assigned the values to the array
    } 
}

您不需要使用

int redValue;
int greenValue;
int blueValue;

变量。

如果您想显示所有可能的颜色组合,您将有 2^24 种组合。三个嵌套循环将起作用:

#define RED 3
#define GREEN 5
#define BLUE 6

#define delayTime 10

void setup()
{
    pinMode(RED, OUTPUT);
    pinMode(GREEN, OUTPUT);
    pinMode(BLUE, OUTPUT);

    for (int r = 0; r < 256; r++) {
        for (int g = 0; g < 256; g++) {
            for (int b = 0; b < 256; b++) {
                analogWrite(RED, r);
                analogWrite(GREEN, g);
                analogWrite(BLUE, b);
                delay(delayTime);
            }
        }
    }
}

void loop()
{
}

以上代码将显示所有颜色一次。如果你想不断循环所有颜色:

#define RED 3
#define GREEN 5
#define BLUE 6

#define delayTime 10

// The current color - 32 bit value but only using 24 bits
// Will pack the 8 bit red, green, and blue values into this
// 00000000bbbbbbbbggggggggrrrrrrrr
unsigned long current_color = 0;
// Macros to get the red, green, and blue elements from a 32-bit value
#define GET_RED(x)   ((x) & 0xFF)
#define GET_GREEN(x) (((x) & 0xFF00) >> 8)
#define GET_BLUE(x)  (((x) & 0xFF0000) >> 16)

void setup()
{
    pinMode(RED, OUTPUT);
    pinMode(GREEN, OUTPUT);
    pinMode(BLUE, OUTPUT);
}

void loop()
{
    analogWrite(RED, GET_RED(current_color));
    analogWrite(GREEN, GET_GREEN(current_color));
    analogWrite(BLUE, GET_BLUE(current_color));
    delay(delayTime);

    current_color += 1;
    if (current_color > 0xFFFFFF) current_color = 0;
}