Tft not responding

I have arduino uno with 3.5"(?) tft touchscreen and using MCUFRIEND_KBV library
Screen itself is working and buttons do work but when I add my rotary encoder to it, the buttons stop working and screen does not respond to touch.

#if 1
#include <Arduino.h>
#include <RotaryEncoder.h>
#define PIN_IN1 12
#define PIN_IN2 13
#define ROTARYSTEPS 0
#define ROTARYMIN -360
#define ROTARYMAX 360
RotaryEncoder encoder(PIN_IN1, PIN_IN2, RotaryEncoder::LatchMode::TWO03);

int lastPos = -1;

#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#include <Fonts/FreeSans9pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSerif12pt7b.h>

#include <FreeDefaultFonts.h>
#define MINPRESSURE 50
#define MAXPRESSURE 1000
void(* resetFunc) (void) = 0;
// ALL Touch panels and wiring is DIFFERENT
// copy-paste results from TouchScreen_Calibr_native.ino
const int XP = 7, XM = A1, YP = A2, YM = 6; //240x320 ID=0x6809
const int TS_LEFT = 928, TS_RT = 105, TS_TOP = 63, TS_BOT = 918;


TouchScreen ts = TouchScreen(XP, YP, XM, YM, 400);

Adafruit_GFX_Button on_btn, off_btn, on1_btn, off1_btn, stop_btn;

int pixel_x, pixel_y;     //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);      //restore shared pins
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);   //because TFT control pins
  digitalWrite(XM, HIGH);
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);
  if (pressed) {
    pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
    pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());
  }
  return pressed;
}

#define BLACK   0x0000
#define NAVY 0x000F
#define DARKGREEN 0x03E0
#define DARKCYAN 0x03EF
#define MAROON 0x7800
#define PURPLE 0x780F
#define OLIVE 0x7BE0
#define LIGHTGREY 0xC618
#define DARKGREY 0x7BEF
#define BLUE 0x001F
#define GREEN 0x07E0
#define CYAN 0x07FF
#define RED 0xF800
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define ORANGE 0xFD20
#define GREENYELLOW 0xAFE5
#define PINK 0xF81F

void setup(void)
{

  pinMode(10, OUTPUT); // Motor pin 1
  pinMode(11, OUTPUT); // Motor pin 2

  Serial.begin(115200);

  uint16_t ID = tft.readID();

  if (ID == 0xD3D3) ID = 0x9486; // write-only shield
  tft.begin(ID);
  tft.setRotation(0);            //PORTRAIT
  tft.fillScreen(BLACK);
  on_btn.initButton(&tft,  60, 250, 115, 40, LIGHTGREY, LIGHTGREY, BLACK, "R-LEFT", 2);
  off_btn.initButton(&tft, 180, 250, 115, 40, LIGHTGREY, LIGHTGREY, BLACK, "R-RIGHT", 2);


  on1_btn.initButton(&tft,  60, 299, 115, 40, LIGHTGREY, LIGHTGREY, BLACK, "SWR / PWR", 2);
  off1_btn.initButton(&tft, 180, 299, 115, 40, LIGHTGREY, LIGHTGREY, BLACK, "RESET", 2);


  stop_btn.initButton(&tft, 120, 205, 235, 40, LIGHTGREY, LIGHTGREY, RED, "STOP!", 2);

  on_btn.drawButton(false);
  off_btn.drawButton(false);

  on1_btn.drawButton(false);
  off1_btn.drawButton(false);

  stop_btn.drawButton(false);

  tft.fillRect(0, 0, 240, 80, DARKGREY);


}


void rot(void) {
  for (int i = 0; i < 1000000; i++) {
    encoder.tick();
    int newPos = encoder.getPosition() * 0.75;

    if (newPos < ROTARYMIN) {
      analogWrite(10, 0);
      analogWrite(11, 0);
      newPos = ROTARYMIN;

    } else if (newPos > ROTARYMAX) {
      analogWrite(10, 0);
      analogWrite(11, 0);
      newPos = ROTARYMAX;

    }

    if (lastPos != newPos) {

      Serial.println(newPos);
      Serial.println();
      lastPos = newPos;
      tft.setTextColor(GREEN, BLACK);
      tft.setTextSize(2);
      tft.setCursor(5, 120);
      tft.print(newPos);
      tft.print("         ");
    }

  }
}



void loop(void)
{

  bool down = Touch_getXY();

  showmsgXY(65, 10, 3, NULL, "OH1CJO");
  showmsgXY(30, 40, 2, NULL, "Antenna rotator");
  showmsgXY(30, 60, 2, NULL, "SWR / PWR meter");

  showmsgXY2(5, 85, 1, NULL, "OUTSIDE TEMP: 5c");
  showmsgXY2(142, 85, 1, NULL, "INSIDE TEMP: 21c");

  on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
  off_btn.press(down && off_btn.contains(pixel_x, pixel_y));

  on1_btn.press(down && on1_btn.contains(pixel_x, pixel_y));
  off1_btn.press(down && off1_btn.contains(pixel_x, pixel_y));

  stop_btn.press(down && stop_btn.contains(pixel_x, pixel_y));



  if (on_btn.justReleased())
    on_btn.drawButton();
  if (off_btn.justReleased())
    off_btn.drawButton();
  if (on1_btn.justReleased())
    on1_btn.drawButton();
  if (off1_btn.justReleased())
    off1_btn.drawButton();
  if (stop_btn.justReleased())
    stop_btn.drawButton();

  if (stop_btn.justPressed()) {
    stop_btn.drawButton(true);
    analogWrite(10, 0);
    analogWrite(11, 0);

    tft.setTextColor(RED, BLACK);
    tft.setTextSize(2);
    tft.setCursor(10, 100);
    tft.print("     STOPPED!             ");
  }
  if (on_btn.justPressed()) {
    on_btn.drawButton(true);
    analogWrite(10, 100);
    analogWrite(11, 0);

    tft.setTextColor(RED, BLACK);
    tft.setTextSize(2);
    tft.setCursor(25, 100);
    tft.print(" Rotating left!  ");
  }

  if (off_btn.justPressed()) {
    off_btn.drawButton(true);
    analogWrite(11, 100);
    analogWrite(10, 0);

    tft.setTextColor(RED, BLACK);
    tft.setTextSize(2);
    tft.setCursor(25, 100);
    tft.print(" Rotating right!  ");
  }



  if (on1_btn.justPressed()) {
    on1_btn.drawButton(true);
    tft.setTextColor(WHITE, BLACK);
    tft.setTextSize(2);
    tft.setCursor(25, 100);
    tft.print("SWR / PWR METER!");

  }
  if (off1_btn.justPressed()) {
    off1_btn.drawButton(true);

    resetFunc();
  }
  rot();
}

#endif



void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg)
{
  int16_t x1, y1;
  uint16_t wid, ht;
  tft.setFont(f);
  tft.setCursor(x, y);
  tft.setTextColor(BLACK);
  tft.setTextSize(sz);
  tft.print(msg);
}

void showmsgXY2(int x, int y, int sz, const GFXfont *f, const char *msg)
{
  int16_t x1, y1;
  uint16_t wid, ht;
  tft.setFont(f);
  tft.setCursor(x, y);
  tft.setTextColor(WHITE);
  tft.setTextSize(sz);
  tft.print(msg);
}

Here is my code. at the end of the loop, there is rot();
its the encoder part thats needs to show the value on the screen all the time. if I remove the rot();
the screen starts working and buttons start working.
if I add the rot(); under the "R-LEFT" button then everything works until I press R-LEFT button. Then it starts showing the encoder value and no buttons work anymore.
How can I fix this?
I tried something like:

void rot(void){
   static unsigned long nextReport = 0;
   if ((signed long)(millis() - nextReport) <=0 ) 
      return ;
   nextReport = millis() + 100; 
    encoder.tick();
    int newPos = encoder.getPosition() * 0.75;

    if (newPos < ROTARYMIN) {
      analogWrite(10, 0);
      analogWrite(11, 0);
      newPos = ROTARYMIN;

    } else if (newPos > ROTARYMAX) {
      analogWrite(10, 0);
      analogWrite(11, 0);
      newPos = ROTARYMAX;

    }

    if (lastPos != newPos) {

      Serial.println(newPos);
      Serial.println();
      lastPos = newPos;
      tft.setTextColor(GREEN, BLACK);
      tft.setTextSize(2);
      tft.setCursor(5, 120);
      tft.print(newPos);
      tft.print("         ");
    }
}

but with this, the encoder value does not change at all. everything is frozen.
tldr: The screen buttons need to be working and the encoder value has to refresh
at the moment it is bad to turn antenna because I press "motor turn" button, the antenna starts turning and I cannot stop it. It stops when encoder reads 360 degree.

//edit. also if someone know this mcufriend_kbv library then how can I change "round" buttons to square?
Like the button edges are round. I want to change them straight 90degree. Have not figured it out how to.

Does the trouble begin when You physically connect the last device or does the trouble begin when the code for the last device is included?
I suspect schematics are needed.

what? the encoder is connected to uno all the time.
The code is the problem. if encoder shows me data, nothing else is responding.
If I remove the encoder part from code, buttons start working...

It looks like the libraries are trying to use the same resource, timer, interrupt, .....

That would be outside my comfort zone but lets wait and see if a more knowíng helper steps in!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.