Arduino Mega and Simple Motor Controller 18v7(Pololu)

Hi I am a secondary student that wants to learn how to control a motor (GA25Y-370, DC:6V, 33rpm) using an Arduino Mega that is connected to the simple motor controller.Below is the code that I used to control the speed of the motor, however I will need to change the program and upload the program into the arduino before the speed changes. I was wondering if there is any other way to enable me to change the speed through the serial monitor of arduino? I would also like to change the delay time highlighted below as I realise that is the way to control the position of the motor if I want the motor to run in positioning mode. Is there a way to control it using an analog potentiometer as well?

I am very sorry for the long post as I do not have much knowledge in this field and had only picked this up a month ago. I am interested in inventing my own robot but is in need of help for now. I would appreciate if anyone could give me a sample program on how to change the variables without having to change the number and reuploading it everytime. Thanks in advance

#include <SoftwareSerial.h> #define rxPin 3 // pin 3 connects to smcSerial TX (not used in this example) #define txPin 4 // pin 4 connects to smcSerial RX SoftwareSerial smcSerial = SoftwareSerial(rxPin, txPin); // required to allow motors to move // must be called when controller restarts and after any error void exitSafeStart() { smcSerial.write(0x83); } // speed should be a number from -3200 to 3200 void setMotorSpeed(int speed) { if (speed < 0) { smcSerial.write(0x86); // motor reverse command speed = -speed; // make speed positive } else { smcSerial.write(0x85); // motor forward command } smcSerial.write(speed & 0x1F); smcSerial.write(speed >> 5); } void setup() { // initialize software serial object with baud rate of 19.2 kbps smcSerial.begin(19200); // the Simple Motor Controller must be running for at least 1 ms // before we try to send serial data, so we delay here for 5 ms delay(5); // if the Simple Motor Controller has automatic baud detection // enabled, we first need to send it the byte 0xAA (170 in decimal) // so that it can learn the baud rate smcSerial.write(0xAA); // send baud-indicator byte // next we need to send the Exit Safe Start command, which // clears the safe-start violation and lets the motor run exitSafeStart(); // clear the safe-start violation and let the motor run } void loop() { setMotorSpeed(3200); // full-speed forward <font color="#FF0000">delay(1000)</font>; setMotorSpeed(-3200); // full-speed reverse <font color="#FF0000">delay(1000)</font>; }


If you want to have the be ability to change the speed of your motor on the fly, you can use a variable in your program and then call delay(variable). You could then read inputs from the serial monitor and change the value of that variable accordingly. If you want to use a potentiometer as an input, you could read the output of the potentiometer using your Arduino Mega and then command the Simple Motor Controller accordingly. You might find this tutorial on Arduino website helpful in understanding how to read the values from a potentiometer.

Alternatively, you can control the Simple Motor Controller directly using a potentiometer. You can read more about this method in the “Connecting a Potentiometer or Analog Joystick” section of the Simple Motor Controller user’s guide.

- Grant