HazardsMind:
Is the whole triangle going to be the needle or will the triangle be on the tip of the needle?
#include <ITDB02_Graph16.h>
extern uint8_t SmallFont[];
//UTFT myGLCD(ITDB32S, 38,39,40,41); // Remember to change the model parameter to suit your display module!
ITDB02 myGLCD(A1, A2, A0, A4, A5, 2);
double pi = 3.14159265;
double x = 0, oldx = 50; // setting variable to 0 is not needed, it will be written over anyways, it just looks good.
double y = 0, oldy = 50;// same as above
int rad = 50; // radius 50 pixels
void setup()
{
myGLCD.InitLCD(LANDSCAPE);
myGLCD.setBackColor(50, 50, 50);
myGLCD.setFont(SmallFont);
myGLCD.setColor(125, 255, 255);
myGLCD.fillRect(50, 50, 150, 100);
delay(1);
}
double mD(double data, double in_min, double in_max, double out_min, double out_max)//Map double
{
return (data - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
void drawNeedle(int sensor, int pos_x, int pos_y) {
double cur = mD(analogRead(sensor), 0, 1023, 0, pi);
int rhbx = pos_x + sin(cur) * 10; // right half base
int rhby = pos_y + cos(cur) * 10; // right half base
x = pos_x + sin(cur + (pi / 2)) * rad; // tip
y = pos_y + cos(cur + (pi / 2)) * rad; // tip
int lhbx = pos_x + sin(cur + pi) * 10; // left half base
int lhby = pos_y + cos(cur + pi) * 10; // left half base
if (x != oldx && y != oldy)
{
myGLCD.setColor(125, 255, 255);
myGLCD.drawLine(oldx, oldy, pos_x, pos_y);
Triangle(oldx, oldy, rhbx, rhby, lhbx, lhby);
myGLCD.setColor(255, 0, 0);
myGLCD.drawLine(x, y, pos_x, pos_y); // (cx, cy, calculated x, calculated y)
Triangle(x, y,rhbx, rhby, lhbx, lhby);
oldx = x;
oldy = y;
myGLCD.printNumF(cur, 2, CENTER, 10); //used for debugging
}
}
void loop() {
drawNeedle(A15, 100, 100);
}
void Triangle(int x1, int y1,int x2,int y2,int x3,int y3)
{
myGLCD.drawLine(x1,y1,x2,y2);// lean left
myGLCD.drawLine(x2,y2,x3,y3);// _ base
myGLCD.drawLine(x3,y3,x1,y1);// / lean right
}
It doesn't quite delete the old triangle before drawing the new one. It leaves an outline.
But the arc part of the needle seems to be working.