Multiple serial signals at once

Hello everyone,

i'm working on a project with arduino and c# and i have to send multiple signals from c# to arduino.
every second there has to go a signal "REQUEST_TEMPERATURE#" from c# to arduino but i also need to be able to receive the signal "ENGINE_1_ON#" and "ENGINE_1_OFF#". It works normal until i send ENGINE_1_ON/OFF then it stops working.
I'm having alot of trouble and don't know what to do anymore any ideas ?

my project is in the attachements

cmonmanarduino.ino (1.09 KB)

It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

When using Cstrings you must use strcmp() to compare values rather than ==

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

You will make things a great deal easier for yourself if the C# program sends single-character commands - for example 'N' for "ENGINE_1_ON#" and 'F' for off. Using the alphanumeric characters you can have 62 different single-character commands. Communication between two computer programs should be made convenient for the computers rather than for a human.

I don't understand what you mean exactly by "stops working" - can you post some output from the programs. Put some Serial.print() statements in the Arduino program so you can see what it is doing. Test it with the Arduino Serial Montor.

You probably should have a two-way dialogue so that the C# program only sends a message when it knows the Arduino is ready.

This line in loop() is wrong

void engineTemp();

it should be

engineTemp();

...R

PS.. You can's send Serial signals at once. Serial means one after the other.

OPs original code

#include <OneWire.h> 
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2 
OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature sensors(&oneWire);

String data; <<<====  Avoid Strings
int ledPin = 11;

void setup(void) 
{ 
 pinMode(ledPin, OUTPUT);
 Serial.begin(9600); 
 sensors.begin(); 
} 

void loop() 
{ 
  data = Serial.readString();  
  tempSensor();
  engineControl();
  void engineTemp();  <<<====
} 

void tempSensor()
{
  if(data == "REQUEST_TEMPERATURE#")
  {
    sensors.requestTemperatures(); 
    Serial.println(sensors.getTempCByIndex(0));     
  }
}
void engineControl()
{
  if(data == "ENGINE_1_ON#")
  {
    digitalWrite(ledPin, HIGH);
  }

  else if(data == "ENGINE_1_OFF#")
  {
    digitalWrite(ledPin, LOW);
  }
}

void engineTemp()
{
  if((data == "REQUEST_TEMPERATURE#") && (data == "ENGINE_1_OFF#"))
  {
    digitalWrite(ledPin, LOW);
    sensors.requestTemperatures(); 
    Serial.println(sensors.getTempCByIndex(0));  
  }

  else if((data == "REQUEST_TEMPERATURE#") && (data == "ENGINE_1_ON#"))
  {
    digitalWrite(ledPin, HIGH);
    sensors.requestTemperatures(); 
    Serial.println(sensors.getTempCByIndex(0)); 
  }
}

If you want the sensor values 'quickly', you must remember the REQUEST function takes a little while before the GET can return a value.
If you need the other processes to run smoothly (non-blocking), try interleaving the REQUESTS & GETs with a simple state machine so the code doesn't block while waiting for each request to be completed by the sensors