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);
}