Erram
Erram

Reputation: 35

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.
}

}

Why do the colorArray[0] that is assigned "redValue" not get assigned another valoue in the for loop? I tested it and printed each result and saw that everything i printed equaled 0, except the colorArray[0] that had the value of i. It seems as the colorArray[0] acts as if it was a seperate variable and does not apply the integer into the elements.

Here is what i printed:

[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

It just keeps going on until it hits 255 in which point the J value becomes 1 and the loop continues.

EDIT this is the setup

#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);
}

Upvotes: 0

Views: 155

Answers (2)

001
001

Reputation: 13533

If you want to show all possible color combinations you will have 2^24 combinations. Three nested loops will work:

#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()
{
}

The above code will show all the colors one time. If you want to continually cycle through all the colors:

#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;
}

Upvotes: 1

ahmed
ahmed

Reputation: 98

The way you're writing the values is wrong, you're assigning the i value to the array index j and never used it. I guess what you thought there that if you modify the array that you assigned different variables to, the variables will get modified too, but actually no.

Here's the correct way to do it:

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
    } 
}

You don't need to use the

int redValue;
int greenValue;
int blueValue;

variables.

Upvotes: 1

Related Questions