SMC 18v7 how to correct for my project

RE SMC 18v7, I need help to correct my code to work as I want.
I recently purchased one Simple Motor Controller 18v7 to apply to control DC motor to replace present stepping motor. I made about 6 months ago for use with marble machines “Marble mini Elevator”, as you will see on YouTube, its URL being,
MarbleminiElevator (uploaded, June 2017)


However, after having made it, I realized stepping motor’s shortcoming. When power gets cut off the bucket, while being on the way of upward position, comes down quickly without any brakes.

Therefore, I am trying to replace this stepping motor to ordinary DC motor.
Basic mechanism is simple: about 3 in acrylic cube loaded at the bottom with servo about 10-15 marbles,
gets lifted up to ~4 feet high, and tilted with upper servo to offload the marbles, and then comes down to load
next batch of marbles. I am planning to use limit switches to accurately locate the bucket to the bottom and top.

In order to use the DC motor for this project, I tested the Arduino’s Simple Example code, 6.7.1. in Users’ Guide, as a first trial. In this code I changed into three steps of motor speed: max (3200), 1/2(1600) and 1/3(1000) after the loop().
Before the motor started, my Servo motor code is placed to charge marbles and the motor starts to lift. When the bucket reaches at the top, second Servo at the top works to tilt the bucket to unload marbles, and the acrylic cube moves down to the bottom to next step.

With this code there are movements to be corrected, one important problem is the loading and unloading by servos while the motor is still moving. The motor does not stop when the
motor is changing the direction. (the movie seems to be too big to show here, too bad)

The tested sketch is placed here:

//  Test Pololu Simple sample (three motor speeds) plus 2 servos, Jan 30. 2018


#include <VarSpeedServo.h>

VarSpeedServo myservo1;  // lower servo for loading
VarSpeedServo myservo2;  // upper servo to press offloading gate

int position1;
int position2;

#define sv1pos1 30
#define sv1pos2 0
#define sv2pos1 40
#define sv2pos2 0

#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()
{
  myservo1.attach(10);
  myservo2.attach(11);
  

  position1 = 40;
  myservo1.write(sv1pos2, 30, true);
  position2 = 0;
  myservo2.write(sv2pos2, 30, true);
   
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);
  pinMode(11, OUTPUT);
  digitalWrite(11, HIGH);
  
  // 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()
{
   //Servo gate opens and closes to load marbles to bucket

  position1 = 0;
  myservo1.write(sv1pos1, 20, true); //sets servo position to 0 deg
  delay(700);
  position2 = 30;
  myservo1.write(sv1pos2, 20, true); //sets servo position to 30 deg
  delay(3000); 
  
  setMotorSpeed(3200);  // full-speed forward
  delay(7000);
  setMotorSpeed(1600);  // 1/2-speed forward
  delay(2000);
  setMotorSpeed(1000);  // 1/3-speed forward
  delay(3000);

// Upper bucket gate opens to unloading

  position1 = 0;
  myservo2.write(sv2pos1, 30, true); 
  // sets the servo position to 0 deg 
  delay(2000);
  position2 = 40;
  myservo2.write(sv2pos2, 30, true);

  delay(2000);
 
  setMotorSpeed(-3200);  // full-speed reverse
  delay(7000);
  setMotorSpeed(-1600);  // 1/2-speed reverse
  delay(2000);
  setMotorSpeed(-1000);  // 1/3-speed reverse
  delay(3000);
 
}

Hello, Nori.

How are your limit switches connected to your system? There are a couple of ways you might be able to stop the motor at the top of its motion.

The motor can be stopped by setting the motor speed to 0 with the code setMotorSpeed(0);. It looks like you are moving the motor upward for about 12 seconds right now (quickly at first, and then more slowly near the end). Instead of the delay(3000); line at the end of your motor movement, you could place a while loop in to monitor the status of the limit switch and to turn the motor off when it senses the limit switch is triggered.

It is possible to connect the limit switches to the SMC and to configure the SMC to stop the motor when the limit switches are triggered. There is more about this in the Input Settings section of the user’s guide. Instead of monitoring the limit switches in the Arduino code, you could just have the code pause for an appropriate period of time, then assume that the motor has reached the limit switch (and stopped) and have the code proceed with the rest of its routine.

-Nathan

Hello Nathan,

Thank you for your response. I am connecting two limit switches in this simple example and while the motor is in the slow portion, if I apply the limit switch it works to stop, with red LED on, but never comes back to turn on the motor.
I am a novice to modify the code and I seem to require more knowledge to even make any further change in the code.
Another way of applying the limit switch is to directly use two pins to Arduino and then at the place where the motor becomes slow movement, to try to apply the limit switch. This way hasn’t been done yet, may be coming Monday.
I found out there is very little of overrun when the limit switch is used at the slow movement of the motor. Under present situation, the motor and the servos are moving completely independently, they don’t listen to the code.

Nori

If the limit switches are connected properly and the SMC is configured correctly, that code looks like it could work. Which inputs on the SMC are the forward and reverse limit switches connected to? It appears from your code that the bucket moves up when the motor is going forward and down when moving in reverse. Can you confirm that? Can you save the settings file from the Pololu Simple Motor Control Center software (* File->Save Settings File* when running the software with your SMC attached to the computer) so we can verify the settings for your system? Could you run the SMC control Center Software when the red LED is on and look to see which errors appears in the “Status” tab?

-Nathan