1065 Motor control reversing

Supporting Browser-based Javascript and Node.js
Post Reply
MarshaJ847
Fresh meat
Posts: 2
Joined: Sun Feb 06, 2022 4:17 pm
Contact:

1065 Motor control reversing

Post by MarshaJ847 »

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);
User avatar
Patrick
Lead Developer
Posts: 3403
Joined: Mon Jun 20, 2005 8:46 am
Location: Canada
Contact:

Re: 1065 Motor control reversing

Post by Patrick »

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
Contact:

Re: 1065 Motor control reversing

Post by MarshaJ847 »

Patrick, Thank you, code worked perfectly.

Marsha
Post Reply

Who is online

Users browsing this forum: No registered users and 10 guests