How would I change this code to toggle on off the LEDs?

UKHeliBob:
You are still reading serial input when there may not be any data to read. You still don't know what the value of myCol is when you use readBytesUntil() but no data is available, or at any other time for that matter. What sort of frequency does the LED flash at ? It would not happen to be once per second by any chance, would it ? That is the default value for serial timeout (used by readBytesUntil()) unless you have changed it.

yes it does flash at a roughly once per second, maybe a bit faster, and if I click the other buttons then it will turn off the led.

Now I'm not very good with a lot of this stuff and I'm new to Arduino code, but it seems to me that there should be a simple way to do what I need done here?

aarg:
Asked in reply #29, no answer.

Sorry, too many things to keep track of :wink:
About once per second flashes.

UKHeliBob:
You are still reading serial input when there may not be any data to read. You still don't know what the value of myCol is when you use readBytesUntil() but no data is available, or at any other time for that matter. What sort of frequency does the LED flash at ? It would not happen to be once per second by any chance, would it ? That is the default value for serial timeout (used by readBytesUntil()) unless you have changed it.

The guy who did the Unity Communication to Arduino had mentioned the following about why he has the serial time out set for 16:

"In my example you’ll note that sp.ReadTimeout is set to 16. This is so the scene can maintain a framerate of near to 60fps. (16*60 – 960ms). The reason for this is that if the Arduino is not sending any data, Unity will pause for the duration set in ReadTimeout. A number of 200 will drop the frame rate to 5fps. This would be bad and can be seen in my Ultrasonic test from earlier in the year."

it seems to me that there should be a simple way to do what I need done here?

Wrap the whole of the code in your loop() function in a test to see whether there serial data is available

if (Serial.available())
  {
    //code from loop()
  }

@Knightriderguy, you are talking about the timeout on the PC side. We are talking about the timeout on the Arduino side, in the readBytesUntil() function.

OK I did a little playing around in my Unity code, I went to a different scene where I knew I had a button that was coded with a toggle function like this:

public void TogglePlay()
	{
		if(GetComponent<AudioSource>().isPlaying)
		{
			GetComponent<AudioSource>().Stop();
			Sending.sendRedOff ();
		}
		else
		{
			GetComponent<AudioSource>().Play();
			Sending.sendRed ();
		}
	}

I then changed the "Sending" script like this:

public static void sendRed(){
    	sp.Write("r");
    }

	public static void sendRedOff(){
		sp.Write("1");
	}

See how I added the "sendredOff"? I originally tried it with ro for red off but it did not work so I just put a number 1 in there.

In my Arduino code I did this:

void loop() {
  int lf = 10;
  Serial.readBytesUntil(lf, myCol, 1);
    if(strcmp(myCol,"r")==0)
   {
       //redState = !redState;
       digitalWrite(rLed, HIGH); //Turns Red LED on
   }

   if(strcmp(myCol,"1")==0)
   {
       //redState = !redState;
       digitalWrite(rLed, LOW); //Turns Red LED off
   }

And this seems to work.... now like I say being new to Arduino and not being the greatest at code to begin with I don't know if this is the best way but it did work.... seems to me that there should be an easier way though?

I fail to see why you need to use readBytesUntil() and/or strcmp() when all of your commands are one letter.

if(Serial.available() > 0)
{
   chat ltr = Serial.read();
   switch(ltr)
   {
       case 'r':
          redState = !redState;
          digitalWrite(rLed, redState); //Toggles RED led.
          break;
       // other cases
   }
}

PaulS:
I fail to see why you need to use readBytesUntil() and/or strcmp() when all of your commands are one letter.

if(Serial.available() > 0)

{
  chat ltr = Serial.read();
  switch(ltr)
  {
      case 'r':
          redState = !redState;
          digitalWrite(rLed, redState); //Toggles RED led.
          break;
      // other cases
  }
}

I don't know, I am simply following what he had on his blog.... a lot of my problem is when people respond with a code snippet they just put part of it like I should know what else is missing.... and I don't :wink:
I'm only as good as the information I have.
Complete explanations are best and complete codes so I can see all that needs to be done. But yeah if there is an simpler solution I'm all ears my friend :wink:

Whatever method you use to read incoming data and act on it, please only read from serial when there is something to read. Notice how Paul wrapped his code in a test to determine whether there was anything available to read as I suggested you do.

UKHeliBob:
Whatever method you use to read incoming data and act on it, please only read from serial when there is something to read. Notice how Paul wrapped his code in a test to determine whether there was anything available to read as I suggested you do.

OK you did see I was able to get it to work one way. I think he (The guy who originally did the tutorial) did it a certain way because of the way unity works with frames rates and crap like that so I'm not sure.
plus it's hard for me to try something if someone just posts a snippet of code because I get confused with what I need from it Vs what I have. Not sure if that makes sense but like I say I'm not the greatest at code and I am new to Arduino so it's tricky understanding the relationship between the two. but I'm not trying to break new ground here, I have seen people do this on YouTube but they never answer their messages to be able to ask them how they did it.
All I need to be able to do it turn an LED on and off with a single button from Unity 3D... simple I would have though.

Now the function he has where you turn one LED on and the others turn off I can find a use for that in my software for sure but it's not what I ma trying to deal with just this moment. :wink:

If I have interpreted correctly what is going on it is as follows.
You click a button in Unity and it sends a single character via a serial link to the Arduino. In your Arduino code you are constantly reading the serial interface using the readBytesUntil() function until an linefeed character turns up or the serial wait times out. Whether or not the linefeed character has been found or the serial wait has timed out the value of the myCol variable is tested and the state of the associated LED is changed.

If that is the case and the value of the myCol variable has not changed because no linefeed was received then the LED will still change state. This seems to be the problem that you are describing. If that is the case then you need to take action on the Arduino only when there is serial data available which will prevent the existing value of the myCol variable causing the LED to change state.

Something like this

start of loop()
  if serial data is available
    read serial
    if a trigger character is received
      change the state of the associated LED
    end if
  end if
end of loop()

UKHeliBob:
If I have interpreted correctly what is going on it is as follows.
You click a button in Unity and it sends a single character via a serial link to the Arduino. In your Arduino code you are constantly reading the serial interface using the readBytesUntil() function until an linefeed character turns up or the serial wait times out. Whether or not the linefeed character has been found or the serial wait has timed out the value of the myCol variable is tested and the state of the associated LED is changed.

If that is the case and the value of the myCol variable has not changed because no linefeed was received then the LED will still change state. This seems to be the problem that you are describing. If that is the case then you need to take action on the Arduino only when there is serial data available which will prevent the existing value of the myCol variable causing the LED to change state.

Something like this

start of loop()

if serial data is available
    read serial
    if a trigger character is received
      change the state of the associated LED
    end if
  end if
end of loop()

Yes a single character gets sent from Unity but I am still not fully understanding what code I need to implement to make this work. A more complete code with an explanation would help out greatly short sections of code are just more confusing.

Have you tried what I suggested in reply #43 ?

UKHeliBob:
Have you tried what I suggested in reply #43 ?

Well I have been messing around with it for hours and it's quite a mess and very confusing as to what I need and what is garbage. Perhaps you can help clean it up if you would not mind?

int gLed = 10;
int yLed = 11;
int rLed = 12;
int bLed = 9;

//byte redState = LOW;
char myCol[20];

void setup() {  
   Serial.begin (9600);  
   pinMode(gLed, OUTPUT); //Sets all the LED pins to Output on start up   
   pinMode(yLed, OUTPUT);   
   pinMode(rLed, OUTPUT);
   pinMode(bLed, OUTPUT);   
  
   digitalWrite(gLed, LOW); //turns all LEDs off
   digitalWrite(yLed, LOW);
   digitalWrite(rLed, LOW);
   digitalWrite(bLed, LOW); 
 
}


void loop() {
  int lf = 10;
  //Serial.readBytesUntil(lf, myCol, 1);
  if (Serial.available())
    if(strcmp(myCol,"r")==0)
   {
       //redState = !redState;
       digitalWrite(rLed, HIGH); //Turns Red LED on
   }

   if(strcmp(myCol,"1")==0)
   {
       //redState = !redState;
       digitalWrite(rLed, LOW); //Turns Red LED on
   }

  if(strcmp(myCol,"y")==0){
      digitalWrite(yLed, HIGH); //Turns Yellow LED on
   }
  if(strcmp(myCol,"g")==0){
      digitalWrite(gLed, HIGH); //Turns Green LED on
   }
   if(strcmp(myCol,"b")==0){
      digitalWrite(bLed, HIGH); //Turns Blue LED on
   }
   
}
  if (Serial.available())
    if(strcmp(myCol,"r")==0)
   {

Did you forget to read something from the serial interface there?

AWOL:

  if (Serial.available())

if(strcmp(myCol,"r")==0)
  {



Did you forget to read something from the serial interface there?

It looks like I have that? What did I miss?

Serial.available() tells you how many bytes of data are available, if any
Serial.read() actually reads a byte of data.
You have the former but not the latter.

With

Serial.readBytesUntil(lf, myCol, 1);

It gets something, LEDs still flash instead of doing what I need them to bloody do.

with the

if (Serial.available())

Nothing happens?

Please post your complete program that uses Serial.available() and Serial.read()

Have you tried my suggestion from reply #43 ?

Ok I tried attaching the original zip file that had the sample project I was following so you could take a look for yourself at the project but it was not uploading so I'll have to stress this link again where you can download and read about the original project in better detail than I can obviously explain as I'm getting nowhere with this.

Here is the page link that has the project that you can download:

http://r3dstar.co.uk/?p=211