Altering line angle on TFT Display

Hi all!

I am making a big project; a Arduino electric scooter!

Right now I am working on the UI (on a 2.8" TFT LCD display), and am wondering how I would program the screen to just display a simple horizontal line across the middle (to represent ground angle) that changes according to a gyroscope input.

I would post all of my code, but it is ~460 lines right now and counting :upside_down_face:.

I am using this library to control the display:

This seems like something simple, but I can't wrap my head around it lol.

Thanks in advance for any tips!

I recommend the Adafruit GFX library. It is trivial to draw lines at any angle, between any two points on the screen, using the drawLine() function.

I see that the DFRobot library adopted the GFX library internally, so the same calls should work.

1 Like

Ah, great! Thanks again @jremington; that was a simple fix :wink:.

Syntax:
screen.drawLine(int16_t x0, int16_t y0, int16_t x1, int16_t y1, uint16_t color)

How would I program the line angle to reflect the gyroscope x axis (or a single angle value)?

1 Like

Thanks @jremington!

Something like this?

#define DEG2RAD 0.0174532925
float angle = 30//gyro x
int x=160,y=120;

float cosa = cos(angle * DEG2RAD), sina = sin(angle * DEG2RAD);
int x = x - ((w * cosa / 2) - (h * sina / 2));
int y = y - ((h * cosa / 2) + (w * sina / 2));

Try it and see what happens.

The beauty of Arduino is that you can write a short program and run it in a matter of minutes.

That is much faster than posting code snippets with undefined variables and mystery constants, then hoping that forum members can make sense of your question.

1 Like

Haha, good point.

After altering some things 20 times, here's what I've got; and it works perfectly!

  #define DEG2RAD 0.0174532925
  float screenangle1 = 135; //value for right line to make it level
  float screenangle2 = 315; //value for left line to make it level; appears to be one line instead of two separate ones
  float angle1 = 10;//testing angle; is going to be gyro x
  float angle2 = angle1;  
  int x=160,y=140; //center point of the two lines
  int w=110,h=110; //length of each line; one going right and one going left
 
  float cosa1 = cos((screenangle1+angle1) * DEG2RAD), sina1 = sin((screenangle1+angle1) * DEG2RAD);
  int otherx1 = x - ((w * cosa1 / 2) - (h * sina1 / 2));
  int othery1 = y - ((h * cosa1 / 2) + (w * sina1 / 2));
  screen.drawLine(x, y, otherx1, othery1, COLOR_RGB565_CYAN);//line going right
  screen.drawLine(x+1, y+1, otherx1+1, othery1+1, COLOR_RGB565_CYAN);//double thick line
  float cosa2 = cos((screenangle2+angle2) * DEG2RAD), sina2 = sin((screenangle2+angle2) * DEG2RAD);
  int otherx2 = x - ((w * cosa2 / 2) - (h * sina2 / 2));
  int othery2 = y - ((h * cosa2 / 2) + (w * sina2 / 2));
  screen.drawLine(x, y, otherx2, othery2, COLOR_RGB565_CYAN);//line going left
  screen.drawLine(x+1, y+1, otherx2+1, othery2+1, COLOR_RGB565_CYAN);//double thick line

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