Author Topic: Multiple Servo Postions from one click?  (Read 1216 times)

jason

  • Administrator
  • Hero Member
  • *****
  • Posts: 502
    • ioBridge.com
Multiple Servo Postions from one click?
« on: January 03, 2009, 05:43:58 PM »
I was recently asked how to make a servo go to a set position wait 3 seconds and return home from single mouse click.  With the widgetExecute function and a little javascript, this can be done very easily.

First make 2 widgets, one for servo position A and another for servo position B.
Now assuming the widget ID's for A and B are XXXXXXXX and YYYYYYYY respectively....

Code: [Select]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<title>Servo Routine</title>

<script type="text/javascript">

function ServoRoutine(){
   var date = new Date();
   var curDate = null;
   
   // assuming the servo is already at position A

   widgetExecute('YYYYYYYY');    //  position B

   do { curDate = new Date(); }   //  wait 3 seconds
   while(curDate-date < 3000);     

   widgetExecute('XXXXXXXX');    //  position A

}</script>

</head>

<body>
<a onClick="ServoRoutine()">Servo Routine</a>

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

</body>
</html>
 
« Last Edit: January 05, 2009, 03:35:12 PM by jason »
Jason Winters
ioBridge Developer

Psychoarts

  • Newbie
  • *
  • Posts: 16
  • Peace, Love, and 1-Shot
    • Psychoarts.com
Re: Multiple Servo Postions from one click?
« Reply #1 on: January 03, 2009, 06:56:42 PM »
Thanks, I was playing with this earlier this afternoon and couldn't even come close.

WaveJam

  • Jr. Member
  • **
  • Posts: 63
    • WaveJam Technologies
Re: Multiple Servo Postions from one click?
« Reply #2 on: January 04, 2009, 07:00:31 AM »
What an excellent question this one was. ;-)  Thanks for the help Jason. Your code was a big help and worked great in IE but I could not get it to work in Firefox / Safari (and iPhone). Firefox and Safari would execute the pause first then the widgetExecute's back to back with no timeout. So I kept at it during the Colts/Chargers game last night and I wrangled up the following which is based off of Jasons code and works with IE, Firefox, Safari (and iPhone):

Code: [Select]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title></title>

<script language="javascript">
<!--

function ServoRoutine1(){
   
   // assuming the servo is already at position A

   widgetExecute('XXXXXXXXXX');    //  position B

   window.setTimeout(ServoRoutine2,3000);

}

function ServoRoutine2(){

   widgetExecute('YYYYYYYYYY');    //  position A

}

//-->
</script>

</head>

<body>

<a onClick="ServoRoutine1()">Servo Routine</a>

<div id="ioWidgets" style="display:none;">

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

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

</div>

</body>

</html>
« Last Edit: January 04, 2009, 08:40:37 AM by WaveJam »
Pete Raumann
WaveJam Technologies
http://www.wavejam.com
http://www.tankedcam.com

jason

  • Administrator
  • Hero Member
  • *****
  • Posts: 502
    • ioBridge.com
Re: Multiple Servo Postions from one click?
« Reply #3 on: January 04, 2009, 10:41:06 AM »
I'm not sure why that code didn't work for you.  I just tried it again, and it worked fine in Firefox, IE and Chrome.
Jason Winters
ioBridge Developer

WaveJam

  • Jr. Member
  • **
  • Posts: 63
    • WaveJam Technologies
Re: Multiple Servo Postions from one click?
« Reply #4 on: January 04, 2009, 10:48:40 AM »
Hmmm... Weird. I tried it on multiple machines and I checked the code over and over. Eh, maybe I had a typo. I have been known to fat finger a few (million) times. ;-)

Thanks Jason!
Pete Raumann
WaveJam Technologies
http://www.wavejam.com
http://www.tankedcam.com

rusnak

  • Newbie
  • *
  • Posts: 10
Re: Multiple Servo Postions from one click?
« Reply #5 on: July 17, 2010, 06:19:58 PM »
I am having the hardest time getting this to work. All i see is "Servo Routine" written in black. Nothing on the site is clickable. I tried safari and firefox and even ran both versions of the code above. I subbed in my widgets and my servo positions, but get nothing. Any ideas?