Hi! I´m pretty new to this and was wondering if there is any possibility to detect whether there is serial communication or not.
The thing is that I want to control an arduino Uno with Processing but still have the possibility to manuever the equipment without a PC connected.
How do I write to use hard buttons AND serial commands if there is serial connection and just hard buttons if there is not. I don´t neccesary need the possibility to disconnect the serial connection while the program is running so it´s enough if the detection is done when the Arduino powers up.
I tried this way of thinking below but the else-part runs regardless if there is serial connection or not...
if(Serial.available() > 0)
{
code that runs if there is a serial connection
}
else
{
code that runs if there is no serial connection
}
I would really appretiate some help with this.
Just handle any incoming serial commands.
If no serial is connected nothing will come in.
Thanks for a quick response.
Since I´m pretty new to this I don´t quite understand how to do that. Some code would really help.
Let´s say I want to light up an LED while I press a button and when I let go of the button the LED goes out. I want to be able to light up that LED from a hard button regardless of whether I´m connected via serial communication or not, but if I´m connected I want to light up that same LED from both the hard button and via a button in Processing.
My problem is that if I´m connected to Processing the hard buttons off-state overrides the serial command since I haven´t found a way to reparate code depending on if there is a serial connection or not.
Malte75:
Some code would really help.
I absolutely agree.
Why didn't you post it?
Please post your complete code as descibed in Read this before posting a programming question ...
Whandall:
I absolutely agree.
Why didn't you post it?
I have no code to post, that´s why.
I´m simply asking how to write a condition to check if there is no serial communication like the example in the beginning of the code.
I decided to be nice and give you small working example.
Builtin led is controlled via serial commands "on", "off" and "t",
a button connecting to GND on pin 2 toggles the same led.
The button works with or without serial input.
#include <Bounce2.h>
const byte buttonPin = 2;
Bounce button;
void setup() {
Serial.begin(250000);
pinMode(LED_BUILTIN, OUTPUT);
button.attach(buttonPin, INPUT_PULLUP);
}
void loop() {
serialHandler();
if (button.update()) {
if (button.fell()) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
}
}
void processCmd(const char* buf) {
if (!strcmp_P(buf, PSTR("on"))) {
digitalWrite(LED_BUILTIN, HIGH);
} else if (!strcmp_P(buf, PSTR("off"))) {
digitalWrite(LED_BUILTIN, LOW);
} else if (*buf == 't') {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
}
void serialHandler()
{
const byte sCBMax = 30;
static char sCBuffer[sCBMax];
static byte buffIndex = 0;
byte inChar;
bool doCheck = false;
while (Serial.available()) {
inChar = Serial.read();
if (inChar == 13) {
doCheck = true;
} else if (inChar != 10) {
if ((buffIndex == 0) && isWhitespace(inChar)) {
continue;
}
sCBuffer[buffIndex++] = inChar;
doCheck = (buffIndex == (sCBMax - 1));
}
if (doCheck) {
sCBuffer[buffIndex] = 0;
doCheck = false;
if (buffIndex != 0) {
processCmd(sCBuffer);
buffIndex = 0;
}
}
}
}
Thank you very much! I´ll look into it tomorrow, it´s past midnight here.
Been googling around all day and finally got it working. This is how I did it. Only thing is that the Arduino has to be reset to detect loss of communication but that´s fine by me.
A call for the function establishContact() in the setup. The comPin is just for indicating with a LED if there is communication or not.
void establishContact()
{
delay(100);
if (Serial.available() <= 0)
{
digitalWrite(comPin, LOW);
serialState = 0;
}
else
{
digitalWrite(comPin, HIGH);
serialState = 1;
}
}
Malte75:
The comPin is just for indicating with a LED if there is communication or not.
No.
The led shows that at least one byte arrived in the first 100 ms after startup.
Well, since the goal was to see if a PC is used together with the arduino or not I feel that I´ve reached what I was aiming for. 
Don't mention it in any advertisement, it will not work reliably. 
So what you are saying is that I can not be sure that there is a computer connected to the arduino if there is serial communication?
No.
All you can tell with your methode is that at least one byte arrived in the first 100 ms after startup,
which is a 100ms window around 1000ms after reset.
That is not directly connected to 'something is attached to serial'.
It needs a program on the PC that opens the port and starts transmitting to hit the 100mS window.
I would use a 'if nobody answers my messages there is nobody attached' approach
and only in the case that there is no communication from the attached device for some time.