I'm writing a sketch that takes the input from a 4x4 keypad and a potentiometer, and stores it in a Struct that it sends to other Arduinos using an nRF24L01. The number keys toggle between two states, so I've stored the current state of each key in a bool array. There is a second array that stores settings when *[Number] is pressed. The letter keys A-D are four options for a single setting, and are stored as a char. Speed and direction for driving a motor are stored as bytes.
There are 8 settings on the number keys, and 4 settings on *[Number], which is the size of the relevant array.
One of these arrays works as it should, but the other stores what appear to be random numbers in the last element, and also the subsequent elements that haven't been declared (I hope that makes sense!). I've tried everything I can think of to trace this problem. If I remove the affected array, it affects the other one. If I change the size of the array, the problem still affects the last element.
This is the relevant code, that I've stripped out everything else from:
//*******KEYPAD*******
#include <Keypad.h>
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {6, 7, 8 ,9}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {2, 3, 4, 5}; //connect to the column pinouts of the keypad
//initialize an instance of class NewKeypad
Keypad keys = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
bool keyAlt = false;
byte keyNo;
//*******DATA STRUCTURE*******
struct Tx_Data {
byte speed;
byte direction;
char fyTrack;
bool points[8]; //Set to number of points (8 points = points[8])
bool sections[4]; //Set to number of sections (4 sections = sections[4])
};
struct Tx_Data controlData;
byte maxPoints = sizeof(controlData.points);
byte maxSections = sizeof(controlData.sections);
void setup() {
Serial.begin(9600);
Serial.print(F("Max Points: "));
Serial.println(maxPoints);
Serial.print(F("Max Sections: "));
Serial.println(maxSections);
}
void loop() {
//*******READ KEYPAD*******
char keyPressed = keys.getKey();
if (keyPressed) {
Serial.print(keyPressed);
Serial.print("(");
Serial.print(byte(keyPressed));
Serial.print(")");
//Letter keys
if (keyPressed == 'A' || keyPressed == 'B' || keyPressed == 'C' || keyPressed == 'D') {
controlData.fyTrack = char(keyPressed);
} else {
//Number keys
keyNo = byte(keyPressed) - 48;
if (keyPressed == '*') {
keyAlt = true;
} else {
if (keyAlt == true) {
if (keyNo <= maxSections) {
controlData.sections[keyNo] = !controlData.sections[keyNo];
}
} else {
if (keyNo <= maxPoints) {
controlData.points[keyNo] = !controlData.points[keyNo];
}
}
keyAlt = false;
}
}
Serial.print(F(" FY:"));
Serial.print(controlData.fyTrack);
Serial.print(F(" S3:"));
Serial.print(controlData.sections[3]);
Serial.print(F(" S4:"));
Serial.print(controlData.sections[4]);
Serial.print(F(" S5:"));
Serial.print(controlData.sections[4]);
Serial.print(F(" P7:"));
Serial.print(controlData.points[7]);
Serial.print(F(" P8:"));
Serial.print(controlData.points[8]);
Serial.print(F(" P9:"));
Serial.println(controlData.points[9]);
} //End of keyPressed()
}
This is what appears on the serial monitor:
Max Points: 8
Max Sections: 4
A(65) FY:A S3:0 S4:176 S5:176 P7:0 P8:0 P9:0
B(66) FY:B S3:0 S4:106 S5:106 P7:0 P8:0 P9:0
7(55) FY:B S3:0 S4:166 S5:166 P7:1 P8:0 P9:0
7(55) FY:B S3:0 S4:29 S5:29 P7:0 P8:0 P9:0
8(56) FY:B S3:0 S4:236 S5:236 P7:0 P8:1 P9:0
8(56) FY:B S3:0 S4:106 S5:106 P7:0 P8:0 P9:0
*(42) FY:B S3:0 S4:186 S5:186 P7:0 P8:0 P9:0
3(51) FY:B S3:1 S4:13 S5:13 P7:0 P8:0 P9:0
*(42) FY:B S3:1 S4:188 S5:188 P7:0 P8:0 P9:0
3(51) FY:B S3:0 S4:107 S5:107 P7:0 P8:0 P9:0
Note that S4 and S5 display an apparently random number that changes each time the Struct is updated. S5 and P9 don't actually exist, but S5 displays the same number!
I've tried this on a different make of Uno with just the keypad connected, and it makes no difference.
I'm totally baffled by this, and can't think of anything else to try. Does anyone have an answer?