JavaScript

Integers

Some JavaScript commands require integers and not strings. The parseInt() function parses a string and returns an integer.

Here is an example:

distance = parseInt(distance);

Increment / Decrement Servo Position

If you want to create buttons or links to increment or decrement the current servo position, you can add some JavaScript to your application.

What you need is a Variable Servo Widget to start. Get the embed code and copy-and-paste it into the body of an HTML web page. You can hide the widget and use the JavaScript API to send positions to the widget.

In the following source code, replace the “widgetID” of “k7bgNMyCTW9b” with your actual “widgetID” of the variable servo position widget created on the ioBridge.com Interface.

<html>
<title>Servo Control Example</title>
<head>

<script type="text/javascript">

function moveServo(widgetID, direction, distance){

  distance = parseInt(distance);
  current = parseInt(widgetGetString(widgetID));

  if (direction == "CCW") {
       distance = current-distance;
  }
  else {
       distance = current+distance;
  }

   widgetSetString(widgetID, distance);

}

</script>
</head>

<body>

Click the arrow buttons to move the servo Clockwise or Counter-clockwise.

<br /><br />

<form><input type="button" Name="CounterClockWise" Value="<-" onClick="moveServo('m1tbq9qLgqFO', 'CCW', '40')"><input type="button" Name="ClockWise" Value="->" onClick="moveServo('m1tbq9qLgqFO', 'CW', '40')"></form>

<div id="ioWidgets" style="display:none;">
<script type="text/javascript">document.write(unescape("%3Cscript src='" + "http://www.iobridge.com/widgets/io.js?m1tbq9qLgqFO' type='text/javascript'%3E%3C/script%3E"));</script>
</div>

</body>
</html>

Special Characters

If you want to send special characters and bytes using JavaScript, use URL encoded characters.

For example, if you wanted to break a message into two lines with a carriage and linefeed, issue this JavaScript command:

widgetSetString('Q6Vs3RepsH6I','This is line 1.%0D%0AThis is line 2.');

The result will be:

This is line 1.

This is line 2.

%0D = carriage return

%0A = linefeed

Here is a table that lists all of the URL codes:

URL Codes

Additional Resources