Here is the full code in case you have a question about how it is used.
#include <AccelStepper.h>
#include <SPI.h>
#include <Wire.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Adafruit_STMPE610.h"
AccelStepper stapler(AccelStepper::DRIVER, 3, 4);
AccelStepper roserotate(AccelStepper::DRIVER, 12, 13);
AccelStepper gantry(AccelStepper::DRIVER, 5, 6);
AccelStepper carriage(AccelStepper::DRIVER, 7, 11);
// Default values for Adafruit shield v2.
#define STMPE_CS 8
#define TFT_DC 9
#define TFT_CS 10
Adafruit_STMPE610 ts = Adafruit_STMPE610(STMPE_CS);
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define TS_MINX 150
#define TS_MINY 130
#define TS_MAXX 3800
#define TS_MAXY 4000
// Rotations 0,2 = portrait : 0->USB=right,upper : 2->USB=left,lower
// Rotations 1,3 = landscape : 1->USB=left,upper : 3->USB=right,lower
byte rotation = 1; //(0->3)
const byte _numBtns = 3;
Adafruit_GFX_Button btn[_numBtns];
int btnColor[8] = {RED, GREEN, CYAN, BLUE, MAGENTA, YELLOW, WHITE, BLACK};
TS_Point p;
int x, y;
//VALUES FOR BUTTON
const int button = 2; // pin button is on
int val = LOW; // current button state
int old_val = LOW;
int buttonstate = LOW;
unsigned long previousMillis;
//DELAY FOR LCD SCREEN
unsigned long previousmillis3;
const unsigned long pause = 300;
//VALUES FOR TURNING STAPLER 4 TURNS(using stapler hall Sensor) plus delay the start
int staplercounter = 0;
int staplercurrentState = 0;
int staplerpreviousState = 0;
int staplerSensor = 47;
//VALUE FOR PAUSING ROSETTE TURN
unsigned long previousMillis2;
const unsigned long interval = 210;
//VALUE FOR GANTRY MOVE
int gantryin = 46;
int gantryout = 43;
int gantryhome = 0;
int gantryfinish = 0;
//VALUE FOR CARRIAGE MOVE
int carriagebottom = 45;
int carriagetop = 44;
int carriagehome = 0;
int carriagefinish = 0;
//VALUES FOR VARIABLE STEPS
int start_steps = 0;
int staples = 0;
void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(staplerSensor, INPUT_PULLUP);
pinMode(gantryout, INPUT_PULLUP);
pinMode(gantryin, INPUT_PULLUP);
pinMode(carriagetop, INPUT_PULLUP);
pinMode(carriagebottom, INPUT_PULLUP);
Serial.begin(115200);
tft.begin();
ts.begin();
if (!ts.begin()) {
Serial.println("Couldn't start touchscreen controller");
while (1);
}
tft.setRotation(rotation);
tft.fillScreen(BLUE);
// x coordinate of 100 doesn't seem right, but it works.
btn[0].initButton( &tft, 75, 60, 100, 50, BLACK, btnColor[0], BLACK, "4", 2 );
btn[0].drawButton(false);
btn[1].initButton( &tft, 75, 130, 100, 50, BLACK, btnColor[1], BLACK, "5", 2 );
btn[1].drawButton(false);
btn[2].initButton( &tft, 75, 200, 100, 50, BLACK, btnColor[2], BLACK, "6", 2 );
btn[2].drawButton(false);
gantry.setMaxSpeed(10000.0);
gantry.setAcceleration(15000.0);
carriage.setMaxSpeed(10000.0);
carriage.setAcceleration(15000.0);
stapler.setMaxSpeed(10000.0);
stapler.setAcceleration(10000.0);
roserotate.setMaxSpeed(1000);
roserotate.setAcceleration(1000);
previousMillis = millis();
previousMillis2 = millis();
// Homing();
}
void loop() {
/////VALUES FOR LCD SCREEN
/////////////////////////////////////////////////////////////////////////////////
if (!ts.bufferEmpty()) {
p = ts.getPoint();
switch (rotation) {
case 0:
x = map(p.x, TS_MINX, TS_MAXX, 0, tft.width());
y = map(p.y, TS_MINY, TS_MAXY, 0, tft.height());
break;
case 1:
// p.x, p.y reversed //
x = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
y = map(p.x, TS_MAXX, TS_MINX, 0, tft.height());
break;
case 2:
x = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());
break;
case 3:
// p.x, p.y reversed //
x = map(p.y, TS_MAXY, TS_MINY, 0, tft.width());
y = map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
break;
case 4:
x = map(p.x, TS_MAXX, TS_MINX, 0, tft.width());
y = map(p.y, TS_MAXY, TS_MINY, 0, tft.height());
break;
case 5:
// p.x, p.y reversed //
x = map(p.y, TS_MAXY, TS_MINY, 0, tft.width());
y = map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
break;
}
while (ts.touched()) {
if (btn[0].contains(x, y)) {
Serial.println("Button 1 hit.");
invertBtn(0);
}
if (btn[1].contains(x, y)) {
Serial.println("Button 2 hit.");
invertBtn(1);
}
if (btn[2].contains(x, y)) {
Serial.println("Button 3 hit.");
invertBtn(2);
}
}
}
if (btn[0].contains(x, y)) {
start_steps = 3;
staples = 4;
}
else if (btn[1].contains(x, y)) {
start_steps = 4;
staples = 5;
}
else if (btn[2].contains(x, y)) {
start_steps = 5;
staples = 6;
}
/////////////////////////////////////////////////////////////////////////////////
starter();
counter();
gantryhome = digitalRead(gantryin);
gantryfinish = digitalRead(gantryout);
carriagehome = digitalRead(carriagebottom);
carriagefinish = digitalRead(carriagetop);
staplercurrentState = digitalRead(staplerSensor); //set state for movement
if (buttonstate == HIGH && gantryhome == HIGH && staplercounter < start_steps) {
gantry.move(50); //move to sensor for stapling
gantry.setSpeed(2500);
}
if (buttonstate == HIGH && carriagehome == HIGH && staplercounter < start_steps) {
carriage.move(-50); //move to sensor for stapling
carriage.setSpeed(2000);
}
if (buttonstate == HIGH && staplercounter >= 1) {
if ( (millis() - previousMillis2) >= interval) {
roserotate.move(-45);
roserotate.setSpeed(700);
previousMillis2 = millis();
if (staplercounter >= staples) {
roserotate.setSpeed(0);
}
}
}
if (buttonstate == HIGH && gantryhome == LOW) {
stapler.setSpeed(-6000);
}
///////////////////////////////////////////////////////////////////////////////
if (staplercounter >= staples && gantryfinish == HIGH) { //Slide gantry back to start position
stapler.setSpeed(0); //turn stapler motor off
gantry.moveTo(-50);
gantry.setSpeed(2500);
if (staplercounter >= staples && carriagefinish == HIGH) { //Slide carriage back to start position
carriage.moveTo(50);
carriage.setSpeed(2000);
buttonstate = LOW; //reset button
staplercounter = 0; //reset counter for next button push
staplercurrentState = 0; //reset state for next button push
}
}
stapler.runSpeed();
gantry.runSpeedToPosition();
carriage.runSpeedToPosition();
roserotate.runSpeedToPosition();
}
void starter() {
if ( (millis() - previousMillis) >= 50) { //state machine for button & debounce
val = digitalRead(button);
if ((val == LOW) && (old_val == HIGH)) {
buttonstate = HIGH;
}
previousMillis = millis();
old_val = val;
//Serial.println(buttonstate);
}
}
void counter() {
staplercurrentState = digitalRead(staplerSensor); //used to time all events
if (staplercurrentState != staplerpreviousState) { //check the count and add 1
if (staplercurrentState == 1) {
staplercounter = staplercounter + 1;
Serial.println(staplercounter);
}
}
staplerpreviousState = staplercurrentState;
}
void Homing() {
digitalRead(staplerSensor);
if (staplerSensor == HIGH) {
stapler.move(-5);
stapler.setSpeed(100);
staplercounter = 0;
}
}
void invertBtn (byte btnHit) {
btn[btnHit].drawButton(true);
delay(300);
// normal
btn[btnHit].drawButton(false);
}