-
Notifications
You must be signed in to change notification settings - Fork 9
Pin Controllers
PiFaceSharp contains a couple of pin controllers that run on a separate thread and will automatically manage the state of output pins for you in the background. For example, the BlinkingPinController will automatically switch the state of a pin between HIGH and LOW on a timed interval. This is useful if you have an LED attached to a pin and want it to blink on and off, but you don't want to handle the timing yourself in your main program loop.
There's also a PwmPinController which which can be used to vary the speed of DC motors, or even as a dimmer switch for a simple LED. Simply set the power level and the PwmPinController will handle the high-speed switching to turn the pin HIGH and LOW as required. (See https://en.wikipedia.org/wiki/Pulse-width_modulation for more about PWM).
// get reference to default piface device
var piface = new PiFace();
// create a new background-thread pin controller that turns pin 3 on and off every 500 milliseconds
var controller = new BlinkingPinController(piface, 3, 500);
// start the controller. it will run in the background regardless
// of what the main program thread is doing
controller.Start();
// do your main program logic here while the LED blinks away
System.Threading.Thread.Sleep(10000);
// stop the controller
controller.Stop();
// get reference to default piface device
var piface = new PiFace();
// create a new background-thread PWM controller on pin 5 which has a 50 millisecond
// cycle period
var controller = new PwmPinController(piface, 5, 50);
// start the controller. it will drive the motor in the background regardless of what
// the main program thread is doing
controller.Duty = 0.50; // run at 50% power
controller.Start();
// do something else here (e.g. wait for user input) while the Pwm controller
System.Threading.Thread.Sleep(10000);
// now run at 25% power
controller.Duty = 0.25;
System.Threading.Thread.Sleep(10000);
// stop the controller
controller.Stop();