I am using the VarSpeedServo library with the following code but I can't get a smooth bounce as shown in the video. The servo is controlled by a Tx with pot controllers so when I turn the pot one way it goes through a movement sequence and when I turn it the other it performs the other set of sequences:
void SignalsChange(boolean newState) {
// Move the signal servo to the new position
SignalServo.write((newState ? 110 : 90), 25, true);
SignalServo.write((newState ? 90 : 110), 20, true);
SignalServo.write((newState ? 100 : 100), 15, true);
SignalServo.write((newState ? 95 : 95), 10, true);
SignalServo.write((newState ? 110 : 90), 10, true);
Is there another library out there better suited to what I am trying to achieve?
The bouncing of the real arm is caused by loose joints and inertia. Does your model have loose joints? Does your arm have any mass?
I didn't think posting my full code would be relevant to my question in this instance but here it is anyway:
// Libraries
#include <VarSpeedServo.h>
// Variables
VarSpeedServo SignalServo;
VarSpeedServo TurnoutServo;
VarSpeedServo UncouplerServo;
// Receiver channels
const byte SignalsInputPin = 22;
const byte TurnoutsInputPin = 26;
const byte UncouplerInputPin = 24;
const byte CHANNELS = 3; // Number of channels
const byte ChannelPins[CHANNELS] = {TurnoutsInputPin, SignalsInputPin, UncouplerInputPin};
const byte SignalsServoPin = 50;
const byte TurnoutsServoPin = 52;
const byte UncouplerServoPin = 48;
boolean ChannelStatePrevious[CHANNELS];
unsigned long ChannelChangeTime[CHANNELS];
const int CHANNEL_MAX = 1600; //control knob value max (microseconds)
const int CHANNEL_MIN = 1100; //control knob value min (microseconds)
const int CHANNEL_THRESHOLD = ((CHANNEL_MIN + CHANNEL_MAX) / 2); // Center point
const unsigned long CHANNEL_DEBOUNCE = 5;
void setup() {
for (int i = 0; i < CHANNELS; i++) {
pinMode (ChannelPins[i], INPUT); //set up input pins
}
SignalServo.attach(SignalsServoPin); //attach signal servo
UncouplerServo.attach(UncouplerServoPin); //attach uncoupler servo
TurnoutServo.attach(TurnoutsServoPin); //attach turnout servo
}
void loop () {
// Convert the channel inputs to switch state changes
for (int i = 0; i < CHANNELS; i++) {
boolean channelState = pulseIn(ChannelPins[i], HIGH) > CHANNEL_THRESHOLD;
if (channelState != ChannelStatePrevious[i] && millis() - ChannelChangeTime[i] >= CHANNEL_DEBOUNCE) {
ChannelStatePrevious[i] = channelState;
ChannelChangeTime[i] = millis();
// The channel has changed state. Do something!
switch (i) {
case 0:
SignalsChange(channelState);
break;
case 1:
UncouplerChange(channelState);
break;
case 2:
TurnoutsChange(channelState);
break;
}
}
}
//Servo animations
}
void SignalsChange(boolean newState) {
// Move the signal servo to the new position
SignalServo.write((newState ? 110 : 90), 25, true); //first value signal halt movement, second value clear movement
SignalServo.write((newState ? 90 : 110), 20, true);
SignalServo.write((newState ? 100 : 100), 15, true);
SignalServo.write((newState ? 95 : 95), 10, true);
SignalServo.write((newState ? 110 : 90), 10, true);
}
void UncouplerChange(boolean newState) {
// Move the uncoupler servo to the new position
UncouplerServo.write((newState ? 110 : 120), 10, true);
}
void TurnoutsChange(boolean newState) {
// Move the turnout servo to the new position
TurnoutServo.write((newState ? 56 : 118), 6, true);
}
My model signal is made from Plastikard so very light unfortunately.
Here is an example of someone using a Picaxe microcontroller with similar sized model signals so I wondered if the same could be achieved with an Arduino?
I don't think there is a ready made library for it. Thing is, you want the speed to ramp down before it hits the end. Now the speed is just linear the whole way after which you flip the direction and move the other way with a constant speed. But you want to ramp it up and down. You can do that yourself but I think you need a bit more resolution then the single degree accuracy. This can be achieved with writeMicroseconds().
Btw, the blocking mode (aka wait true) is a bi of a problem if you want to do other stuff in the mean time.