Serial read resets Arduino

Hi!

I’ve made an serial receiver of my Arduino. I’ve tested it with my Uno R3 (which works great!) but when I run it on my Nano - it resets during reading of message.

When I send “14424562,0,1,0” the serial output is:

SEND] 14424562,0,1,0
buf = 14424�start

So I guess there’s something wrong with Serial.readBytesUntil() function, but I can’t see the problem.

#include <NexaTransmitter.h>
#include <stdlib.h>

#include <LiquidCrystal_I2C.h>
#include <Wire.h> 

const int txPin = 12;

const int SENDER = 1;


void setup() {
	Serial.begin(9600);
	Serial.println("start");

}

void loop() {



	int i = 0;

	uint32_t incomingByte = 0;

	char buf[16];
	String senderTemp = "";
	uint32_t sender = 0;
	int	state = 0;
	int	firstBreak = 0;
	int	secondBreak = 0;
	int thirdBreak = 0;
	int	dimLevel = 0;
	String dimTemp = "";
	int unit = 0;
	int separatorCounter = 0;

	
	memset(buf, 0, sizeof(buf));
	
	if (Serial.available() > 0) {

		incomingByte = (Serial.readBytesUntil('\n', buf, sizeof(buf))) - '0';


		Serial.print("buf = ");
		Serial.print(buf);
		Serial.println("");


		while(i < strlen(buf)) {


			if(buf[i] == ',') {

				separatorCounter++;
				switch (separatorCounter) {

					case SENDER:
						for(int x = 0; x < i; x++) senderTemp += buf[x];
						sender = senderTemp.toInt();
						Serial.print("sender = ");
						Serial.println(sender);

						unit = buf[i+1] - '0';
						Serial.print("unit = ");
						Serial.println(unit);

						break;


//********************* Code continues... ****************************
        incomingByte = (Serial.readBytesUntil('\n', buf, sizeof(buf))) - '0';

What would possess you to store the number of bytes read in a variable called incomingByte? That name makes NO sense.

Why would you presume that the function would NULL terminate the array? Why would you NOT think you needed to do that yourself?

The "problem" with the function is with your unreasonable expectations.

Is this looking better?

numBytes = (Serial.readBytesUntil('\n', buf, sizeof(buf))) - '0';
buf[numBytes] = '\0';

Try one of the simple reliable examples in serial input basics.

...R

http://www.gammon.com.au/serial

Is this looking better?

The code is. Are the results?

I remade my code using Nick Gammons functions. After some debugging I found the issue, but I can’t explain why.

I pass the serial message forward using a RF 433MHz Transmitter module.

I’m using NexaTransmitter library, based on Home Easy, to send the RF message.

This is the code.

NexaTransmitter remote(txPin, sender);
remote.setSwitch(true, 0);

If I comment out remote.setSwitch(true, 0);, then my code works. I can’t find any info on why it should not work on Nano. Any suggestions?

Any suggestions?

Sure. Explain what setSwitch() does and why you are calling it.

NEXA is a socket receiver for turning on/off lamps remotely.

My Arduino transmitter sends an 32bit RF-message including:

  1. sender ID (initialized in remote)
  2. Unit ID
  3. On/off

remote.setSwitch(true, 0); I'm setting unit ID 0 to on.