The GHI Electronics Motor Driver L298 Module provides a method for setting the speed of the two motors it can operate concurrently: MoveMotor(Gadgeteer.Modules.GHIElectronics.MotorControllerL298.Motor _motorSide, int _newSpeed). There is also a method that will ramp up the speed over a time interval: MoveMotorRampNB(Gadgeteer.Modules.GHIElectronics.MotorControllerL298.Motor _motorSide, int _newSpeed, int _rampingDelayMilli). These methods work pretty much as you would expect after you know that the units of the speed parameter represent a percentage of maximum speed.
Using these methods in code is fine, but you may want to include a potentiometer in a .NET Gadgeteer device, such as the GHI Electronics Potentiometer module, so that the user can manually change the speed of a motor. One way to get the user settings from the potentiometer is to include a .NET Gadgeteer.Timer object in the application code and set the speed of the motor accordingly in the Gadgeteer.Timer.TickEventHandler.
The following illustration shows the .NET Gadgeteer Designer view of the modules used in the potentiometer / timer scenario (click to enlarge).
The Program.cs file with the segment that sets the motor speed in the Timer.Tick event handler is shown in the following code block.
using Microsoft.SPOT; using GT = Gadgeteer; using Gadgeteer.Modules.GHIElectronics; namespace MotorDriver { public partial class Program { GT.Timer timer; void ProgramStarted() { button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed); timer = new GT.Timer(500); timer.Tick += new GT.Timer.TickEventHandler(timer_Tick); Debug.Print("Program Started"); } void timer_Tick(GT.Timer timer) { double percent = potentiometer.ReadPotentiometerPercentage(); motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor1, (int)(percent * 100)); } void button_ButtonPressed(Button sender, Button.ButtonState state) { if (!button.IsLedOn) { motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor1, 1); timer.Start(); button.TurnLEDOn(); } else { motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor1, 0); timer.Stop(); button.TurnLEDOff(); } } } }
If you want to set a motor to ramp up to a percentage of the maximum speed in an interval defined in milliseconds, use the following code in the Button.ButtonPressed event handler without the timer.
void button_ButtonPressed(Button sender, Button.ButtonState state) { if (!button.IsLedOn) { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 100, 5000); button.TurnLEDOn(); } else { motorControllerL298.MoveMotorRamp(MotorControllerL298.Motor.Motor1, 0, 5000); button.TurnLEDOff(); } }
See also: .NET Gadgeteer Motor Driver with Two Motors and Potentiometer
Note: Some users have reported that the MotorDriverL298 has mislabeled motor-connection terminals. After some initial perplexity, I had to connect my motor to M2- and M2+ to get the foregoing code to run. GHI is going to fix the driver code: http://www.tinyclr.com/forum/21/6413/