Please read this instruction carefully, failure to follow below steps may result in motor damage or short circuit.
/* The code to control the movement of the slider*/
/* The version 1.2 starts the glider from the top**/
// Variables shared in both setup and loop
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // object for lcd control
const double Speed = 0.5; // The speed of the motor circle/second
const double Bottomlen = 47.7; //cm
const double Gliderlen = 41.8; //cm
const double Maxcircle = 5.5625;
const int Pulse = 6400; // pulse per circle
int lastchoice = 0;
int thischoice = 0;
double circle = 0;
double interval = 400; // interval waiting for user input ms
//setup for the locations of pins
byte pul = 32;
byte dir = 33;
byte loc = A4;
byte upin = 14;
byte downin = 17;
void setup() {
lcd.init();
lcd.backlight();
pinMode(pul,OUTPUT);
pinMode(dir,OUTPUT);
// The A4, 14, 17 by default, are the inputs
}
void loop() {
lastchoice = thischoice;
lcd.setCursor(0,0);
// display the current target location from the analog input
double upperlen = Gliderlen*analogRead(loc)/1024;
lcd.print("TrgLoc: -");
lcd.print(upperlen);
lcd.print("cm");
// judge the user movement
if (digitalRead(upin)>digitalRead(downin)){
thischoice = 1;
}
else if (digitalRead(upin)<digitalRead(downin)){
thischoice = -1;
}else{
thischoice = 0;
}
if ((lastchoice!=1)&&(thischoice==1)){
tone(pul,Pulse*Speed,1000*circle/Speed);
delay(1000*circle/Speed);
}
if ((lastchoice!=-1)&&(thischoice==-1)){
circle = Maxcircle*analogRead(loc)/1024;
digitalWrite(dir,HIGH);
tone(pul,Pulse*Speed,1000*circle/Speed);
delay(1000*circle/Speed);
digitalWrite(dir,LOW);
}
delay(interval);
}
The control pannel uses Arduino Mega which was produced in China, if your computer connot recognize the arduino, you can download the chinese version Mega usb driver at:
The default power of the microstep diver is 2A and the default ratate speed is 6400 pulse/circle.