const int stepPin = 2;
const int dirPin = 3;
const int increment = 4;
const int decrement = 5;
const int sincrement = 6;
const int sdecrement = 7;
const int led = 13;
const int pot = A0;
int mode = 1;
int x = 1, z = 0, speed = 0, level = 0, potv;
float y;
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool inc, dec, sinc, sdec, latch = LOW;
void setup() {
lcd.init();
lcd.backlight();
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(" Potentiometer ");
lcd.setCursor(0, 1);
lcd.print("Position Control ");
delay(3000); // Sets the two pins as Outputs
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(increment, INPUT_PULLUP);
pinMode(decrement, INPUT_PULLUP);
pinMode(sincrement, INPUT_PULLUP);
pinMode(sdecrement, INPUT_PULLUP);
for (int i = 0; i < 5; i++) {
digitalWrite(led, HIGH);
delay(500);
digitalWrite(led, LOW);
delay(500);
}
inc = digitalRead(increment);
dec = digitalRead(decrement);
while (inc == LOW) {
if (mode == 4) { mode = 0; }
mode = mode + 1;
if (mode == 1) {
lcd.setCursor(0, 0);
lcd.print("Mode-1 ");
}
if (mode == 2) {
lcd.setCursor(0, 0);
lcd.print("Mode-2 ");
}
if (mode == 3) {
lcd.setCursor(0, 0);
lcd.print("Mode-3 ");
}
delay(1000);
inc = digitalRead(increment);
}
}
void loop() {
//Mode-1 Free rotation right/left
if (mode == 1) {
inc = digitalRead(increment);
dec = digitalRead(decrement);
sinc = digitalRead(sincrement);
sdec = digitalRead(sdecrement);
if (inc == LOW && latch == LOW) {
delay(300);
speed = 1;
latch = HIGH;
inc = HIGH;
} //Fast forward
if (inc == LOW && latch == HIGH) {
delay(300);
speed = 0;
latch = LOW;
inc = HIGH;
} //Stop
if (dec == LOW && latch == LOW) {
delay(300);
speed = 2;
latch = HIGH;
dec = HIGH;
} //Fast reverse
if (dec == LOW && latch == HIGH) {
delay(300);
speed = 0;
latch = LOW;
dec = HIGH;
} //STOP
if (sinc == LOW && latch == LOW) {
delay(300);
speed = 3;
latch = HIGH;
sinc = HIGH;
} //Slow forward
if (sinc == LOW && latch == HIGH) {
delay(300);
speed = 0;
latch = LOW;
sinc = HIGH;
} //Stop
if (sdec == LOW && latch == LOW) {
delay(300);
speed = 4;
latch = HIGH;
sdec = HIGH;
} //Slow reverse
if (sdec == LOW && latch == HIGH) {
delay(300);
speed = 0;
latch = LOW;
sdec = HIGH;
} //STOP
}
while (speed == 1 && x < 2000) //Fast forward
{
lcd.setCursor(0, 0);
lcd.print("Mode-1: Fwd:Fast");
lcd.setCursor(0, 1);
lcd.print("Pos: ");
x++;
y = x / 200.0;
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1);
digitalWrite(stepPin, LOW);
delayMicroseconds(1);
lcd.setCursor(4, 1);
lcd.print(x);
lcd.setCursor(10, 1);
lcd.print(y);
if (x == 2000) {
speed = 0;
}
readswitch();
}
while (speed == 3 && x < 2000) //Slow forward
{
lcd.setCursor(0, 0);
lcd.print("Mode-1: Fwd:Slow");
lcd.setCursor(0, 1);
lcd.print("Pos: ");
x++;
y = x / 200.0;
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(10);
digitalWrite(stepPin, LOW);
delayMicroseconds(10);
lcd.setCursor(4, 1);
lcd.print(x);
lcd.setCursor(10, 1);
lcd.print(y);
if (x == 2000) {
speed = 0;
}
readswitch();
}
while (speed == 2 && x > 0) //Fast reverse
{
lcd.setCursor(0, 0);
lcd.print("Mode-1: Rev:Fast");
lcd.setCursor(0, 1);
lcd.print("Pos: ");
x--;
y = x / 200.0;
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1);
digitalWrite(stepPin, LOW);
delayMicroseconds(1);
lcd.setCursor(4, 1);
lcd.print(x);
lcd.setCursor(10, 1);
lcd.print(y);
if (x == 1) {
speed = 0;
}
readswitch();
}
while (speed == 4 && x > 0) //Slow reverse
{
lcd.setCursor(0, 0);
lcd.print("Mode-1: Rev:Slow");
lcd.setCursor(0, 1);
lcd.print("Pos: ");
x--;
y = x / 200.0;
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(10);
digitalWrite(stepPin, LOW);
delayMicroseconds(10);
lcd.setCursor(4, 1);
lcd.print(x);
lcd.setCursor(10, 1);
lcd.print(y);
if (x == 1) {
speed = 0;
}
readswitch();
}
//Mode-2 Predefined rotation right/left
if (mode == 2) {
inc = digitalRead(increment);
dec = digitalRead(decrement);
sinc = digitalRead(sincrement);
sdec = digitalRead(sdecrement);
if (inc == LOW && latch == LOW) {
delay(300);
speed = 5;
latch = HIGH;
inc = HIGH;
} //Fast forward
if (inc == LOW && latch == HIGH) {
delay(300);
speed = 0;
latch = LOW;
inc = HIGH;
} //Stop
if (dec == LOW && latch == LOW) {
delay(300);
speed = 6;
latch = HIGH;
dec = HIGH;
} //Fast reverse
if (dec == LOW && latch == HIGH) {
delay(300);
speed = 0;
latch = LOW;
dec = HIGH;
} //STOP
}
while (speed == 5 && x < z) //Preset forward
{
lcd.setCursor(0, 0);
lcd.print("Mode-2: Fwd: ");
lcd.setCursor(11, 0);
lcd.print(z);
lcd.setCursor(0, 1);
lcd.print("Pos: ");
x++;
y = x / 200.0;
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1);
digitalWrite(stepPin, LOW);
delayMicroseconds(1);
lcd.setCursor(4, 1);
lcd.print(x);
lcd.setCursor(10, 1);
lcd.print(y);
if (x == z) {
speed = 0;
}
readswitch1();
}
while (speed == 6 && x >0) //Preset reverse
{
lcd.setCursor(0, 0);
lcd.print("Mode-2: Rev: ");
lcd.setCursor(11, 0);
lcd.print(z);
lcd.setCursor(0, 1);
lcd.print("Pos: ");
x--;
y = x / 200.0;
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(10);
digitalWrite(stepPin, LOW);
delayMicroseconds(10);
lcd.setCursor(4, 1);
lcd.print(x);
lcd.setCursor(10, 1);
lcd.print(y);
if (x == 1) {
speed = 0;
}
readswitch1();
}
while (speed == 2 && x > 0) //Fast reverse
{
lcd.setCursor(0, 0);
lcd.print("Mode-2: Rev: ");
lcd.setCursor(11, 0);
lcd.print(z);
lcd.setCursor(0, 1);
lcd.print("Pos: ");
x--;
y = x / 200.0;
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(1);
digitalWrite(stepPin, LOW);
delayMicroseconds(1);
lcd.setCursor(4, 1);
lcd.print(x);
lcd.setCursor(10, 1);
lcd.print(y);
if (x == 1) {
speed = 0;
}
readswitch();
}
while (speed == 4 && x > 0) //Slow reverse
{
lcd.setCursor(0, 0);
lcd.print("Mode-2: Rev: ");
lcd.setCursor(11, 0);
lcd.print(z);
lcd.setCursor(0, 1);
lcd.print("Pos: ");
x--;
y = x / 200.0;
digitalWrite(dirPin, LOW);
digitalWrite(stepPin, HIGH);
delayMicroseconds(10);
digitalWrite(stepPin, LOW);
delayMicroseconds(10);
lcd.setCursor(4, 1);
lcd.print(x);
lcd.setCursor(10, 1);
lcd.print(y);
if (x == 1) {
speed = 0;
}
readswitch();
}
// Mode 3 - Potentiometer setting
if (mode == 3) {
potv = analogRead(pot);
potv = map(potv, 0, 1023, 0, 2000);
lcd.setCursor(0, 0);
lcd.print(" Mode-3: Pot Set ");
lcd.setCursor(0, 1);
lcd.print("Turns: ");
lcd.setCursor(6, 1);
lcd.print(potv);
sinc = digitalRead(sincrement);
if (sinc == LOW) {
delay(300);
mode = 2;
z = potv;
lcd.setCursor(0, 0);
lcd.print(" Mode-2: Pot ");
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(11, 0);
lcd.print(potv);
}else{ delay(500);}
}
}
void readswitch() {
inc = digitalRead(increment);
dec = digitalRead(decrement);
sinc = digitalRead(sincrement);
sdec = digitalRead(sdecrement);
if (inc == LOW && latch == LOW) {
delay(300);
speed = 1;
latch = HIGH;
inc = HIGH;
} //Fast forward
if (inc == LOW && latch == HIGH) {
delay(300);
speed = 0;
latch = LOW;
inc = HIGH;
} //Stop
if (dec == LOW && latch == LOW) {
delay(300);
speed = 2;
latch = HIGH;
dec = HIGH;
} //Fast reverse
if (dec == LOW && latch == HIGH) {
delay(300);
speed = 0;
latch = LOW;
dec = HIGH;
} //STOP
if (sinc == LOW && latch == LOW) {
delay(300);
speed = 3;
latch = HIGH;
sinc = HIGH;
} //Slow forward
if (sinc == LOW && latch == HIGH) {
delay(300);
speed = 0;
latch = LOW;
sinc = HIGH;
} //Stop
if (sdec == LOW && latch == LOW) {
delay(300);
speed = 4;
latch = HIGH;
sdec = HIGH;
} //Slow reverse
if (sdec == LOW && latch == HIGH) {
delay(300);
speed = 0;
latch = LOW;
sdec = HIGH;
} //STOP
}
void readswitch1() {
inc = digitalRead(increment);
dec = digitalRead(decrement);
if (inc == LOW && latch == LOW) {
delay(300);
speed = 5;
latch = HIGH;
inc = HIGH;
} //Fast forward
if (inc == LOW && latch == HIGH) {
delay(300);
speed = 0;
latch = LOW;
inc = HIGH;
} //Stop
if (dec == LOW && latch == LOW) {
delay(300);
speed = 6;
latch = HIGH;
dec = HIGH;
} //Fast reverse
if (dec == LOW && latch == HIGH) {
delay(300);
speed = 0;
latch = LOW;
dec = HIGH;
} //STOP
}