I'm controlling a light bulb with my Arduino Nano from serial or RF (via nRF24L01+) and I'm passing the commands as a string. After I get all the variables (3 ints) I try calling the function execOrder
but no matter what I put in there, the Arduino just hangs or resets. Code is as follows:
#define SSR 3
#define SW 4
#define nanoID 1
bool isPWM = false;
int value;
// Módulo RF
#include <SPI.h>
#include "nRF24L01.h"
#include "RF24.h"
#include "printf.h"
RF24 radio(9,10);
const uint64_t pipes[2] = { 0xF0F0F0F0E1LL, 0xF0F0F0F0D2LL };
#define PayLoad 20
#include <MemoryFree.h>
void setup() {
pinMode(SSR, OUTPUT);
pinMode(SW, INPUT_PULLUP);
digitalWrite(SSR, LOW);
value = 0;
Serial.begin(9600);
Serial.println("------ CEACI SAS -- Controller Module ------");
Serial.println("Module No: " + String(nanoID));
// Inicializar RF
Serial.println("Inicializando RF");
radio.begin();
radio.setRetries(15,15);
radio.setPayloadSize(PayLoad);
radio.setPALevel(RF24_PA_MIN);
radio.setDataRate(RF24_250KBPS);
radio.setCRCLength(RF24_CRC_16);
delay(100);
radio.openWritingPipe(pipes[1]);
radio.openReadingPipe(1,pipes[0]);
radio.startListening();
//radio.printDetails(); //RF Debug
Serial.println("RF listo!");
Serial.print("freeMemory()=");
Serial.println(freeMemory());
}
void loop() {
if (Serial.available()) {
Serial.println("Rcvd Serial!");
String msgRcvd = Serial.readString();
int ID = msgRcvd.substring(4,5).toInt();
int cmdCode = msgRcvd.substring(10,11).toInt();
int cmdVal = msgRcvd.substring(16).toInt();
//Serial.println("ID: "+String(ID)+", CMD: "+String(cmdCode)+", VAL: "+String(cmdVal));
Serial.print("freeMemory()=");
Serial.println(freeMemory());
//execOrder(ID,cmdCode,cmdVal);
} else if (radio.available()){
int RFlen=0;
char msgBuff[]="";
RFlen = radio.getDynamicPayloadSize();
delay(10);
radio.read(&msgBuff, RFlen);
String msgRcvd(msgBuff);
int ID = msgRcvd.substring(4,5).toInt();
int cmdCode = msgRcvd.substring(10,11).toInt();
int cmdVal = msgRcvd.substring(16).toInt();
//Serial.println("ID: "+String(ID)+", CMD: "+String(cmdCode)+", VAL: "+String(cmdVal));
Serial.print("freeMemory()=");
Serial.println(freeMemory());
//execOrder(ID,cmdCode,cmdVal);
} else {
chkSwitch();
}
}
void execOrder(int ID, int CMD, int VAL){
if (ID == nanoID || ID == 0){
switch(CMD){
case 0:
State();
break;
case 1:
value = VAL;
OnOff();
break;
case 2:
value = VAL;
PWM();
break;
}
}
}
I wanted to process the incoming string in a function, but it resets as well.
By the way, I'm using MemoryFree
library to check if I'm out of memory, and there's plenty of it, so I don't really know where's the problem. Any help would be greatly appreciated!
EDIT:
For the serial part I can do the following (my original code):
if (Serial.find("MOD=")) {
int ID = Serial.parseInt();
if (ID == nanoID || ID == 0) {
Serial.find("CMD=");
int command = Serial.parseInt();
int cmdVal = 0;
if (command != 0){
Serial.find("VAL=");
cmdVal = Serial.parseInt();
}
Serial.println("MOD: "+String(ID)+", CMD: "+String(command) + ", VAL: " + String(value));
Serial.print("freeMemory()=");
Serial.println(freeMemory());
execOrder(ID,command,cmdVal);
}
}
And it works flawlessly. But I don't know how to achieve the same results with the RF input. The problem seems to be with using so many strings.