Changing the value of an array

Hi,

I'm having problems changing the value in an array. What I expect to happen is the array values for bandVal will be changed depending on the status of two pins.

int incomingByte = 0;   // for incoming serial data

byte bandVal[7] = {0x00,0x00,0x00,0x24,0x80,0x00,0x01};
    
void setup() {
        Serial1.begin(9600);     // opens serial port, sets data rate to 9600 bps

  //  switch inputs are declared here
        pinMode(50, INPUT);
        pinMode(51, INPUT);

             }

void loop() {
        // send data only when you receive data:
                if (Serial1.available() > 0) {
                // read the incoming byte:
                incomingByte = Serial1.read();

    int SwitchVal = 0;   
    if (digitalRead(50) == HIGH)
      SwitchVal += 1;
    if (digitalRead(51) == HIGH)
      SwitchVal += 2;
      
      if (SwitchVal == 0) 
      byte bandVal[7] = {0x00,0x00,0x50,0x00,0x00,0x00,0x01};
      else if (SwitchVal == 1) 
      byte bandVal[7] = {0x00,0x01,0x44,0x00,0x00,0x00,0x01};
      else if (SwitchVal == 2) 
      byte bandVal[7] = {0x00,0x04,0x32,0x00,0x00,0x00,0x01};
      else if (SwitchVal == 3) 
      byte bandVal[7] = {0x00,0x12,0x96,0x00,0x00,0x00,0x01};
      
      Serial1.write(bandVal,7);
            }
}

I thought declaring bandVal at the top of the code would make it global thus allowing me to change it in the loop.

The change below works but I don't like it because the pin values change infrequently and I don't want to send the array out unless I receive a request on Serial1

      if (SwitchVal == 0) 
      {byte bandVal[7] = {0x00,0x00,0x50,0x00,0x00,0x00,0x01};
      Serial1.write(bandVal,7);}
      else if (SwitchVal == 1) 
      {byte bandVal[7] = {0x00,0x01,0x44,0x00,0x00,0x00,0x01};
      Serial1.write(bandVal,7);}
      else if (SwitchVal == 2) 
      {byte bandVal[7] = {0x00,0x04,0x32,0x00,0x00,0x00,0x01};
      Serial1.write(bandVal,7);}
      else if (SwitchVal == 3) 
      {byte bandVal[7] = {0x00,0x12,0x96,0x00,0x00,0x00,0x01};
      Serial1.write(bandVal,7);}

This leads me to think I am having problems with the concept of scope but I can't find a way to get my desired result.

Thanks

Lou

The only time you can fill all the elements of an array in one line like that is when you first create it.

In the rest of your code when you write "byte" in front of the array name you are telling the program to create a new array with that same name. Having two arrays with the same name is a recipe for disaster. Don't do it.

If you want to fill in all the values in an array then you'll have to use a for loop or another array and memcpy.

if (SwitchVal == 0) 
      byte temp[7] = {0x00,0x00,0x50,0x00,0x00,0x00,0x01};
      memcpy(bandVal, temp, 7);

Also, all of those new arrays are defined at the statement block level, so you will get a "scope" error message since they all go out of scope when you exit their statement block. You can prove this to yourself by commenting out the first definition of the array in line 2.

econjack:
Also, all of those new arrays are defined at the statement block level, so you will get a "scope" error message since they all go out of scope when you exit their statement block. You can prove this to yourself by commenting out the first definition of the array in line 2.

no error, it uses the global one

Thanks Delta_G

I didn't know about memcpy and that got me out of my problem.

@ econojack, Yep I found that during my attempts to fix the the problem.

I'm happy with those responses and my project is moving along now.

Lou