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