Supporting Browser-based Javascript and Node.js
MarshaJ847
Fresh meat
Posts: 2 Joined: Sun Feb 06, 2022 4:17 pm
Post
by MarshaJ847 » Tue Mar 01, 2022 6:17 pm
I'm trying to update some Phidgets 21 code to 22.
Trying to run a motor one direction, wait 5 seconds and run back the other way. 1065 PhidgetMotorControl 1-motor
I just can't figure it out.
Can someone please help?
Marsha
Code: Select all
var phidget22 = require('phidget22');
function runExample() {
var dcMotor0 = new phidget22.DCMotor();
dcMotor0.open(5000).then(function() {
dcMotor0.setTargetVelocity(1);
setTimeout(function () {
dcMotor0.close();
process.exit(0);
}, 5000);
});
dcMotor0.open(5000).then(function() {
dcMotor0.setTargetVelocity(-1);
setTimeout(function () {
dcMotor0.close();
process.exit(0);
}, 5000);
});
}
var conn = new phidget22.Connection(5661, 'localhost');
conn.connect().then(runExample);
Patrick
Lead Developer
Posts: 672 Joined: Mon Jun 20, 2005 8:46 am
Location: Calgary
Post
by Patrick » Thu Mar 10, 2022 10:47 am
I didn't test this code, but something like this would do it:
Code: Select all
var phidget22 = require('phidget22');
const conn = new phidget22.Connection(5661, 'localhost');
const dcMotor0 = new phidget22.DCMotor();
conn.connect().then(() => {
return dcMotor0.open(5000);
}).then(() => {
return dcMotor0.setTargetVelocity(1);
}).then(() => {
return new Promise(resolve => setTimeout(resolve, 5000));
}).then(() => {
return dcMotor0.setTargetVelocity(-1);
}).then(() => {
return new Promise(resolve => setTimeout(resolve, 5000));
}).then(() => {
dcMotor0.close();
process.exit(0);
});
-Patrick
MarshaJ847
Fresh meat
Posts: 2 Joined: Sun Feb 06, 2022 4:17 pm
Post
by MarshaJ847 » Sun Mar 13, 2022 4:29 pm
Patrick, Thank you, code worked perfectly.
Marsha