// Define stepper motor connections and steps per revolution:
#define enPin 7
#define dirPin 8
#define stepPin 9
#define limPin 6
#define LED 13
//#define stepsPerRevolution 800
//#define variable Stepper Motor Speed
#define speedPin1 2
#define speedPin2 3
#define speedPin3 4
char input; //Belum tau ini untuk apa
String inputSTR; //serial input
int buttonState = 0; //Ini untuk awalan
int TotStep = 100000; //Step tak terhingga untuk mencari Total Step yang dibtuhkan
int inStep = 0; //Ini initial Step
int finStep = 7000; //Ini final Step - 8
//int finStep = 3500; //Ini final Step - 4
//int finStep = 1750; //Ini final Step - 2/A
//int finStep = 875; //Ini final Step - 1
int finRep = 10; // Ini final pengulangan
int tdelay =550; //Ini manipulasi Step
//int tdelay = 1000; //Ini manipulasi Step
int tdelaystop = 4000; //Ini manipulasi Step
int vol = 100; //Ini manipulasi Step
int adjStep = 0; //Ini adjustment Step
//nt tgtStep = 1000; //Ini target Step
//int adjStep = 1; //Ini target Step
double percentStep = 0.2; //Ini target Step
double tgtStep = 3; //Ini target Step
//Note:
//1000 ml = F;
//900 ml = G - 0.9;
//800 ml = H - 0.8;
//700 ml = I - 0.7;
//600 ml = J - 0.6;
//500 ml = K - 0.5;
//400 ml = L - 0.4;
//300 ml = M - 0.3;
//200 ml = N - 0.2;
//100 ml = O - 0.1;
//Fungsi motor untuk menggerakkan awal / Adjustment sesuai dengan volume
void motorAdj()
{
// Set the spinning direction STOP "S":
digitalWrite(enPin, LOW);
digitalWrite(dirPin, HIGH);
// Spin the stepper motor:
for (int i = 0; i < adjStep; i++)
{
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
//delay(tdelay);
delayMicroseconds(tdelay);
digitalWrite(stepPin, LOW);
//delay(tdelay);
delayMicroseconds(tdelay);
}
// Set the spinning direction STOP "S":
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, HIGH);
delay(tdelaystop);
}
// Fungsi motor untuk menggerakkan sesuai dengan step pengulangan
void motorTgt()
{
// Set the repeatition of the pumping
for (int j = 0; j < finRep; j++)
{
if(input=='S')
{
// Set the spinning direction STOP "S":
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, HIGH);
delay(tdelaystop);
}
else
{
// Spin the stepper motor spinning direction FORWARD "F"/clockwise::
digitalWrite(enPin, LOW);
digitalWrite(dirPin, HIGH);
// Spin the stepper motor:
for (int i = 0; i < tgtStep; i++)
{
// These four lines result in 1 step - FORWARD:
digitalWrite(stepPin, HIGH);
// delay(tdelay);
delayMicroseconds(tdelay);
digitalWrite(stepPin, LOW);
// delay(tdelay);
delayMicroseconds(tdelay);
}
// Set the spinning direction STOP "S":
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, HIGH);
delay(tdelaystop);
// Set the spinning direction BACKWARD "B"/C_clockwise:
digitalWrite(enPin, LOW);
digitalWrite(dirPin, LOW);
// Spin the stepper motor:
for (int i = 0; i <tgtStep; i++)
{
// These four lines result in 1 step - BACKWARD:
digitalWrite(stepPin, HIGH);
//delay(tdelay);
delayMicroseconds(tdelay);
digitalWrite(stepPin, LOW);
delayMicroseconds(tdelay);
//delay(tdelay);
}
// Set the spinning direction STOP "S":
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, HIGH);
delay(tdelaystop);
}
}
}
//Fungsi motor untuk kembali ke awal / Adjustment sesuai dengan volume
void motorRtn()
{
// Set the spinning direction STOP "S":
digitalWrite(enPin, LOW);
digitalWrite(dirPin, LOW);
// Spin the stepper motor:
for (int i = 0; i < adjStep; i++)
{
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
//delay(tdelay);
delayMicroseconds(tdelay);
digitalWrite(stepPin, LOW);
//delay(tdelay);
delayMicroseconds(tdelay);
}
// Set the spinning direction STOP "S":
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, HIGH);
delay(tdelaystop);
}
// prosedur set kecepatan Stepper motor
void StepperSpeed(char pin)
{
// Serial.println("case");
// Serial.println(pin);
switch (pin) {
case '0':
digitalWrite(speedPin1, 0);
digitalWrite(speedPin2, 0);
digitalWrite(speedPin3, 1);
//Serial.println("nol");
break;
case '1':
digitalWrite(speedPin1, 0);
digitalWrite(speedPin2, 0);
digitalWrite(speedPin3, 1);
// Serial.println("satu");
break;
case '2':
digitalWrite(speedPin1, 0);
digitalWrite(speedPin2, 1);
digitalWrite(speedPin3, 0);
break;
case '4':
digitalWrite(speedPin1, 1);
digitalWrite(speedPin2, 0);
digitalWrite(speedPin3, 0);
break;
}
}
void setup()
{
Serial.begin(115200);
Serial.println("Serial Com 1 Format pilihan spasi kecepatan");
Serial.println(" contoh F 1 >> pilihan F 1000 ml kecepatan 1" );
// Declare pins as output:
pinMode(enPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(limPin, INPUT_PULLUP);
pinMode(LED, OUTPUT);
digitalWrite(enPin, HIGH);
//delay(3000);
pinMode(speedPin1, OUTPUT);
pinMode(speedPin2, OUTPUT);
pinMode(speedPin3, OUTPUT);
//set first tepper motor speed
digitalWrite(speedPin1, 0);
digitalWrite(speedPin2, 0);
digitalWrite(speedPin3, 1);
}
void loop()
{
digitalWrite(enPin, HIGH);
while (Serial.available())
{
if (Serial.available()>0)
{
//input=(byte)Serial.read();
inputSTR=Serial.readStringUntil('\n');
if(inputSTR.charAt(0)=='F')
{
//Serial.println(inputSTR.length());
//Serial.println(inputSTR.charAt(0));
//Serial.println(inputSTR.charAt(2));
StepperSpeed(inputSTR.charAt(2));
percentStep = 0; //
adjStep = finStep * percentStep; //
tgtStep = finStep - adjStep; //
motorTgt();
}
else if(inputSTR.charAt(0)=='C')
{
StepperSpeed(inputSTR.charAt(2));
percentStep = 0; //
adjStep = 100*1; //
tgtStep = finStep; //
motorAdj();
}
else if(inputSTR.charAt(0)=='G')
{
StepperSpeed(inputSTR.charAt(2));
percentStep = 0.9; //
adjStep = finStep * percentStep; //
tgtStep = adjStep; //
//motorAdj();
motorTgt();
//motorRtn();
}
else if(inputSTR.charAt(0)=='H')
{
StepperSpeed(inputSTR.charAt(2)) ;
percentStep = 0.8; //
adjStep = finStep * percentStep; //
tgtStep = adjStep; //
//motorAdj();
motorTgt();
//motorRtn();
}
else if(inputSTR.charAt(0)=='I')
{
StepperSpeed(inputSTR.charAt(2)) ;
percentStep = 0.7; //
adjStep = finStep * percentStep; //
tgtStep = adjStep; //
//motorAdj();
motorTgt();
//motorRtn();
}
else if(inputSTR.charAt(0)=='J')
{
StepperSpeed(inputSTR.charAt(2));
percentStep = 0.6; //
adjStep = finStep * percentStep; //
tgtStep = adjStep; //
//motorAdj();
motorTgt();
//motorRtn();
}
else if(inputSTR.charAt(0)=='K')
{
StepperSpeed(inputSTR.charAt(2));
percentStep = 0.5; //
adjStep = finStep * percentStep; //
tgtStep = adjStep; //
//motorAdj();
motorTgt();
//motorRtn();
}
else if(inputSTR.charAt(0)=='L')
{
StepperSpeed(inputSTR.charAt(2)) ;
percentStep = 0.4; //
adjStep = finStep * percentStep; //
tgtStep = adjStep; //
//motorAdj();
motorTgt();
//motorRtn();
}
else if(inputSTR.charAt(0)=='M')
{
StepperSpeed(inputSTR.charAt(2));
percentStep = 0.3; //
adjStep = finStep * percentStep; //
tgtStep = adjStep; //
//motorAdj();
motorTgt();
//motorRtn();
}
else if(inputSTR.charAt(0)=='N')
{
StepperSpeed(inputSTR.charAt(2));
percentStep = 0.2; //
adjStep = finStep * percentStep; //
tgtStep = adjStep; //
//motorAdj();
motorTgt();
//motorRtn();
}
else if(inputSTR.charAt(0)=='O')
{
StepperSpeed(inputSTR.charAt(2));
percentStep = 0.1; //
adjStep = finStep * percentStep; //
tgtStep = adjStep; //
//motorAdj();
motorTgt();
//motorRtn();
}
else if(inputSTR.charAt(0)=='S')
{
// Set the spinning direction STOP "S":
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, HIGH);
// Spin the stepper motor:
for (int i = 0; i < finStep; i++)
{
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delay(tdelay);
digitalWrite(stepPin, LOW);
delay(tdelay);
}
}
else if(inputSTR.charAt(0)=='R')
{
// Set the spinning direction RETURN "R" (to calculate the total step):
digitalWrite(enPin, LOW);
digitalWrite(dirPin, LOW);
// Spin the stepper motor:
for (int i = 0; i < TotStep; i++)
{
if(inputSTR.charAt(0)=='S')//cek lagi
{
// Set the spinning direction STOP "S":
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, HIGH);
delay(tdelaystop);
}
else
{
// Jika Limit SWITCH aktif
//buttonState = digitalRead(limPin);
//if (buttonState == HIGH)
//{
//Serial.println(i);
// Set the spinning direction STOP "S":
//digitalWrite(enPin, HIGH);
//digitalWrite(dirPin, HIGH);
//break;
// }
// These four lines result in 1 step:
digitalWrite(stepPin, HIGH);
delay(tdelay);
digitalWrite(stepPin, LOW);
delay(tdelay);
}
}
}
else if(inputSTR.charAt(0)=='X')
{
// Set the spinning direction RETURN "R" (to calculate the total step):
digitalWrite(enPin, LOW);
digitalWrite(dirPin, HIGH);
// Spin the stepper motor:
for (int i = 0; i < TotStep; i++)
{
if(inputSTR.charAt(0)=='S')//cek lagi
{
// Set the spinning direction STOP "S":
digitalWrite(enPin, HIGH);
digitalWrite(dirPin, HIGH);
delay(tdelaystop);
}
else
{
digitalWrite(stepPin, HIGH);
delay(tdelay);
digitalWrite(stepPin, LOW);
delay(tdelay);
Serial.println(i);
}
}
}
}
}
}Kecepatan stepper dip switch