Note that there are some explanatory texts on larger screens.

plurals
  1. POSending UISlider value using AsyncUdpSocket to Arduino via UDP
    text
    copied!<p>i am trying to send the UISlide value to Arduino Uno (With wifi shield) to control the Servo attached.</p> <p>The following is the Xcode i have made. i am using the AsyncUdpSocket class , also tried with the GCDAsyncUdpSocket. Ok i managed to get it to work by clearing the buffer after every value received. Now the problem is the Lag :( .. </p> <blockquote> <p>-(void)viewDidLoad { [super viewDidLoad];<br> mySocket = [[AsyncUdpSocket alloc]initWithDelegate:self]; gcdSocket = [[GCDAsyncUdpSocket alloc]initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; mySlide.minimumValue = 0; mySlide.maximumValue = 180; // Do any additional setup after loading the view, typically from a nib. }</p> <p>-(IBAction)slideValueChange:(UISlider*)sender{<br> NSInteger value = lround(sender.value); myLabel.text = [NSString stringWithFormat:@"%d",value]; NSString * address = @"192.168.1.7"; UInt16 port = 8888; NSData *myData = [myLabel.text dataUsingEncoding:NSUTF8StringEncoding];</p> </blockquote> <pre><code>//[gcdSocket sendData:myData toHost:address port:port withTimeout:-1 tag:2]; [mySocket sendData:myData toHost:address port:port withTimeout:-1 tag:2]; } </code></pre> <blockquote> <p>-(void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }</p> <p>-(IBAction)sendMessage:(id)sender {</p> </blockquote> <pre><code>NSString *message = [[NSString alloc]init]; NSString * address = @"192.168.1.7"; message = @"test"; UInt16 port = 8888; NSData * data = [message dataUsingEncoding: NSUTF8StringEncoding]; [mySocket sendData:data toHost:address port:port withTimeout:-1 tag:1]; </code></pre> <p>}</p> <blockquote> <p>-(void)onUdpSocket:(AsyncUdpSocket *)sock didSendDataWithTag:(long)tag {</p> </blockquote> <pre><code>if (tag == 1){ NSLog(@"something is happening !!"); } else if (tag == 2){ NSLog(@"received message from slider");}} </code></pre> <blockquote> <p>Following is my arduino code</p> </blockquote> <pre><code>Servo servo ; int status = WL_IDLE_STATUS; char ssid[] = "Allcad"; // your network SSID (name) char pass[] = "gauravsoni"; unsigned int localPort = 8888 ; char PacketBuffer [ UDP_TX_PACKET_MAX_SIZE ] ; WiFiUDP Udp; void setup () { Serial.begin(9600); // 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); } Serial.println("Connected to wifi"); printWifiStatus(); Serial.println("\nStarting connection to server..."); Udp.begin(localPort); servo.attach ( 6 ) ; } void printWifiStatus() { // print the SSID of the network you're attached to: Serial.print("SSID: "); Serial.println(WiFi.SSID()); // 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"); } void loop ( ) { int packetSize = Udp.parsePacket() ; if ( packetSize ) { Udp.read ( PacketBuffer , UDP_TX_PACKET_MAX_SIZE ) ; int num = 0; num = atoi ( PacketBuffer ) ; Serial.print("New num value is: "); Serial.println(num); servo.write (num) ; for(int i=0;i&lt;3;i++) { Serial.println(i); PacketBuffer[i] = 0; } } delay ( 5 ) ; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload