Difference between revisions of "Blinking LED"

From Nearwiki
Jump to: navigation, search
(Created page with " This simple example shows how to implement an infinite loop in the JavaSript code running on a Browser. File:hello_world_6.png ---- ===JavaScript Code=== <br /> In...")
 
(No difference)

Latest revision as of 21:00, 29 March 2018

This simple example shows how to implement an infinite loop in the JavaSript code running on a Browser.


Hello world 6.png



JavaScript Code


In order to run this code you should copy the following code into a file, save it with html extension and run it in your browser. Additionally you should replace the device_ID, user and password in this file.



<!DOCTYPE html>

<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="PAGE" ></script>
<script type='text/javascript' src='http://nearbus.net/downloads/js_apps/nearapi_v05.js'></script>

<script>
// ****************************************************************************************************************************** 
// * NEARBUS JAVASCRIPT EXAMPLE - www.nearbus.net                                                                               *
// * Description: This simple example shows how to implement an infinite loop in the JavaSript code running on a Browser        *
// * Support: info@nearbus.net                                                                                                  *
// *                                                                                                                            *
// * REVISION HISTORY                                                                                                           *
// * v0.1 - 09-08-13 - Initial Release                                                                                          *
// * v0.2 - 14-10-13 - Upgrade to support nearapi_0v5                                                                           *
// ******************************************************************************************************************************

// ********************************************************************************
// *                                 IMPORTANT                                    *
// * Be careful to avoid run more than one scripts simultaneously !!!             *
// * Don't forget CLOSE the running script before start a new or modified script. *
// * The NearBus system can run multiple scripts but the system output will be    *
// * the mix of all running sessions.                                             *    
// ********************************************************************************

// **********************************************************************
// * To run this example you should replace the following parameters    *
// * device_id         Your device ID, or.. devices ID :)               *
// * user              Your NearBus Web user                            *
// * pass              Your NearBus Web password                        *
// **********************************************************************

var device_id = "NB100***";   // Your device ID
var user      = "****";       // Your NearBus Web user
var pass      = "****";       // Your NearBus Web password

var sysEnable;

function sysStop() {
    sysEnable = 0;
}

function sysStart() {
    sysEnable = 1;
    sysControl();    
}
        
function sysControl(e){

var loop = 0;
var ret = 0;

    setInterval( function()
    {
        if( sysEnable == 1 ) 
        {
            if( loop == 1 ) {
                ret = NearAPIjs( "DIG_OUTPUT", device_id, 0, 0 );
                if ( ret == "DONE" ) {
		    loop = 0;     
		}					
            }
            else {        
                ret = NearAPIjs( "DIG_OUTPUT", device_id, 0, 1 );
		if ( ret == "DONE" ) {
		    loop = 1;     
		}	
            }
        }
        else
	{
            NearAPIjs( "DIG_OUTPUT", device_id, 0, 0 );	
        }
    }, 1000);    
}

</script>

</head>
<body>

<br />
<p><b> NEARBUS - BLINKING LED EXAMPLE</b></p>
<br />
<button onclick="sysStart()">START</button>
<br />
<br />
<button onclick="sysStop()">STOP</button>

</body>
</html>