.NET Gadgeteer Motor Driver with Two Motors and Potentiometer

An earlier post describes how to use a potentiometer with the GHI Electronics Motor Driver L298 module.  This example connects two motors to the motor controller, and the motors run simple mechanical parts, as shown in the following video. There are two buttons that start and stop the separate motors, and a potentiometer controls the speed of the motor that is running.  Both circuits are powered by 7.5 volt DC source connected to the motor controller.  See also: .NET Gadgeteer Bluetooth Control of Motor Driver.

The modules used in this scenario are shown in the following screen shot from the Visual Studio Designer (click to enlarge).

Motor Controller for Two Motors with Pot

Motor Controller for Two Motors with Pot

Application logic is contained in the following code block.

using Microsoft.SPOT;

using GT = Gadgeteer;
using Gadgeteer.Modules.GHIElectronics;

using System.Threading;

namespace MotorDriver
{

    public partial class Program
    {
        GT.Timer timer;

        void ProgramStarted()
        {
            button.ButtonPressed += new Button.ButtonEventHandler(button_ButtonPressed);
            button1.ButtonPressed += new Button.ButtonEventHandler(button1_ButtonPressed);
            timer = new GT.Timer(500);
            timer.Tick += new GT.Timer.TickEventHandler(timer_Tick);
            Debug.Print("Program Started");
        }

        void button1_ButtonPressed(Button sender, Button.ButtonState state)
        {
            if (!button1.IsLedOn)
            {
                motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor2, 1);
                timer.Start();
                button1.TurnLEDOn();
            }
            else
            {
                motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor2, 0);
                timer.Stop();
                button1.TurnLEDOff();
            }
        }

        void timer_Tick(GT.Timer timer)
        {
            double percent = potentiometer.ReadPotentiometerPercentage();

            if (button.IsLedOn)
            {
                motorControllerL298.MoveMotor(MotorControllerL298.Motor.Motor2, (int)(percent * 100));
            }
            else if (button1.IsLedOn)
            {
                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();
            }
        }
    }
}

!<<Some users will notice that the motor driver does not provide a way to reverse the direction of the motor. This will take some ingenuity to work around.>>

Marco Minerva looked at the source code and found that you can set the speed parameter to a negative value to reverse the direction of the motors.

, ,

  1. .NET Gadgeteer Motor Driver con due motori e un potenziometro (Italiano) « Integral Design

Leave a comment