First time using softwareserial. Is this correct, because it still does not work...
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2, 3);
//macro for calculating the length of arrays
#define ARY_LEN(a) (sizeof(a)/sizeof(a[0]))
//array called "state"
boolean state [] = {
LOW, LOW, LOW, LOW };
//array called "command" that defines the commands that need to be received from the remote
int command [] = {
0x31, 0x32, 0x33, 0x34 };
//get the length of the "command" array and call it "num_commands"
const int NUM_COMMANDS = ARY_LEN(command);
// array called "pins" that defines which pins are outputs
int pins [] = {
8, 9, 10, 11 };
void setup() {
mySerial.begin(9600);
//sets pins defined in "pins" array as outputs
for(int i = 0; i<NUM_COMMANDS; i++) pinMode(pins [i], OUTPUT);
}
void loop() {
if (mySerial.available() > 0) {
// reads data from mySerial and saves it as a variable called "data"
int data = mySerial.read();
//for-loop that starts with i=0 which is the first box of each array.
//proceeds through the loop, then adds one to i, for i=1 which is the second
//box of each array. does this till i=num_commands at which time it exits the loop
for(int i = 0; i<NUM_COMMANDS; i++) {
// if the data received from the mySerial port = the ith value of the "command" array, then
if (data == command[i]){
//change the state of the ith command to its opposite
state[i] = !state[i];
//make the ith pin match the state that was set above
digitalWrite(pins [i], state [i]);
}
}
}
}
edit: Got it. On the mega, "only the following can be used for RX: 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69 " So the above worked, I just had to change the SoftwareSerial pins.