I have ben running this sketch flawlessly for a week and decided to introduce a potentiometer and hall effect sensor over the weekend and now I have a gremlin somewhere.
When I run the void dblros(); code block every 80 or so button pushes the code will screw up, the table will slide in but on the way out it will stop and grind like it hit the end of the rail then stop and run the last line of code to move the table back out again so I get an in/out/out action and miss the in for some reason along with the grinding stop.
I can reset it by powering on and off to home the slider table again and then it works fine for another 80 or so button pushes and then does it again, have I not kept track of something I should have with the new sensors? It looks right to me but that isn't saying a whole lot. lol
#include <SPI.h>
#include <Wire.h>
#include <Stepper.h>
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "Adafruit_STMPE610.h"
// 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
Stepper tableSlider = Stepper(200, 5, 6); //table slider pin 5 and 6
Stepper subsRotate = Stepper(200, 3, 4); //substrate rotate pin 8 and 9
const int gluemac = 7; // pin to actuate glue machine relay
const int button = 2; // pin button is on
int val = 0; // current button state
int old_val = 0;
int state = 0;
unsigned long time = 0;
unsigned long debounce = 15;
int speedKnob = A1; //variable speed for double rosette
int sensorValue = 0; //store pot value
int positionSensor = A0; //hall effect home pin
int homeVal = 0;
// 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 = 2;
Adafruit_GFX_Button btn[_numBtns];
int btnColor[8] = {RED, GREEN, BLUE, CYAN, MAGENTA, YELLOW, WHITE, BLACK};
TS_Point p;
int x, y;
void invertBtn (byte btnHit) {
btn[btnHit].drawButton(true);
delay(300);
// normal
btn[btnHit].drawButton(false);
}
void tableHome() {
homeVal = digitalRead(positionSensor);
tableSlider.setSpeed(1000);
while (homeVal == HIGH) {
tableSlider.step(100); //home table to sensor
homeVal = digitalRead(positionSensor);
}
}
void single() {
Serial.println("Single function called.");
tableSlider.step(7000); //slide table in
Serial.println("clockwise");
digitalWrite(gluemac, LOW); //turn glue machine on
Serial.println("relay_on");
delay(300);
subsRotate.step(800); //rotate 1 time
Serial.println("rotate");
digitalWrite(gluemac, HIGH); //turn glue off
Serial.println("relayoff");
subsRotate.step(300); //rotate a little more glue dropping
Serial.println("rotate");
delay(300);
tableSlider.step(-7000); // slide table out
Serial.println("counterclockwise");
state = 0; //turn everything off
}
void dblros()
{
Serial.println("Double function called.");
tableSlider.step(8800); //slide table in
Serial.println("clockwise");
digitalWrite(gluemac, LOW); //turn glue machine on
Serial.println("relay_on");
delay(300);
subsRotate.step(800); //rotate 1 time
Serial.println("rotate");
digitalWrite(gluemac, HIGH); //turn glue off
Serial.println("relayoff");
tableSlider.step(-8800); // slide table out
Serial.println("counterclockwise");
sensorValue = analogRead(speedKnob) * 10;
delay(sensorValue);
tableSlider.step(11000); // slide table in
Serial.println("clockwise");
digitalWrite(gluemac, LOW); //turn glue machine on
Serial.println("relay_on");
delay(300);
subsRotate.step(800); //rotate 1 time
Serial.println("rotate");
digitalWrite(gluemac, HIGH); //turn glue off
Serial.println("relayoff");
subsRotate.step(200); //rotate a little more glue dropping
Serial.println("rotate");
delay(300);
tableSlider.step(-11000); // slide table out
Serial.println("counterclockwise");
state = 0; //turn everything off
}
void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(gluemac, OUTPUT);
digitalWrite(gluemac, HIGH); //relay is active low
pinMode(positionSensor, INPUT_PULLUP);
tableHome();
tableSlider.setSpeed(3000); // table slide
subsRotate.setSpeed(100); //substrate rotate
Serial.begin(9600);
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, 100, 60, 150, 50, BLACK, btnColor[0], BLACK, "1 INCH", 2 );
btn[0].drawButton(false);
btn[1].initButton( &tft, 100, 130, 150, 50, BLACK, btnColor[1], BLACK, "DOUBLE", 2 );
btn[1].drawButton(false);
}
void loop() {
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()) {
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);
}
}
}
homeVal = digitalRead(positionSensor);
if (btn[0].contains(x, y) && homeVal == HIGH) {
tableSlider.step(1000);
}
else if (btn[1].contains(x, y) && homeVal == LOW ) {
tableSlider.step(-4000); //home table to double rosette
}
val = digitalRead(button); //read input value and store it
Serial.print("val = "); Serial.println(val); Serial.print("old_val = "); Serial.println(old_val);
if (val == LOW && old_val == HIGH && millis() - time > debounce ) { //check if there was a transition
Serial.print("state = "); Serial.println(state);
state = 1 - state;
time = millis(); //button debounce
}
old_val = val; //store old value
if (state == 1 && btn[0].contains(x, y)) {
single();
}
else if (state == 1 && btn[1].contains(x, y)) {
dblros();
}
}