[SOLVED] Serial.available and uploading issues

I am having two issues with my code:

  1. While the RX LED lights up when I send data to the Arduino UNO I am using, the built-in light continues to blink(as Serial.available is not greater than 0).
  2. After uploading new code, then running my other program(sending serial data), I get the error ser_open(): can't open device "\.\COM3": Access is denied.

I am not sure what is going wrong in either case, any help would be greatly appreciated!
P.S. The upload error fixes when I unplug and reconnect the board.

int inputData;
void setup() {
  // put your setup code here, to run once:
  // start serial port at 9600 bps:
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT); 
  digitalWrite(LED_BUILTIN, LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()>0)
  {
    inputData = Serial.read();
    digitalWrite(LED_BUILTIN, HIGH);
  }
  else
  {
    digitalWrite(LED_BUILTIN, HIGH);
     delay(150);
     digitalWrite(LED_BUILTIN, LOW);
     delay(500);
  }
}

For question 1, I think it would be difficult to visually detect the longer flash when Serial.available>0 because it would only be a few ms longer than otherwise, e.g. 155ms Vs 150ms.

Maybe hook up another led and switch that on when Serial.available>0 and switch it off when LED_BUILTIN is switched off.

Do you realise how fast 9600 baud is? ... 9600 bits a second... roughly 1000 bytes (characters) a second.

So unless you have constant stream of data coming in if (Serial.available()>0) is only going to be true for a very short time. As @PaulRB said.. you won't be able to detect this.

What code?

Just editing/attempting to fix these bugs. This is the latest iteration I had at the time of posting.

My current data stream is also transmitting at 9600 baud, so shouldn't that cover for this issue?

It depends how much data you are actually sending. If you send 10 characters if will take approximately 10 milli seconds to receive all the data.

I see. I have discovered that by adding a delay of 10 miliseconds that seems to clear up the issue nicely, aside from one small issue: Now, when I stop sending serial data, there is visible delay before the BUILTIN LED turns off, about a second after the RX led turns off. Is there any way to prevent that?

Where did you put the delay?

Would be useful to see that actual code.

int inputData;
void setup() {
  // put your setup code here, to run once:
  // start serial port at 9600 bps:
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT); 
  digitalWrite(LED_BUILTIN, LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (Serial.available()>0)
  {
    inputData = Serial.read();
    digitalWrite(LED_BUILTIN, HIGH);
    delay(10);
  }
  else
  {
    digitalWrite(LED_BUILTIN, HIGH);
     delay(150);
     digitalWrite(LED_BUILTIN, LOW);
     delay(500);
  }
}

Right below the digitalWrite if data received!

... and what data are your sending? This code blocks 10 ms between each character.

At the moment I am simply sending the string "1" from unity when it detects the spacebar key pressed down.

After digging around, I realize this only update 50 times a second. As such, do I need a delay of 20 between checks?

What only updates 50 times a second?

Unity's FixedUpdate, which is where I am calling my send method from.

Ok. Please remember this is not the Unity forum, few here will be familiar with Unity.

Not necessarily. But we don't really understand what you are trying to do yet.

50 times a second, I am sending a single character, either "1" or "0". When it is "1", I want to have the light turn on, and once it returns to "0", the light turns off.

Returns? You mean when the next "0" is received?

Assuming that's it, no, you don't need any delays at all. Switch the led on when a "1" is received and off when a "0" is received.

That... is a remarkably simple fix. Thanks for your help, no idea why I didn't think of that! :0