Page 1 of 1

1065 Motor control reversing

Posted: Tue Mar 01, 2022 6:17 pm
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);

Re: 1065 Motor control reversing

Posted: Thu Mar 10, 2022 10:47 am
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

Re: 1065 Motor control reversing

Posted: Sun Mar 13, 2022 4:29 pm
by MarshaJ847
Patrick, Thank you, code worked perfectly.

Marsha