If statement changes the variable of the condition it checks?

Whenever I run my code the if statement of lines 58-67 change the variable "SELECT1" to always be 1. If I take that section of code out I don't have the issue, but the arrays I want copied obviously wont copy. I've also tried a for loop and have looked everywhere for answers. Please help ::slight_smile:

//define colors RGB
int RED[3] = {50, 0, 0};
int ORANGE[3] = {50, 30, 30};
int YELLOW[3] = {50, 30, 30};
int GREEN[3] = {0, 50, 0};
int BLUE[3] = {0, 0, 50};
int PURPLE[3] = {25, 0, 25};
int WHITE[3] = {50, 50, 50};

//define pins
//color pins
int RLED = 3;
int GLED = 5;
int BLED = 6;

//Button analog pin = A1
int BUTTON = 0;
int SELECT0 = 1;
int SELECT1 = 1;

// COLOR0 is current color, COLOR1 is next color
int COLOR0[3] = {0, 0, 0};
int COLOR1[3] = {0, 0, 0};
int COLOR2[3] = {0, 0, 0};


void setup() {
  //Start serial communication
  Serial.begin(9600);
  //start pins
  pinMode(RLED, OUTPUT);
  pinMode(GLED, OUTPUT);
  pinMode(BLED, OUTPUT);
  pinMode(A1, INPUT_PULLUP);
  //
}

int BUTTON_SELECT()
{
  BUTTON = analogRead(A1);
  if (BUTTON > 0 && BUTTON < 35)
    SELECT1 = 1;
  else if (BUTTON > 55 && BUTTON < 90)
    SELECT1 = 2;
  else if (BUTTON > 100 && BUTTON < 130)
    SELECT1 = 3;
  else if (BUTTON > 145 && BUTTON < 175)
    SELECT1 = 4;
  else if (BUTTON > 190 && BUTTON < 205)
    SELECT1 = 5;
  SELECT0 = SELECT1;
  return SELECT1;
}

void loop() {
  //changes color based on button //DEBUG
  SELECT1 = BUTTON_SELECT();
  if (SELECT1 = 1)
    memcpy(COLOR1, WHITE, sizeof(COLOR1));
  else if (SELECT1 = 2)  
    memcpy(COLOR1, WHITE, sizeof(COLOR1));
  else if (SELECT1 = 3)
    memcpy(COLOR1, WHITE, sizeof(COLOR1));
  else if (SELECT1 = 4)
    memcpy(COLOR1, WHITE, sizeof(COLOR1));
  else  if (SELECT1 = 5)
    memcpy(COLOR1, WHITE, sizeof(COLOR1));
  //DEBUG

  //prints debugging info
  Serial.println(BUTTON);
  Serial.print("SELECT 1 :");
  Serial.println(SELECT1);
  Serial.print("SELECT 0 :");
  Serial.println(SELECT0);
  delay(500);


  //sets LED color
  analogWrite(RLED, COLOR1[0]);
  analogWrite(GLED, COLOR1[1]);
  analogWrite(BLED, COLOR1[2]);
}

Table_LED2.ino (1.81 KB)

I can’t see your code.
Please read the stickies about - HOW TO FORMAT & POST YOUR CODE

Attachments aren’t a good idea in most cases, because those contributors on phones and tablets can’t open them.

Use == instead of = within if statements

//define colors RGB
int RED[3] = {50, 0, 0};
int ORANGE[3] = {50, 30, 30};
int YELLOW[3] = {50, 30, 30};
int GREEN[3] = {0, 50, 0};
int BLUE[3] = {0, 0, 50};
int PURPLE[3] = {25, 0, 25};
int WHITE[3] = {50, 50, 50};

//define pins
//color pins
int RLED = 3;
int GLED = 5;
int BLED = 6;

//Button analog pin = A1
int BUTTON = 0;
int SELECT0 = 1;
int SELECT1 = 1;

// COLOR0 is current color, COLOR1 is next color
int COLOR0[3] = {0, 0, 0};
int COLOR1[3] = {0, 0, 0};
int COLOR2[3] = {0, 0, 0};


void setup() {
  //Start serial communication
  Serial.begin(9600);
  //start pins
  pinMode(RLED, OUTPUT);
  pinMode(GLED, OUTPUT);
  pinMode(BLED, OUTPUT);
  pinMode(A1, INPUT_PULLUP);
  //
}

int BUTTON_SELECT()
{
  BUTTON = analogRead(A1);
  if (BUTTON > 0 && BUTTON < 35)
    SELECT1 = 1;
  else if (BUTTON > 55 && BUTTON < 90)
    SELECT1 = 2;
  else if (BUTTON > 100 && BUTTON < 130)
    SELECT1 = 3;
  else if (BUTTON > 145 && BUTTON < 175)
    SELECT1 = 4;
  else if (BUTTON > 190 && BUTTON < 205)
    SELECT1 = 5;
  SELECT0 = SELECT1;
  return SELECT1;
}

void loop() {
  //changes color based on button
  SELECT1 = BUTTON_SELECT();
  if (SELECT1 == 1)    // == instead of =
    memcpy(COLOR1, WHITE, sizeof(COLOR1));
  else if (SELECT1 == 2)    // == instead of =
    memcpy(COLOR1, WHITE, sizeof(COLOR1));
  else if (SELECT1 == 3)    // == instead of =
    memcpy(COLOR1, WHITE, sizeof(COLOR1));
  else if (SELECT1 == 4)    // == instead of =
    memcpy(COLOR1, WHITE, sizeof(COLOR1));
  else  if (SELECT1 == 5)    // == instead of =
    memcpy(COLOR1, WHITE, sizeof(COLOR1));

  //prints debugging info
  Serial.println(BUTTON);
  Serial.print("SELECT 1 :");
  Serial.println(SELECT1);
  Serial.print("SELECT 0 :");
  Serial.println(SELECT0);
  delay(500);


  //sets LED color
  analogWrite(RLED, COLOR1[0]);
  analogWrite(GLED, COLOR1[1]);
  analogWrite(BLED, COLOR1[2]);
}

Turn warnings on:
File / Preferences / Show verbose output during compilation: CHECK
and you will get some warnings with the original code, like:

warning: suggest parentheses around assignment used as truth value [-Wparentheses]
   if (SELECT1 = 1)
  BUTTON = analogRead(A1);

By convention, all capital letter names are constants. Constants never appear on the left of an equal sign, except when the initial value is assigned.

int BUTTON_SELECT()
{

Why does this function return the value in a global variable?

Why IS BUTTON global?