I have a functioning project that uses code I have written and the last gremlin to fixes to do with inadvertent button presses by the operator.
I have a foot switch that when pressed it will start movement of stepper motors, they are all controlled by if statements and they move as coded but if the foot switch is activated a second time while the steppers are moving everything comes to a halt, except for one motor that I just turn on and let run and it will run all day until power is turned off to reset the Arduino.
I'm having trouble adding some statement to the existing if statements that I have to ignore any inadvertent button presses but for the life of me I'm stuck and was hoping a nudge will get me thinking about it correctly.
I tried to be cheeky and used the millis delay on the button to increase the debounce for a second or two and by that time movement would be stopped on the motors and everything would be reset and waiting for another button push. It worked a couple times and I thought I was good enough but then the button push wasn't registering for some reason.
Sorry for the length of the code, if you need me to pair it down let me know.
Any guidance is appreciated.
#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 = 2; //(0->3)
TS_Point p;
int x, y;
const byte row = 4;
const byte col = 2;
byte lastHit = 0;
byte currentHit = 6;
Adafruit_GFX_Button btn[row * col];
//VALUES FOR BUTTON
const int button = 2; // pin button is on
int val = 0; // current button state
int old_val = 0;
int buttonstate = 0;
unsigned long previousMillis = 0;
//VALUES FOR TURNING STAPLER AT SELECTED NUMBER OF TURNS(using stapler hall Sensor) plus delay the start
int staplercounter = 0;
int staplercurrentState = 0;
int staplerpreviousState = 0;
int staplerSensor = 47;
unsigned long previousMillis2 = 0;
const long interval = 25;
//VALUE FOR GANTRY MOVE
int gantryin = 46;
int gantryout = 43;
int gantryhome;
int gantryfinish;
//VALUE FOR CARRIAGE MOVE
int carriagebottom = 45;
int carriagetop = 44;
int carriagehome;
int carriagefinish;
//VALUES FOR VARIABLE STEPS
int start = 0;
int steps = 0;
int staples = 0;
void setup() {
delay(500); //Allow LCD screen time to power up
pinMode(button, INPUT);
pinMode(staplerSensor, INPUT);
pinMode(gantryout, INPUT);
pinMode(gantryin, INPUT);
pinMode(carriagetop, INPUT);
pinMode(carriagebottom, INPUT);
Serial.begin(115200);
tft.begin();
ts.begin();
if (!ts.begin()) {
Serial.println("Couldn't start touchscreen controller");
while (1);
}
tft.setRotation(rotation);
tft.fillScreen(BLACK);
btnGrid();
gantry.setMaxSpeed(1000.0);
gantry.setAcceleration(1000.0);
carriage.setMaxSpeed(1000.0);
carriage.setAcceleration(1000.0);
stapler.setMaxSpeed(10000.0);
roserotate.setMaxSpeed(1000.0);
previousMillis = millis();
while (digitalRead(carriagetop) == HIGH) {
carriage.move(1);
carriage.setSpeed(800);
carriage.runSpeed();
}
while (digitalRead(gantryin) == HIGH) {
gantry.setSpeed(500);
gantry.runSpeed();
}
delay(1000);
while (digitalRead(gantryout) == HIGH) {
gantry.setSpeed(-500);
gantry.runSpeed();
}
}
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;
}
while (ts.touched()) {
for (uint8_t b = 0; b < row * col; b++) {
if (btn[b].contains(x, y)) {
btn[b].press(true);
btn[b].drawButton(true);
currentHit = b;
} else if (btn[b].contains(x, y) == false) {
btn[b].press(false);
if (b == lastHit) {
btn[b].drawButton(false);
}
} else {
return;
}
}
lastHit = currentHit;
}
}
if (btn[0].contains(x, y)) {
start = 1;
steps = 10;
staples = 4;
}
else if (btn[1].contains(x, y)) {
start = 1;
steps = 8;
staples = 5;
}
else if (btn[2].contains(x, y)) {
start = 1;
steps = 8;
staples = 6;
} else if (btn[3].contains(x, y)) {
start = 1;
steps = 3;
staples = 8;
}
else if (btn[4].contains(x, y)) {
start = 1;
steps = 9;
staples = 11;
}
else if (btn[5].contains(x, y)) {
start = 1;
steps = 7;
staples = 13;
}
else if (btn[6].contains(x, y)) {
start = 2;
loadwire();
}
else if (btn[7].contains(x, y)) {
start = 1;
staples = 1;
}
/////////////////////////////////////////////////////////////////////////////////
starter();
counter();
gantryhome = digitalRead(gantryin);
gantryfinish = digitalRead(gantryout);
carriagehome = digitalRead(carriagebottom);
carriagefinish = digitalRead(carriagetop);
staplercurrentState = digitalRead(staplerSensor);
if (buttonstate == HIGH && gantryhome == HIGH && start == 1) {
gantry.move(1); //move to sensor for stapling
gantry.setSpeed(900);
}
if (buttonstate == HIGH && carriagehome == HIGH && start == 1) {
carriage.move(-1); //move to sensor for stapling
carriage.setSpeed(900);
}
if (buttonstate == HIGH && staplercurrentState == LOW && staplercounter >= 0 && start == 1) { //In position turn with stapler count
roserotate.move(-steps);
roserotate.setSpeed(-800);
}
if (buttonstate == HIGH && gantryhome == LOW && start == 1) { //run the stapler
stapler.setSpeed(7000);
}
///////////////////////////////////////////////////////////////////////////////
if (staplercounter >= staples && gantryfinish == HIGH && start == 1) { //Slide gantry back to start position
stapler.setSpeed(0); //turn stapler motor off
gantry.moveTo(-1); //move gantry back to start position
gantry.setSpeed(-800);
}
if (staplercounter >= staples && carriagefinish == HIGH && start == 1) { //Slide carriage back to start position
carriage.move(250);
carriage.setSpeed(600);
}
if (staplercounter >= staples && start == 1 && carriagefinish == HIGH) {
buttonstate = 0; //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() {
val = digitalRead(button);
if ( (millis() - previousMillis) >= 200) { //state machine for button & debounce
if ((val == 0) && (old_val == 1)) {
buttonstate = 1 - buttonstate;
}
previousMillis = millis();
old_val = val;
//Serial.println(buttonstate);
}
}
void counter() {
unsigned long currentMillis2 = millis();
if (buttonstate == HIGH) {
if (currentMillis2 - previousMillis2 >= interval) {
previousMillis2 = currentMillis2;
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 btnGrid()
{
int left, top;
int l = 10;
int t = 30;
int w = 100;
int h = 40;
byte hgap = 30;
byte vgap = 20;
byte id = 0;
char *titleStr[row * col] = {"4", "5", "6", "8", "11", "13", "Load", "1"};
for (byte j = 0; j < row; j++) {
for (byte i = 0; i < col; i++) {
left = l + i * (w + vgap);
top = t + j * (h + hgap);
btn[id].initButtonUL( &tft, left, top, w, h, WHITE, RED, GREEN, titleStr[id], 3 );
if (id == currentHit) {
// inverted
btn[id].drawButton(true);
} else {
btn[id].drawButton(false);
}
id++;
}
}
}
void loadwire() {
if (buttonstate == HIGH) {
stapler.setSpeed(6000);
}
if (staplercounter == 1) {
stapler.setSpeed(0);
buttonstate = LOW;
staplercounter = 0;
staplercurrentState = 0;
}
stapler.runSpeed();
}