Loop help appreciated!

Supporting Browser-based Javascript and Node.js
Post Reply
gazzla
Phidgetsian
Posts: 11
Joined: Fri May 01, 2020 9:36 am
Contact:

Loop help appreciated!

Post by gazzla »

Just a dabbler here and despite endless research I’m really struggling with making Javascript repeat this code block over and over a set number of times. It's for a Phidget rel1000_0 switch/relay (with a timer for duration).

I can't find a working method to make the process repeat itself.

Below is the working code example, with explanatory notes, from the website. No matter what looping method I cut and paste in, and no matter where it’s placed, it’s not working. Any help VERY welcome! Thanks.

Code:

var phidget22 = require('phidget22');

function runExample() {
//Create your Phidget channels
var digitalOutput0 = new phidget22.DigitalOutput();

//Set addressing parameters to specify which channel to open (if any)
digitalOutput0.setHubPort(2);
digitalOutput0.setDeviceSerialNumber(606877);

//Assign any event handlers you need before calling open so that no events are missed.

//Open your Phidgets and wait for attachment
digitalOutput0.open(5000).then(function() {

//Do stuff with your Phidgets here or in your event handlers.
digitalOutput0.setDutyCycle(1);

setTimeout(function () {
//Close your Phidgets once the program is done.
digitalOutput0.close();
process.exit(0);
}, 3000);
});
}
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Loop help appreciated!

Post by mparadis »

You should be able to use a regular javascript "for" loop, although looping "digitalOutput0.setDutyCycle(1);" won't do anything since you need to alternate between setting the duty cycle or state from 0 to 1 to switch the relay on and off. You can accomplish this by using an "if" statement inside the loop that checks the result of getDutyCycle or getState (or you can keep track of current duty cycle or state in your program with a variable).

If you want some amount of time to pass between relay switches, you can check this link to see how to make a "sleep" function that will pause the program for a certain number of milliseconds.
gazzla
Phidgetsian
Posts: 11
Joined: Fri May 01, 2020 9:36 am
Contact:

Re: Loop help appreciated!

Post by gazzla »

That sounds hugely helpful - many thanks :)

Guess I was rather optimistic, given my last experience with coding was with Basic 35 years ago! :D
gazzla
Phidgetsian
Posts: 11
Joined: Fri May 01, 2020 9:36 am
Contact:

Re: Loop help appreciated!

Post by gazzla »

Embarrassingly, I'm still having problems. If some kind soul has a brief minute to spare, could they add some basic looping code into this - say to make it run five times?

I can get non-Phidget loop examples working elsewhere but just not within this (presumably due to the positioning). I'd be so grateful!

Huge thanks in advance.

Code: Select all


var phidget22 = require('phidget22');

function runExample() {
	//Create your Phidget channels
	var digitalOutput0 = new phidget22.DigitalOutput();

	//Set addressing parameters to specify which channel to open (if any)
	digitalOutput0.setHubPort(2);
	digitalOutput0.setDeviceSerialNumber(606877);

	//Assign any event handlers you need before calling open so that no events are missed.

	//Open your Phidgets and wait for attachment
	digitalOutput0.open(5000).then(function() {

		digitalOutput0.setDutyCycle(1);

		setTimeout(function () {
			//Close your Phidgets once the program is done.
			digitalOutput0.close();
			process.exit(0);
		}, 3000);
	});
}

var conn = new phidget22.Connection(5661, 'localhost');
conn.connect().then(runExample);
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Loop help appreciated!

Post by mparadis »

Here's what it should look like:

Code: Select all


var phidget22 = require('phidget22');

function sleep(ms) {
    return new Promise(resolve => setTimeout(resolve, ms));
}

function runExample() {
   //Create your Phidget channels
   var digitalOutput0 = new phidget22.DigitalOutput();

   //Set addressing parameters to specify which channel to open (if any)
   digitalOutput0.setHubPort(2);
   digitalOutput0.setDeviceSerialNumber(606877);

   //Assign any event handlers you need before calling open so that no events are missed.

   //Open your Phidgets and wait for attachment
   digitalOutput0.open(5000).then(async function() {

   var i;
   for(i=0; i < 6; i++) {
    await sleep(1000);
    if(digitalOutput0.getState() == 0)
      digitalOutput0.setState(1);
    else
      digitalOutput0.setState(0);
   }

      setTimeout(function () {
         //Close your Phidgets once the program is done.
         digitalOutput0.close();
         process.exit(0);
      }, 3000);
   });
}

var conn = new phidget22.Connection(5661, 'localhost');
conn.connect().then(runExample);
- Instead of setting duty cycle I set state, since you mentioned you'd be using it with a relay. (Duty cycle is just for devices that support dimmer control using pulse-width modulation, although setting duty cycle to 1.0 or 0 is equivalent to setting state to 1 or 0, respectively).

- I check what the state of digitalOutput0 is, and then set it to whatever is opposite using the if/else statement. The "for" loop causes it to loop 5 times.

- As I mentioned before, getting a functioning "sleep" command in javascript is a bit roundabout because of the way the language is structured. The fastest way is to create the "sleep" function you can see defined above our runExample function. Then you can call "await sleep (x)" where x is the number of milliseconds to wait. The only caveat is that await can only be used in an asynchronous function so it doesn't block. That's why I had to add "async" to the function we're calling after open completes.
gazzla
Phidgetsian
Posts: 11
Joined: Fri May 01, 2020 9:36 am
Contact:

Re: Loop help appreciated!

Post by gazzla »

Hero! Thank you so much (fingers crossed!)

Update: Works a treat. Thanks so much again :D
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests