#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
const int forwardButton = 6;
const int reverseButton = 5;
const int stepAngleButton = 4;
const int potPin = A0;
int stepdelay;
int previousVal = 0; // Variable to store the previous potentiometer reading
int direction = 2;
int pulse = 3;
int forwardStatePrev = HIGH;
int reverseStatePrev = HIGH;
int stepStatePrev = HIGH;
void setup() {
pinMode(forwardButton, INPUT_PULLUP);
pinMode(reverseButton, INPUT_PULLUP);
pinMode(stepAngleButton, INPUT_PULLUP);
pinMode(direction, OUTPUT);
pinMode(pulse, OUTPUT);
digitalWrite(direction, LOW);
digitalWrite(pulse, LOW);
Serial.begin(9600); // Initialize serial communication
tft.begin();
tft.setRotation(3); // Adjust rotation if needed
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(10, 10);
tft.println("Motor Control");
tft.setCursor(10, 40);
tft.println("Forward");
tft.setCursor(10, 70);
tft.println("Reverse");
tft.setCursor(10, 100);
tft.println("Step Angle");
}
void loop() {
int forwardState = debounce(forwardButton, &forwardStatePrev);
int reverseState = debounce(reverseButton, &reverseStatePrev);
int stepState = debounce(stepAngleButton, &stepStatePrev);
if (forwardState == LOW) {
digitalWrite(direction, LOW);
stepMotor();
updateScreen("Forward");
}
if (reverseState == LOW) {
digitalWrite(direction, HIGH);
stepMotor();
updateScreen("Reverse");
}
if (stepState == LOW) {
input();
updateScreen("Step Angle");
}
}
int debounce(int pin, int* prevState) {
int currentState = digitalRead(pin);
if (currentState != *prevState) {
delay(5);
currentState = digitalRead(pin);
}
*prevState = currentState;
return currentState;
}
void stepMotor() {
int val = analogRead(potPin);
stepdelay = map(val, 0, 1023, 1000, 5000);
if (val != previousVal) {
float rpm = 150000.0 / stepdelay;
Serial.print("RPM = ");
Serial.println(rpm);
previousVal = val;
}
digitalWrite(pulse, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(pulse, LOW);
delayMicroseconds(stepdelay);
}
void input() {
Serial.println("Enter the angle in degrees");
while (Serial.available() == 0) {}
float angle = Serial.parseFloat();
while (Serial.available() > 0) {
Serial.read();
}
Serial.println("enter the time in seconds");
while (Serial.available() == 0) {}
float time = Serial.parseFloat();
float steps = abs(angle) * (200.0 / 360);
if (angle >= 0) {
digitalWrite(direction, HIGH);
} else {
digitalWrite(direction, LOW);
}
int stepdelay = (time / steps) * 0.5 * 1000000;
float rpm = 150000.0 / stepdelay;
Serial.print("RPM = ");
Serial.print(rpm);
for (int u = 0; u < steps; u++) {
digitalWrite(pulse, HIGH);
delayMicroseconds(stepdelay);
digitalWrite(pulse, LOW);
delayMicroseconds(stepdelay);
}
while (Serial.available() > 0) {
Serial.read();
}
}
void updateScreen(String message) {
tft.setCursor(10, 120);
tft.fillRect(10, 120, 200, 20, ILI9341_BLACK); // Clear previous message
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println(message);
}