Hello
When (tft.println) print value on the screen every second, causes stop of stepper motor. How to avoid.
int relay1Pin = 23; // relay
int relay2Pin = 25;
int relay3Pin = 27;
int relay4Pin = 29;
int m0 = 39; // stepper
int m1 = 41;
int sleepPin = 47;
const int stepPin = 49;
int dirPin = 51;
#define sclk 76 //TFT
#define mosi 75
#define cs 10
#define dc 9
#define rst 8
int stepPinState = LOW;
long previous1 = 0;
long previous2 = 0;
long previous3 = 0;
long interval = 500;
static byte runStepper = 0;
static byte autoStepper = 0;
long countImpulse = 0;
int lastStepPinState = LOW;
int stepperRev = 0;
int countDistance = 0;
int timer = 0;
#include <Keypad.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
#include <SPI.h>
Adafruit_ST7735 tft = Adafruit_ST7735(cs, dc, rst);
const byte ROWS = 4; // four rows
const byte COLS = 4; // four columns
// define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
{
'1','2','3','A' }
,
{
'4','5','6','B' }
,
{
'7','8','9','C' }
,
{
'*','0','#','D' }
};
byte rowPins[ROWS] = {
22, 24, 26, 28}; // connect to the row pinouts of the keypad
byte colPins[COLS] = {
30, 32, 34, 36}; // connect to the column pinouts of the keypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); // initialize an instance of class NewKeypad
void setup()
{
Serial.begin(9600);
pinMode(relay1Pin, OUTPUT); // relay
pinMode(relay2Pin, OUTPUT);
pinMode(relay3Pin, OUTPUT);
pinMode(relay4Pin, OUTPUT);
pinMode(m0, OUTPUT); // stepper
pinMode(m1, OUTPUT);
pinMode(sleepPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
digitalWrite(m0, LOW);
digitalWrite(m1, LOW);
tft.initR(INITR_REDTAB);
}
void loop()
{
char customKey = customKeypad.getKey();
if (customKey == '0') // distance - reset
{
countDistance = 0;
}
if (customKey == '1') // relay 1 on
{
digitalWrite(relay1Pin,LOW);
}
if (customKey == '4') // relay 1 off
{
digitalWrite(relay1Pin,HIGH);
}
if (customKey == '2') // relay 2 on
{
digitalWrite(relay2Pin,LOW);
}
if (customKey == '5') // relay 2 off
{
digitalWrite(relay2Pin,HIGH);
}
if (customKey == '3') // relay 3 on
{
digitalWrite(relay3Pin,LOW);
}
if (customKey == '6') // relay 3 off
{
digitalWrite(relay3Pin,HIGH);
}
if (customKey == 'A') // stepper - right direction
{
runStepper = 1;
digitalWrite(sleepPin, HIGH);
digitalWrite(dirPin, LOW);
}
if (customKey == 'B') // stepper - left direction
{
runStepper = 2;
digitalWrite(sleepPin, HIGH);
digitalWrite(dirPin, HIGH);
}
if (customKey == 'C') //stepper stop
{
runStepper = 0;
autoStepper = 0;
digitalWrite(sleepPin, LOW);
}
if (runStepper == 0) // stepper - stand by off
{
digitalWrite(sleepPin, LOW);
}
if (runStepper >= 1) // delay for a stepper
{
unsigned long current1 = micros();
if(current1 - previous1 > interval)
{
previous1 = current1;
if (stepPinState == LOW)
stepPinState = HIGH;
else
stepPinState = LOW;
digitalWrite(stepPin, stepPinState);
}
}
if (customKey == 'D') // stepper - automatic feed
{
autoStepper=1;
}
int sensorValue = analogRead(A0); // read sensor Value
int currentValue = sensorValue * (3.3 / 1024.0)*100;
if (currentValue >= 244 && autoStepper==1)
{
runStepper = 3;
digitalWrite(sleepPin, HIGH);
digitalWrite(dirPin, HIGH);
}
if (currentValue < 244 && autoStepper==1)
{
runStepper = 0;
digitalWrite(sleepPin, LOW);
}
countImpulse = 0;
if (stepPinState != lastStepPinState) // counting steps of stepper
{
if (stepPinState == HIGH)
{
countImpulse = 1;
countDistance;
if (runStepper == 1)
{
countDistance = countDistance + 1;
}
if (runStepper == 2)
{
countDistance = countDistance - 1;
}
if (runStepper == 3)
{
countDistance = countDistance + 1;
}
}
}
lastStepPinState = stepPinState;
stepperRev = (countImpulse*interval*0.54);
if (autoStepper == 1)
{
unsigned long current2 = millis(); // timer
if(current2 - previous2 > 1000)
{
timer++;
previous2 = current2;
}
}
unsigned long current3 = millis(); // delay for serial monitor
if(current3 - previous3 > 1000)
{
tft.setCursor(50, 0);
tft.setTextColor(ST7735_WHITE);
tft.setTextSize(1);
tft.fillScreen(ST7735_BLACK);
tft.println(countDistance*0.01);
previous3 = current3;
}
}