Pages: [1]   Go Down
Author Topic: Arduino to unity3d & Ipad  (Read 711 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hello all,

my project is as follows..
we have a unity3d fps app.
the app has a tcpclient which connects to arduino with a wifishield..
the arduino itself is wired to a nerf gun, so when the user fires the nerf gun..the gun inside the app fires too.
when i try the same app on a mac using serial cable the result is immediate. no lag between the user firing and the app responding.
but when using wifi for connectivity theres a 2 sec lag..which for a fps is terrible.

is this a wifi shield related problem?
can we remove the lag using some wifi firmware?
should i simply use the redpqrk serial cable?
should i use a different wifi shield? how about the wifly shield?
or should i use the ethernet shield with some portable wireless router?

plz give your suggestions and share if you have done something similar.
regards
danneskjold
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49080
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
is this a wifi shield related problem?
Maybe. Or a coding issue. My bet would be a coding issue.

Quote
can we remove the lag using some wifi firmware?
Doesn't seem likely.

Quote
should i simply use the redpqrk serial cable?
Does that work for your application?

Quote
should i use a different wifi shield? how about the wifly shield?
What about it? What wifi shield are you using now?

Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey paul,

thnx for replying...
i dont think it would be a coding issue.. all im sending is a client.print on a pushbutton.
i am able to use the serial.print and get immediate results when trying the same app on my mac...so i belive that the redpark would work..plz corr3ct me if im wrong.
im using this wifi shield
arduino.cc/en/Main/ArduinoWiFiShield
it cannot act as a ap or client. so the communication goes from arduino to router to app on ipad.

maybbe the eternet with the tplink router might speed things up
regards
danneskjold
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49080
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
i dont think it would be a coding issue.
I'd like to have a nickel for everyone who ever posted here saying that there code was perfect, when coding changes were needed to solve their problem. But, I don't have any problem with your not posting code.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

dear paul,
i completely agree with your thoughts... i have not said that my code is perfect.... i havent posted the code as im not on my computer..
ill do that first thing in the morning..
if anything written in post has misled you in thinking so..then im sorry and will try to make my posts more descriptive...

thnx for replying..plz have a look tomorrow and help me fix the code..

regards
danneskjold
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

dear paul,

here is the code im using to send parameters to unity .. in unity app theres a tcpclient which connects to arduino and recieves these parameters and executes corresponding events.
for ex..
client.read eq 1 fire
client.read eq 2 reload etc
 plz have a look and help me correct this.

regards
danneskjold


ARDUINO CODE..

#include <SPI.h>
#include <WiFi.h>

char ssid[] = "WLL-1"; //  your network SSID (name)
char pass[] = "97531";    // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;
const int shootpin = 2;
const int reloadpin = 9;
const int switchpin = 8;
const int zoompin = 5;
int led = 13;
WiFiServer server(23);

boolean alreadyConnected = false; // whether or not the client was connected previously

void setup() {
  //Initialize serial and wait for port to open:
 
  Serial.begin(9600);
  pinMode(shootpin,INPUT);
  digitalWrite(shootpin,HIGH);

  pinMode(reloadpin,INPUT);
  digitalWrite(reloadpin,HIGH);

  pinMode(switchpin,INPUT);
  digitalWrite(switchpin,HIGH);

  pinMode(zoompin,INPUT);
  digitalWrite(zoompin,HIGH);

  pinMode(led, OUTPUT);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
 
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue:
    while(true);
  }
 
  // attempt to connect to Wifi network:
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:   
    status = WiFi.begin(ssid, pass);
    // wait 10 seconds for connection:
   delay(10000);
  }
  // start the server:
  server.begin();
  // you're connected now, so print out the status:
  printWifiStatus();
}

void loop()
{
  WiFiClient client = server.available();
  if (client)
  {
    if (!alreadyConnected)
    {
      client.flush();   
      Serial.println("We have a new client");
      client.println("Hello, client!");
      alreadyConnected = true;
    }
    else if (digitalRead(shootpin) == LOW)
    {
      client.println(1);
    }
    else if (digitalRead(switchpin) == LOW)
    {
      client.println(4);
    }
    else if (digitalRead(reloadpin) == LOW)
    {
      client.println(3);
    }
    else if (digitalRead(zoompin) == LOW)
    {
      client.println(5);
    }
    else
    {
      client.println();
    }
    client.flush();
    delay(100);
  }
}

void printWifiStatus() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());
  digitalWrite(led, HIGH);
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49080
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
   else
    {
      client.println();
    }
On every pass through loop, if no switch is pressed, send a carriage return and line feed. Why? You will be sending a lot of these.

Code:
   delay(100);
"My program takes too long" and this call are NOT compatible.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hi paul,

if i dont send a blank print if no switch is pressed.. the shield disconnects after sometime. .like 10 secs.
ill remove the delay and try..hopefully that should fix things
i have tried this on 3 setups..
1.windows 7 machine with wifi...works perfect
2.a mac mini over wifi. .. lags
3. on android note/ipad. ..lags almost the same as the mac mini.


regards
danneskjold
Logged

UK
Offline Offline
Shannon Member
****
Karma: 223
Posts: 12631
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

if i dont send a blank print if no switch is pressed.. the shield disconnects after sometime. .like 10 secs.

If you need to send something at regular intervals to keep the connection alive (seems strange to me, but may be the case) then I suggest you do it on a timed basis e.g. once per second rather than on every time through loop(). Look at the 'blink without delay' example to see how to do things on a timed basis.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey peter,

thnx... just checking that out
regards
danneskjold
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

hey paul/peter,

removed the delay from the previous code.. also removed the empty client.print.
still no luck..the same lag persists..

now what do i need to check???
suggestions plz..

regards
danneskjold
Logged

Pages: [1]   Go Up
Jump to: