Serial communication data stream

Hello

im on a project where i send some values by serial comunication and in the Arduino firmware i use those values to control leds and motors.

But i have some issues with my datastream.
Is there a solution so that i can send some new values but keep the others.

For example i send new data to the leds but i want that the motors keep doing the same things. Or the other way around.

oid loop()																	/* add main program code here, this code starts again each time it ends */
{
	int StateButton = digitalRead(DemoButton);
	if (StateButton ==1)
	{
		Demo();
	}
	else
	{
		
	while (Serial.available() > 0)								//Read all serial data available, as fast as possible			
	{
		char inChar = Serial.read();
		if (inChar == SOP)
		{
			index = 0;
			inDate[index] = '\0';
			started = true;
			ended = false;
		}
		else if (inChar == EOP)
		{
			ended = true;
			break;
		}
		else
		{
			if (index < 70)
			{
				inDate[index] = inChar;
				index++;
				inDate[index] = '\0';
			}
		}
	}
	if (started && ended)
	{
		
		
		Serial.print("Ingelezen data = inData: ");				// Debug reading data
		Serial.println(inDate);
		
		char *token1 = strtok(inDate, ",");
		int Led = atoi(token1);									// Select Led Mode
		Serial.print("Led: ");									// Debug token 1
		Serial.println(Led);
		
		char *token2 = strtok(NULL, ",");
		int LedPixelColor = atoi(token2);						
		Serial.print("LedPixelColor: ");
		Serial.println(LedPixelColor);
		
		char *token3 = strtok(NULL, ",");
		int LedPixieColor = atoi(token3);						
		Serial.print("LedPixieColor: ");
		Serial.println(LedPixieColor);
		
		char *token4 = strtok(NULL, ",");
		int LedPixelBrightness = atoi(token4);
		Serial.print("LedPixelBrightness: ");
		Serial.println(LedPixelBrightness);
		
		char *token5 = strtok(NULL, ",");
		int LedPixieBrightness = atoi(token5);
		Serial.print("LedPixieBrightness: ");
		Serial.println(LedPixieBrightness);
		
		char *token6 = strtok(NULL, ",");
		int Motor = atoi(token6);
		Serial.print("Motor: ");
		Serial.println(Motor);
		
		char *token7 = strtok(NULL, ",");
		int RotationSpeedDirection = atoi(token7);
		Serial.print("RotationSpeedDirection: ");
		Serial.println(RotationSpeedDirection);
		
		char *token8 = strtok(NULL, ",");
		int TiltingSpeedDirection = atoi(token8);
		Serial.print("TiltingSpeedDirection: ");
		Serial.println(TiltingSpeedDirection);
		
		char *token9 = strtok(NULL, ",");
		int PanningSpeedDirection = atoi(token9);
		Serial.print("PanningSpeedDirection: ");
		Serial.println(PanningSpeedDirection);
		
		
		
		switch (Led)
		{
			case 0:
				Serial.println("Case 0 Led: Reset all");				//   <0,X,X,X,X,X,X,X,X>
				resetAllLed();
				break;
			case 1:
				Serial.println("Case 1 Led: Pixies Red");				//   <1,X,X,X,0-255,X,X,X,X>
				PixiesRed(LedPixieBrightness);
				break;
			case 2:
				Serial.println("Case 2 Led: Pixies Green");				//   <2,X,X,X,0-255,X,X,X,X>
				PixiesGreen(LedPixieBrightness);
				break;
			case 3:
				Serial.println("Case 3 Led: Pixies Blue");				//   <3,X,X,X,0-255,X,X,X,X>
				PixiesBlue(LedPixieBrightness);
				break;	
			case 4:
				Serial.println("Case 4 Led: PixiesHeartbeat");			//   <4,X,0-9,X,X,X,X,X,X>
				PixiesHeartbeat(LedPixieColor);
				break;
			case 5:
				Serial.println("Case 5 Led: PixiesStrobo");				//   <5,X,0-9,X,X,X,X,X,X>
				PixiesStrobo(LedPixieColor);
				break;
			case 6:
				Serial.println("Case 6 Led: PixiesRandom");				//   <6,X,X,X,X,X,X,X,X>
				PixiesRandom();
				break;	
				
			case 7:
				Serial.println("Case 7 Led: PixelsHeartbeat");			//   <7,0-9,X,X,X,X,X,X,X>
				PixelsHeartbeat(LedPixelColor);
				break;
				
			//Toevoegen	
		}																// End switch Led sequence
		delay(1);
		
			
		switch (Motor)
		{
			case 0:
				Serial.println("Case 0 Motor: Reset all");				//  <X,X,X,X,X,0,X,X,X>
				ResetAllMotor();
				break;
			case 1:
				Serial.println("Case 1 Motor: StartUpAllMotor");		//  <X,X,X,X,X,1,X,X,X>
				StartUpAllMotor();
				break;
			case 2:
				Serial.println("Case 2 Motor: Rotation only");			//  <X,X,X,X,X,2,-150 to 150,X,X>
				RotationOnly(RotationSpeedDirection);
				break;
			case 3:
				Serial.println("Case 3 Motor: Tilting Only");			//  <X,X,X,X,X,3,X,-150 to 150,X>
				TiltingOnly(TiltingSpeedDirection);
				break;
			case 4:
				Serial.println("Case 4 Motor: Panning Only");			//  <X,X,X,X,X,4,X,X,-150 to 150>
				PanningOnly(PanningSpeedDirection);
				break;
			case 5:
				Serial.println("Case 5 Motor: Random all");				//  <X,X,X,X,X,5,X,X,X>
				RoTiPaRandom();
				break;
			case 6:
				Serial.println("Case 6 Motor: ManuAllMotor");			//  <X,X,X,X,X,6,X,X,X>
				ManuAllMotor(RotationSpeedDirection, TiltingSpeedDirection, PanningSpeedDirection);
				break;
			
		}																// End Switch Motor Sequence 
		delay(1);
		
		started = false;
		ended = false;
		index = 0;
		inDate[index] = '\0';   
	}

Best regards

Use one state variable for the motors and another one for the LEDs.

A little bit more info please?

See int Led, LedPixelColor, LedPixieColor, LedPixelBrightness, Motor, RotationSpeedDirection...

If you send all state values in each record, you have to send all the intended values all the time.

Make Yourself a protocol like: “M1234” to send 1234 to the motor and “L4321” to send 4321 to the LED.

Moondust:
For example i send new data to the leds but i want that the motors keep doing the same things. Or the other way around.

If you divide up your program into a series of short single-purpose functions it will be easier to develop and debug. For example you could have a function to read the switches and save their values, another to get serial data and save the values, another to make the motor move according to whatever values have been save and another to operate the LEDs.

Your code in loop() might be as simple as

void loop() {
  readSwitches();
  getSerialData();
  operateMotor();
  updateLEDs();
}

and each part can be tested separately. Have a look at Planning and Implementing a Program

...R
Several Things at a Time
Serial Input Basics - simple reliable non-blocking ways to receive data.

@Robin2

that are very nice tutorials, i’ve been reading them and it makes a lot of sence.

So i’ve tried to re-write my firmware but it isn’t what i was hoping. So mayby some more guidence?

this is my now result.

void loop()																	/* add main program code here, this code starts again each time it ends */
{
	buttonCheck();
	readSerialData();
	procesData();
}
void readSerialData()
{
	while (Serial.available() > 0)								//Read all serial data available, as fast as possible
	{
		char inChar = Serial.read();
		if (inChar == SOP)
		{
			index = 0;
			inData[index] = '\0';
			started = true;
			ended = false;
		}
		else if (inChar == EOP)
		{
			ended = true;
			break;
		}
		else
		{
			if (index < 70)
			{
				inData[index] = inChar;
				index++;
				inData[index] = '\0';
			}
		}
	}
	if (started && ended)
	{
		Serial.print("Ingelezen data = inData: ");				// Debug reading data
		Serial.println(inData);
		strcpy(tempinData, inData);
			
		delay(1);
			
		started = false;
		ended = false;
		index = 0;
		inData[index] = '\0';
	}
}
void procesData()
{
	char *token1 = strtok(tempinData, ",");
	int LedMotorSelection = atoi(token1);									// Select Led Mode
	Serial.print("LedMotorSelection: ");									// Debug token 1
	Serial.println(LedMotorSelection);
	
	char *token2 = strtok(NULL, ",");
	int Led = atoi(token2);
	Serial.print("Led: ");
	Serial.println(Led);
		
	char *token3 = strtok(NULL, ",");
	int LedPixelColor = atoi(token3);
	Serial.print("LedPixelColor: ");
	Serial.println(LedPixelColor);
	
	char *token4 = strtok(NULL, ",");
	int LedPixieColor = atoi(token4);
	Serial.print("LedPixieColor: ");
	Serial.println(LedPixieColor);
	
	char *token5 = strtok(NULL, ",");
	int LedPixelBrightness = atoi(token5);
	Serial.print("LedPixelBrightness: ");
	Serial.println(LedPixelBrightness);
	
	char *token6 = strtok(NULL, ",");
	int LedPixieBrightness = atoi(token6);
	Serial.print("LedPixieBrightness: ");
	Serial.println(LedPixieBrightness);
	
	char *token7 = strtok(NULL, ",");
	int Motor = atoi(token7);
	Serial.print("Motor: ");
	Serial.println(Motor);
	
	char *token8 = strtok(NULL, ",");
	int RotationSpeedDirection = atoi(token8);
	Serial.print("RotationSpeedDirection: ");
	Serial.println(RotationSpeedDirection);
	
	char *token9 = strtok(NULL, ",");
	int TiltingSpeedDirection = atoi(token9);
	Serial.print("TiltingSpeedDirection: ");
	Serial.println(TiltingSpeedDirection);
	
	char *token10 = strtok(NULL, ",");
	int PanningSpeedDirection = atoi(token10);
	Serial.print("PanningSpeedDirection: ");
	Serial.println(PanningSpeedDirection);
	
	if (LedMotorSelection == 0)
	{
		resetAllLed();
		ResetAllMotor();
	} 
	else if (LedMotorSelection == 1)
	{
		switch (Led)
		{
			case 0:
			Serial.println("Case 0 Led: Reset all");				//   <0,X,X,X,X,X,X,X,X>
			resetAllLed();
			break;
			case 1:
			Serial.println("Case 1 Led: Pixies Red");				//   <1,X,X,X,0-255,X,X,X,X>
			PixiesRed(LedPixieBrightness);
			break;
			case 2:
			Serial.println("Case 2 Led: Pixies Green");				//   <2,X,X,X,0-255,X,X,X,X>
			PixiesGreen(LedPixieBrightness);
			break;
			case 3:
			Serial.println("Case 3 Led: Pixies Blue");				//   <3,X,X,X,0-255,X,X,X,X>
			PixiesBlue(LedPixieBrightness);
			break;
			case 4:
			Serial.println("Case 4 Led: PixiesHeartbeat");			//   <4,X,0-9,X,X,X,X,X,X>
			PixiesHeartbeat(LedPixieColor);
			break;
			case 5:
			Serial.println("Case 5 Led: PixiesStrobo");				//   <5,X,0-9,X,X,X,X,X,X>
			PixiesStrobo(LedPixieColor);
			break;
			case 6:
			Serial.println("Case 6 Led: PixiesRandom");				//   <6,X,X,X,X,X,X,X,X>
			PixiesRandom();
			break;
			
			case 7:
			Serial.println("Case 7 Led: PixelsHeartbeat");			//   <7,0-9,X,X,X,X,X,X,X>
			PixelsHeartbeat(LedPixelColor);
			break;
			
			//Toevoegen
		}																// End switch Led sequence
		delay(1);
	}
	else if (LedMotorSelection == 2)
	{
		switch (Motor)
		{
		case 0:
		Serial.println("Case 0 Motor: Reset all");				//  <X,X,X,X,X,0,X,X,X>
		ResetAllMotor();
		break;
		case 1:
		Serial.println("Case 1 Motor: StartUpAllMotor");		//  <X,X,X,X,X,1,X,X,X>			SNELHEID BIJZETTEN
		StartUpAllMotor();
		break;
		case 2:
		Serial.println("Case 2 Motor: Rotation only");			//  <X,X,X,X,X,2,-150 to 150,X,X>
		RotationOnly(RotationSpeedDirection);
		break;
		case 3:
		Serial.println("Case 3 Motor: Tilting Only");			//  <X,X,X,X,X,3,X,-150 to 150,X>
		TiltingOnly(TiltingSpeedDirection);
		break;
		case 4:
		Serial.println("Case 4 Motor: Panning Only");			//  <X,X,X,X,X,4,X,X,-150 to 150>
		PanningOnly(PanningSpeedDirection);
		break;
		case 5:
		Serial.println("Case 5 Motor: Random all");				//  <X,X,X,X,X,5,X,X,X>
		RoTiPaRandom();
		break;
		case 6:
		Serial.println("Case 6 Motor: ManuAllMotor");			//  <X,X,X,X,X,6,X,X,X>
		ManuAllMotor(RotationSpeedDirection, TiltingSpeedDirection, PanningSpeedDirection);
		break;
		
		}																// End Switch Motor Sequence
		delay(1);
	}
	
}

But i’ve made a mistake in my thinking.
Now this is only 1 time done when i send the data.
I was thinking that when i used this way only the data was updated that belong to the right for loop.
But at the new cycle of the firmware all values are set to 0.
So i need something to keep the sended values and only change them when i send new values for the led or motor and still keep the others.

Define constants in your code that are programmed anew with every new firmware version.

Moondust:
this is my now result.

Please post the complete program as a single piece.

...R

This is the complete firmware.

i know i have to change some other things in the code but now it’s for the serial commuunication that i want to be alride.

LyteQuest_v2.0.ino (28.4 KB)

It's difficult to help with such a large program. When you have problems and need advice it is better to create a short program that illustrates the problem.

Is there a solution so that i can send some new values but keep the others.

I'm not completely clear about what you want to do. What is sending the new values - are you entering them at the Serial Monitor or are they coming from another program?

If the data is coming from another program then I reckon the solution is simple - send all of the items every time but only change the values that need to change.

That would probably be tedious and error-prone if you are typing the data into the Serial Monitor and in that case you need a very different approach - as suggested in Reply #4. The first character of M1234 is a signal to the Arduino that this is a value for a motor and the number 1 can tell the Arduino it is for motor1 (for example).

And if you want at the same time to send an instruction for a motor and an LED you could send M1234, L4321. You can split those up into two parts with the parse example in Serial Input Basics. And when you have the separate parts you can analyse them and act on them.

I note that you have a huge amount of delay()s in your program which means that it will be a long time between calls to readSerialData() so make sure that you never send more than 64 bytes. The delay()s also mean that you run the risk of reading Serial when a message has only partly arrived and it will be a long time before it can check for the rest of the message.

It will be a lot of work to re-write the program without delay()s but if you do then you could expect readSerialData() to be checked several times a second.

...R
...R

Now i send 1 data string.
for example: <1,1,0,0,0,255,0,5,125,0>

to test my firmware i use the serial monitor to send this data string.
But i’m also writing some software to control it. So i was trying to find a solution that when i want to control the leds that i only could use the first 6 values and i could forhget the motor values but that the motors keep doing the same stuff. And when i like to control the motors with new data that the leds keep shining the color and brightness whithout any change.

i was the closed to my problem when i use the out command serial reading part.
So mayby i just need to rewrite my controller software. and send Always the full data string.

Omit in your string all values which do not change. E.g. <1,0> changes only the first and last variable in the receiver code.

Moondust:
So mayby i just need to rewrite my controller software. and send Always the full data string.

I reckon that is by far the simplest solution.

...R