Hello,
Could you please help me in my camera slider project. I would like to build a very simple slider with the camera moving back and forth on the rail. In the ends of the rail there are direction switching buttons and the speed of the dolly is controlled by a mouse scroll wheel. I would like to stop the dolly with the push button of the scroll wheel but this is not yet on the schema.
This is how the schema looks like so far:
and this is my arduino code :
//simple A4988 connection
//jumper reset and sleep together
//connect VDD to Arduino 3.3v or 5v
//connect GND to Arduino GND (GND near VDD)
//connect 1A and 1B to stepper coil 1
//connect 2A and 2B to stepper coil 2
//connect VMOT to power source (9v battery + term)
//connect GRD to power source (9v battery - term)
#define encoder0PinA 2
#define encoder0PinB 3
#define sensorPin 12
#define dirPin 11
#define stepPin 10
#define stepsInFullRound 400
boolean stepDirection = 0; //rotation direction
double speedRPS = 7; //initial speed of stepper
volatile unsigned int encoder0Pos = 0;
void setup() {
pinMode(sensorPin, INPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(encoder0PinA, INPUT);
pinMode(encoder0PinB, INPUT);
digitalWrite(stepPin, LOW);
digitalWrite(dirPin, LOW);
attachInterrupt(0, doEncoderA, CHANGE); // encoder pin on interrupt 0 (pin 2)
attachInterrupt(1, doEncoderB, CHANGE); // encoder pin on interrupt 1 (pin 3)
}
boolean isSensing() {
return digitalRead(sensorPin);
}
// A custom delay function used in the run()-method
void holdHalfCylce() {
long holdTime_us = (long)(1.0 / (double) stepsInFullRound / speedRPS / 2.0 * 1E6);
int overflowCount = holdTime_us / 65535;
for (int i = 0; i < overflowCount; i++) {
delayMicroseconds(65535);
}
delayMicroseconds((unsigned int) holdTime_us);
}
void moveSteps(int stepCount) {
digitalWrite(dirPin, stepDirection);
for (int i = 0; i < stepCount; i++) {
digitalWrite(stepPin, HIGH);
holdHalfCylce();
digitalWrite(stepPin, LOW);
holdHalfCylce();
}
}
void moveUntilSense() {
digitalWrite(dirPin, stepDirection);
while(!isSensing()) {
digitalWrite(stepPin, HIGH);
holdHalfCylce();
digitalWrite(stepPin, LOW);
holdHalfCylce();
}
}
void loop()
{
moveUntilSense();
delay(1000);
stepDirection = !stepDirection;
moveSteps(20);
}
void doEncoderA(){
if (digitalRead(encoder0PinA) == HIGH) {
if (digitalRead(encoder0PinB) == LOW) {
// encoder0Pos = encoder0Pos + 1; // CW
speedRPS = speedRPS + 1;
}
else {
// encoder0Pos = encoder0Pos - 1; // CCW
speedRPS = speedRPS - 1;
if ( speedRPS = 0) {
speedRPS = 1;
}
}
}
else
{
if (digitalRead(encoder0PinB) == HIGH) {
// encoder0Pos = encoder0Pos + 1; // CW
speedRPS = speedRPS + 1;
}
else {
// encoder0Pos = encoder0Pos - 1; // CCW
speedRPS = speedRPS - 1;
if (speedRPS = 0) {
speedRPS = 1;
}
}
}
}
void doEncoderB(){
if (digitalRead(encoder0PinB) == HIGH) {
if (digitalRead(encoder0PinA) == HIGH) {
// encoder0Pos = encoder0Pos + 1; // CW
speedRPS = speedRPS + 1;
}
else {
// encoder0Pos = encoder0Pos - 1; // CCW
speedRPS = speedRPS - 1;
if (speedRPS = 0) {
speedRPS = 1;
}
}
}
else {
if (digitalRead(encoder0PinA) == LOW) {
// encoder0Pos = encoder0Pos + 1; // CW
speedRPS = speedRPS + 1;
}
else {
// encoder0Pos = encoder0Pos - 1; // CCW
speedRPS = speedRPS - 1;
if (speedRPS = 0) {
speedRPS = 1;
}
}
}
}
I put together with a friend of mine this program, but unfortunately only one of the end buttons are working and the stepper motor (TYPE 103-546-8441 2,4Ohm 1.6/step SANYO DENKI) is only working if I do not turn the digital encoder otherwise is stops or starts to shake. I appreciate any help.
slider.pdf (353 KB)
code_viorel.rtf (3.76 KB)