You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
113 lines
2.4 KiB
113 lines
2.4 KiB
|
|
#define dirPin 2
|
|
#define stepPin 3
|
|
#define endPin 4
|
|
|
|
int stepSpeedUp = 1500; // Motor speed - UP - min 500
|
|
int stepSpeedDown = 1500; // Motor speed - DOWN - min 500
|
|
long maxStep = 54000; // Max steps
|
|
|
|
int stepSpeed = 0;
|
|
bool dir = true;
|
|
long stepCount = 0;
|
|
bool rodEnd = false;
|
|
|
|
void setup() {
|
|
Serial.begin(9600);
|
|
pinMode(stepPin, OUTPUT);
|
|
pinMode(dirPin, OUTPUT);
|
|
pinMode(endPin, INPUT);
|
|
digitalWrite(dirPin, dir);
|
|
|
|
while(!rodEnd) {
|
|
Serial.print(stepSpeed);
|
|
Serial.print(" - ");
|
|
Serial.print(stepCount);
|
|
Serial.println("↓");
|
|
|
|
stepCount++;
|
|
stepSpeed = stepCount < 500 ? ((stepSpeedDown / 500) * stepCount) : stepSpeedDown;
|
|
|
|
digitalWrite(stepPin, HIGH);
|
|
delayMicroseconds(stepSpeed);
|
|
digitalWrite(stepPin, LOW);
|
|
delayMicroseconds(stepSpeed);
|
|
|
|
if(digitalRead(endPin)) {
|
|
rodEnd = true;
|
|
stepCount = 0;
|
|
}
|
|
}
|
|
|
|
Serial.println("START");
|
|
|
|
}
|
|
|
|
void loop() {
|
|
|
|
Serial.print(stepSpeed);
|
|
Serial.print(" - ");
|
|
Serial.print(stepCount);
|
|
Serial.print(" ");
|
|
|
|
if(!dir) {
|
|
stepCount++;
|
|
|
|
if(stepCount >= maxStep) {
|
|
dir = !dir;
|
|
digitalWrite(dirPin, dir);
|
|
delay(3000);
|
|
} else {
|
|
|
|
if(stepCount < (maxStep - 500)) {
|
|
stepSpeed = stepCount < 500 ? ((stepSpeedUp / 500) * stepCount) : stepSpeedUp;
|
|
} else {
|
|
stepSpeed = (stepSpeedUp / 500) * (maxStep - stepCount);
|
|
}
|
|
|
|
digitalWrite(stepPin, HIGH);
|
|
delayMicroseconds(stepSpeed);
|
|
digitalWrite(stepPin, LOW);
|
|
delayMicroseconds(stepSpeed);
|
|
Serial.println("↑");
|
|
}
|
|
|
|
} else {
|
|
|
|
stepCount--;
|
|
|
|
if(stepCount <= 0) {
|
|
dir = !dir;
|
|
digitalWrite(dirPin, dir);
|
|
delay(3000);
|
|
} else {
|
|
|
|
if(stepCount > 500) {
|
|
stepSpeed = stepCount > (maxStep - 500) ? ((stepSpeedDown / 500) * (maxStep - stepCount)) : stepSpeedDown;
|
|
} else {
|
|
stepSpeed = (stepSpeedDown / 500) * stepCount;
|
|
}
|
|
|
|
digitalWrite(stepPin, HIGH);
|
|
delayMicroseconds(stepSpeed);
|
|
digitalWrite(stepPin, LOW);
|
|
delayMicroseconds(stepSpeed);
|
|
Serial.println("↓");
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
void serialEvent() {
|
|
while (Serial.available()) {
|
|
char inChar = (char)Serial.read();
|
|
switch(inChar) {
|
|
case 's':
|
|
maxStep = stepCount;
|
|
Serial.print("###################################");
|
|
Serial.println(stepCount);
|
|
}
|
|
}
|
|
}
|