#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <TouchScreen.h>
#define TFT_CS 11
#define TFT_DC 10
#define TFT_MOSI 6
#define TFT_SCK 13
#define TFT_MISO 12
#define TS_CS 5
#define TS_IRQ 7
#define XP A2
#define YP A3
#define XM A1
#define YM A0
#define TS_MINX 150
#define TS_MINY 120
#define TS_MAXX 920
#define TS_MAXY 940
#define TS_SAMPLING 2
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCK, TFT_MISO);
// Define the pins for the stepper motor control
const int stepPin = 9; // Step pin connected to A4988 STEP pin
const int dirPin = 8; // Direction pin connected to A4988 DIR pin
const int buttonPin = 2; // Start button pin
const int buttonPinOff = 4; // Stop button pin
const int potPinSpeed = A0; // Potentiometer pin for speed control
const int potPinRest = A1; // Potentiometer pin for rest time control
const int dirChange = 7; // Direction change button pin
// Define variables
String inputCommand;
int stepDelay = 1000; // Initial delay between steps in microseconds
int restTime = 1000; // Initial rest time between rotations in milliseconds
bool motorRunning = false; // Flag to track motor state
int angle=10;
int previousPotValueSpeed ;
void setup() {
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP); // Internal pull-up resistor
pinMode(buttonPinOff, INPUT_PULLUP); // Internal pull-up resistor
pinMode(dirChange, INPUT_PULLUP);
int previousPotValueSpeed = analogRead(potPinSpeed);
// Set initial direction (clockwise)
digitalWrite(dirPin, HIGH);
// Initialize Serial communication
Serial.begin(9600);
Serial.println("{ \"bounce\": \"0\" }");
Serial.println("{ \"arrow\": \"orange\" }");
Serial.println("Enter the speed and rest time if you want to change digitally: ");
}
void loop() {
// Read the state of the start button
const int dirState = digitalRead(dirChange);
if (digitalRead(buttonPin) == LOW && !motorRunning) {
// Start the motor
motorRunning = true;
Serial.println("Motor started");
digitalWrite(dirPin, HIGH); // Set direction (clockwise)
}
// Read the state of the stop button
if (digitalRead(buttonPinOff) == LOW && motorRunning) {
// Stop the motor
motorRunning = false;
Serial.println("Motor stopped");
}
// Change direction if the button is pressed
if (dirState == LOW) {
if (dirState == LOW) {
const int previousDirection = digitalRead(dirPin);
digitalWrite(dirPin, previousDirection == HIGH ? LOW : HIGH);
Serial.println("Direction Changed");
}
}
// If motor is running, rotate the stepper motor
if (motorRunning) {
// Read analog input from potentiometer to control speed
int currentPotValueSpeed = analogRead(potPinSpeed);
if (currentPotValueSpeed != previousPotValueSpeed){
previousPotValueSpeed=currentPotValueSpeed;
stepDelay = map(currentPotValueSpeed, 0, 1023, 1000, 100); // Adjust the range for speed
}
// Check for digital input to change speed or rest time
if (Serial.available() > 0) {
// int temp = Serial.parseInt();
String input = Serial.readStringUntil('\n'); // Read until newline character
// Parse input command
inputCommand = input.substring(0, 2); // Extract first two characters
int value = input.substring(2).toInt(); // Extract the rest of the input as integer value
// Check command and update variables accordingly
if (inputCommand == "RT") {
restTime = value;
Serial.print("Rest time updated to: ");
Serial.println(restTime);
} else if (inputCommand == "AN") {
angle = value;
Serial.print("Angle updated to: ");
Serial.println(angle);
} else if(inputCommand == "ST"){
stepDelay=value;
Serial.print("stepDelay updated to: ");
Serial.println(stepDelay);
}else {
Serial.println("Invalid command");
}
}
// Execute the motor steps with the current speed
for (int i = 0; i < 200; i++) { // 200 steps for one revolution (adjust as needed)
digitalWrite(stepPin, HIGH);
delayMicroseconds(stepDelay); // Adjust delay for speed
digitalWrite(stepPin, LOW);
delayMicroseconds(stepDelay); // Adjust delay for speed
}
Serial.print("Step delay: ");
Serial.println(stepDelay);
// Pause between rotations
Serial.print("rest time: ");
Serial.println(restTime);
Serial.print("angle: ");
Serial.println(angle);
delay(restTime);
}
}