I wanted to make a cheap motorized sider with centering function for my video projects. It works fine. I have already done some shootings with it.
Promo video:
#include <AccelStepper.h>
#include <SPI.h>
//#include <LM15SGFNZ07.h>
#include <SoftwareSerial.h>
#define EN 8
#define X_DIR 5 // forgató
#define X_STP 2
#define Z_DIR 7 //lineáris slider
#define Z_STP 4
#define status_LED 12 // a visszajelző led
#define system_voltage_level A3
//#define SpeedSPPin A4
//#define PosSPPin A5
//#define buttonPin 9 // A gomb a 9-es lábon
// Define a stepper and the pins it will use
AccelStepper stepper_X(AccelStepper::DRIVER, X_STP, X_DIR);
AccelStepper stepper_Z(AccelStepper::DRIVER, Z_STP, Z_DIR);
//LM15SGFNZ07 lcd(1,12,0); // [SDATA(11),SCLK(13),RS(1),RESET(12),CS(0)]
int dir = 1;
int blinkled = 1;
int blinkcnt = 0;
int onoff = 0; //0-motor off
int autman = 1; //0-man
int buttonState = 0;
//int SpeedSPValue = 0;
//int PosSPValue = 0;
int VoltValue = 0;
int speed_sp = 500;
int move_dev = 30;
int pos = 1500;
int XZ_ratio = 4; //default 2
char command; String string;
int command_type;
int hazaindult = 0;
SoftwareSerial mySerial(10, 11); // RX, TX
void setup()
{
mySerial.begin(9600);
Serial.begin(9600);
pinMode (EN, OUTPUT);
digitalWrite (EN, HIGH);
pinMode (status_LED, OUTPUT);
// pinMode(buttonPin, INPUT);
// digitalWrite (EN, LOW); motor fix engedélyezés
stepper_X.setAcceleration(500 * 4 * XZ_ratio /move_dev);
stepper_Z.setAcceleration(500);
delay(2000);
}
// Z- lineáris
// X- Forgató
void loop()
{
// Analog bemenetek (0 - 1023)
// 1:3,17 fesz osztó
VoltValue = analogRead(system_voltage_level);
if ( VoltValue > 700 ) { digitalWrite (status_LED, HIGH); } // 300
if ( VoltValue <= 700 )
{
if (blinkled == -1) {digitalWrite (status_LED, LOW); }
if (blinkled == 1) {digitalWrite (status_LED, HIGH); }
}
blinkcnt = blinkcnt + 1;
if (blinkcnt > 500) {blinkled = -blinkled; blinkcnt = 0;}
// normál működés
if (onoff==1 && stepper_Z.distanceToGo() == 0)
{
delay(500);
if (autman == 1) {
dir = -dir;
if (move_dev <= 98) { stepper_X.moveTo(pos * XZ_ratio /move_dev * dir * -1); } // ha a mov dev nagyobb mint 98 a forgato nem mozgat
stepper_Z.moveTo(pos* dir);
}
}
// motor engedélyezés ha auto, ha hazamegy, vagy manuális és futnia kell
if ( onoff == 1 || (onoff == 0 && stepper_Z.distanceToGo() != 0) )
{
//ideoda jár
if ( onoff == 1 ) { stepper_X.setMaxSpeed(speed_sp * XZ_ratio / move_dev);
stepper_Z.setMaxSpeed(speed_sp); }
// hazamegy
if ( onoff == 0 ) { stepper_X.setMaxSpeed(500 * XZ_ratio / move_dev);
stepper_Z.setMaxSpeed(500); }
digitalWrite (EN, LOW);
stepper_X.run();
stepper_Z.run();
serread_off();
}
// hazafutás
if (onoff==0 && stepper_Z.distanceToGo() != 0 && hazaindult == 0) {
stepper_X.moveTo(0);
stepper_Z.moveTo(0);
hazaindult = 1;
}
// home, motor lekapcsolt, vagy manuálisban a végén vagyunk
if ( ( onoff==0 && stepper_Z.distanceToGo() == 0) )
{
digitalWrite (EN, HIGH);
serread_full();
hazaindult = 0 ;
}
}
void serread_full()
{
string = "";
while(mySerial.available() > 0)
{
command = ((byte)mySerial.read());
if(command == ':') { break; }
else if(command == 'X') { command_type = 3;}
else if(command == 'L') { command_type = 2;}
else if(command == 'S') { command_type = 1;}
else { string += command; }
delay(1);
// Serial.println(string); //debug
}
if(string =="TON") { onoff = 1; }
if(string =="TOF") { onoff = 0; }
if(string =="TNX") { autman = 0; }
if(string =="TAU") { autman = 1; }
if (command_type == 1 && (string.toInt()>=1) && (string.toInt()<=255)) { speed_sp = (string.toInt()) * 13; } // szor 13
if (command_type == 2 && (string.toInt()>=1) && (string.toInt()<=255)) { pos = ((string.toInt()) * 40) + 300; }
if (command_type == 3 && (string.toInt()>=1) && (string.toInt()<=255)) { move_dev = (string.toInt())+2; }
}
void serread_off()
{
string = "";
while(mySerial.available() > 0)
{
Serial.println(string); //debug
command = ((byte)mySerial.read());
if(command == ':') { break; }
else { string += command; }
delay(1);
// Serial.println(string); //debug
}
if(string =="TOF") { onoff = 0; }
if(string =="TNX") { autman = 0; }
if(string =="TAU") { autman = 1; }
}
Comments