Implementing circle on GLCD using UTFT graphics code to follow pan motor

Mapped 360 points representing a 360 degree turn of a pan on a Quickset pan/tilt motor. The 5K ohm feedback pot located on the pan motor is connected to 5 volts and run through a simple noise filter connected before the pan potentiometer input on A0. A 7 inch TFT CTE70 and mega 2560 are using the UTFT library. I need to draw a dot or small filled circle to represent the pan motion position. The small filled circle will move on top of a larger static circle which is labeled north, south east and west. Have developed a sketch but can't test it for couple of days. Even if it works it will be extremly slow since each x,y position of the solid circle is defined by 360 "if" statements. Please help with code that works and is faster. Some of the code was taken from the UTFT demo and buttons demo....
UTFT graphics and touch libraries at Electronics - Henning Karlsen. The full code sketch is attached and a sample of the code below was shortened to fit within the 9000 forum character limit.

CODE:

/* NEXT STEPS

  1. Connect D9,D10,D11 to scope and confirm ok (D8 already connected)
    2.Change momentary button operation so touch button continously activated pan and tilt motors when held down. ( Press and hold pan and tilt touch buttons).
    Make 360 if statements to define each x,y point to draw small dot or circle indicating pan position. Done.
    */

#include <UTFT.h>
#include <UTouch.h>
#include <UTFT_Buttons.h>

extern uint8_t SevenSegNumFont[];
extern uint8_t BigFont[]; //Declare font extern uint8_t BigFont[];
extern uint8_t SmallFont[];

UTFT myGLCD(CTE70CPLD,38,39,40,41); // Setup CTE70 TFT LCD/SD Shield for Arduino Mega
UTouch myTouch(6,5,4,3,2);
UTFT_Buttons myButtons(&myGLCD, &myTouch); // Setup UTFT buttons
int panPot = 0; //Declare and set pantPot to 0
int tiltPot = 0; //Declare and set tiltPot to 0
int panLeft = 0;
int panRight = 0;
int tiltUp = 0;
int tiltDown = 0;
int panLeftscope = 8;
int panRightscope = 9;
int tiltUpscope = 10;
int tiltDownscope = 11;
void setup()
{

// Serial.begin(9600);
pinMode(panLeftscope,OUTPUT);
pinMode(panRightscope,OUTPUT);
pinMode(tiltUpscope,OUTPUT);
pinMode(tiltDownscope,OUTPUT);

// Setup the LCD
myGLCD.InitLCD();
myGLCD.setFont(BigFont);
myGLCD.clrScr();

// Setup Touch and Buttons
myTouch.InitTouch();
myTouch.setPrecision(PREC_MEDIUM);
myButtons.setTextFont(BigFont);
// pinMode(ledPin, OUTPUT);

//Draw "Remote 1" at top center of screen
myGLCD.setBackColor(255, 0, 0);
myGLCD.setFont(BigFont);
myGLCD.print("* REMOTE 1 *", CENTER, 1);
myGLCD.setBackColor(0, 0, 0);
myGLCD.print("N",150,110);
myGLCD.print("E",30,230);
myGLCD.print("S",150,350);
myGLCD.print("W",275,230);
myGLCD.print("UP",585,30);
myGLCD.print("DOWN",570,440);

//Draw Tilt vertical line
myGLCD.setColor(100, 150, 85);
myGLCD.setBackColor(0, 0, 0);
myGLCD.drawLine(600, 90, 600, 400);

//Draw Tilt line horizontal increments
for (int i=100; i<400; i+=10)
myGLCD.drawLine(595, i, 605, i);
}
void loop()
{

//Setup Pan Button
int pressed_button;
int panLeft = myButtons.addButton(340,80,150,30,"Pan Left");
int panRight = myButtons.addButton(340,180,150,30,"Pan Right");
int tiltUp = myButtons.addButton(340,280,150,30,"Tilt Up");
int tiltDown = myButtons.addButton(340,380,150,30,"Tilt Down");
myButtons.drawButton(panLeft);
myButtons.drawButton(panRight);
myButtons.drawButton(tiltUp);
myButtons.drawButton(tiltDown);

while(1)
{
if (myTouch.dataAvailable() == true)
{
pressed_button = myButtons.checkButtons();
if (pressed_button == panLeft)
{
digitalWrite(panLeftscope, HIGH);
delay(100);
digitalWrite(panLeftscope, LOW);
delay(100);
}
if (pressed_button == panRight)
{
digitalWrite(panRightscope, HIGH);
delay(100);
digitalWrite(panRightscope, LOW);
delay(100);
}

if (pressed_button == tiltUp)
{
digitalWrite(tiltUpscope, HIGH);
delay(100);
digitalWrite(tiltUpscope, LOW);
delay(100);
}
if (pressed_button == tiltDown)
{
digitalWrite(tiltDownscope, HIGH);
delay(100);
digitalWrite(tiltDownscope, LOW);
delay(100);
}

}
// else
{
// digitalWrite(panLeftscope, LOW);
}

// Setup Pan Number Indicator
int panPot = analogRead(A0);
panPot = map(panPot,0,1023,100,360);
myGLCD.setColor(255, 150, 100);
myGLCD.setBackColor(0,0,0);
myGLCD.setFont(SevenSegNumFont);
myGLCD.printNumI(panPot,110,220,3);

//Setup Tilt Number Indicator
int tiltPot = analogRead(A1);
tiltPot = map(tiltPot,0,1023,100,200);
myGLCD.setColor(255, 150, 100);
myGLCD.setBackColor(0,0,0);
myGLCD.setFont(SevenSegNumFont);
myGLCD.printNumI(tiltPot,630,220,3);

// Draw Pan Circle
myGLCD.setColor(255,255,255);
int x=160;
int y=240;
int r=100;
myGLCD.drawCircle(x, y, r);
// if (panPot=1)
// {
// x=1,y=1
// }

// Serial.println(panLeft);

}

//"If" statements from mapped panPot analog read values from 1 to 359 to make filled circle
//that will follow large pan circle.
// Think x,y will not be ok. Will need to adjust values to follow circle
myGLCD.setColor(255,0,0);
if (panPot=1)
{
myGLCD.fillCircle(260,242,10);
}
if (panPot=2)
{
myGLCD.fillCircle(260,243,10);
}

if (panPot=3)
{
myGLCD.fillCircle(260,245,10);
}
if (panPot=4)
{
myGLCD.fillCircle(260,247,10);
}
if (panPot=5)
{
myGLCD.fillCircle(260,249,10);
}
if (panPot=6)
{
myGLCD.fillCircle(259,250,10);
}
if (panPot=7)
{
myGLCD.fillCircle(259,250,10);
}
if (panPot=8)
{
myGLCD.fillCircle(259,254,10);
}
if (panPot=9)
{
myGLCD.fillCircle(259,256,10);
}
if (panPot=10)
{
myGLCD.fillCircle(258,257,10);
}
if (panPot=11)
{
myGLCD.fillCircle(258,259,10);
}
if (panPot=12)
{
myGLCD.fillCircle(258,261,10);
}
if (panPot=13)
{
myGLCD.fillCircle(257,262,10);
}
if (panPot=14)
{
myGLCD.fillCircle(257,264,10);
}
if (panPot=15)
{
myGLCD.fillCircle(257,266,10);
}
if (panPot=16)
{
myGLCD.fillCircle(258,268,10);
}
if (panPot=17)
{
myGLCD.fillCircle(256,269,10);
}
if (panPot=18)
{
myGLCD.fillCircle(255,271,10);
}
if (panPot=19)
{
myGLCD.fillCircle(255,273,10);
}
if (panPot=20)
{
myGLCD.fillCircle(254,274,10);
}
if (panPot=21)
{
myGLCD.fillCircle(253,276,10);
}
if (panPot=22)
{
myGLCD.fillCircle(253,277,10);
}
if (panPot=23)
{
myGLCD.fillCircle(252,279,10);
}
if (panPot=24)
{
myGLCD.fillCircle(251,281,10);
}
if (panPot=25)
{
myGLCD.fillCircle(251,282,10);
}
if (panPot=26)
{
myGLCD.fillCircle(250,284,10);
}
if (panPot=27)
{
myGLCD.fillCircle(249,285,10);
}
if (panPot=28)
{
myGLCD.fillCircle(248,287,10);
}
if (panPot=29)
{
myGLCD.fillCircle(247,288,10);
}
if (panPot=30)
{
myGLCD.fillCircle(247,290,10);
}
if (panPot=31)
{
myGLCD.fillCircle(246,292,10);
}

}

WSFA_TFT_PanTilt_16.ino (28.3 KB)

Three words: "Pythagoras", "octant" and "symmetry"
(And a fourth: "Bresenham")
Please remember to use code tags when posting code.

if (panPot=1)

Oh dear.

  //Setup Pan Button
  int pressed_button;
  int panLeft = myButtons.addButton(340,80,150,30,"Pan Left");
  int panRight = myButtons.addButton(340,180,150,30,"Pan Right");
  int tiltUp = myButtons.addButton(340,280,150,30,"Tilt Up");
  int tiltDown

Having globals and locals with the same name is confusing and error-prone, don't you think?

Sorry,
Each panPot (goes from 1 to 359) and represents the mapped (should be 1 to 359) position. The following "if" statement represents the corresponding x,y position on the GLCD. So panPot =1 means pan Pot is one degree and the if statement represents the x,y position on the display. Crude I know- no provision for erasing old circles- don't know how. Oh, the panPot is on A0 from the pan pot (0 to 5 volts). The tilt pot is on A1. The D8,D9,D10, and D11 outputs and delays are used to test four touchscreen buttons. Pan left is D8, Pan right is D9, Tilt Left is D10 and Tilt right is D11. Eventually D8,D9,D10,D11 will go to relays that go to the pan and tilt motors. Will need the buttons to be push and hold rather than the current momentary push. The delays are temporary to see if the four touch buttons work. Right now the D outputs are connected to LEDs for testing.The buttons send an on/off voltage to the D8,D9,D10,D11 outputs for testing.

Crude I know- no provision for erasing old circles- don't know how.

Redraw them in the background colour?

Thanks. Yes that sounds great. Will try to include later. Right now the panPot x,y positions on the GLCD are determined by multiple if statements. There must a better way. Also the x,y values were pre- calculated with

x=x1+r cos(t)

y=y1+ r sin(t)

Value of large static circle that the small filled circle will follow. The small circle indicates the pan position.
r=100
X1=160
y1=240
t =angle ( 1 to 359)
If panpot = 1 degree x=160+100*cos(1) etc.
.
Then the results were rounded to one digit. The values won't work since the x,y axis goes negative at times and the LCD y pixel count from top to bottom beginning at 0 and increase. That may be backwards. In any case the values will be adjusted to match the static circle. Don't think the If statement method will work. Only way I know how. Also may have to add myGLCD.drawCircle command. Not sure. A beginner.

The values won't work since the x,y axis goes negative at times and the LCD y pixel count from top to bottom beginning at 0 and increase

They only go negative about the Cartesian (0,0) origin.
If you offset the origin, say to the coordinates of the centre of your screen, they won't be negative.

Thanks! That should take care of figuring out the pan positions. Another copy of the sketch is attached with better explanations of the code as requested. I'll try all the suggestions soon.

WSFA_TFT_PanTilt_17.ino (29.7 KB)

You've still got this if (panPot=1)  which simply won't work.
You're assigning the value 1 to panPot, and then test the result of the assignment, which will always be true.
For comparisons, use "==".

However, you don't need this massive list of "if"s, because your trig will give you your coordinates.

xpoint = xcentre + (r * cos (theta))
ypoint = ycentre + (r * sin (theta))

Don't forget to convert your angle to radians.

Don't quite understand. Lots of questions...
Where in the sketch should degrees be converted to radians?
Should decimals be eliminated with radian = degree*1000/57296?
In the formula xpoint = xcentre + (r * cos (theta)), Is theta replaced with radians?
Does that mean the if's aren't needed so it can be written something like this?

#include <math.h>

int r=100
int xcentre=160
int ycentre=240

int panPot = analogRead(A0);

panPotmap = map(panPot, 0, 1023, 1, 359);

panPotrad = panPotmap * 1000/57296 //convert to radians and eliminate decimals

//(Is eliminating the decimal necessary?)

xpoint = xcentre + (r * cos (theta)) // Is theta replaced with panPotrad?

ypoint = ycentre + (r * sin (theta)) // Is theta replaced with panPotrad?

// deg = rad * 57296 / 1000

// rad = deg * 1000 / 57296

int panPot = analogRead(A0);
panPotmap = map(panPot, 0, 1023, 1, 359);
panPotrad = panPotmap * 1000/57296  //convert to radians and eliminate decimals

No need to go from analog input to radians in three steps:

float panPotRad = map(analogRead(A0), 0, 1023, 0, 2*PI);
// OR
float panPotRad = map(analogRead(A0), 0, 1023, -PI, PI);

It certainly doesn't make sense to map to degrees and then to radians.

When working in Radians it certainly makes sense to work in float, especially is you want more than 6 steps per circle.

Thanks! That makes sense. Beginning to see how to do it. Not sure about my problem having the same names for globals and locals. Need to study up on that. Still confused. I will let everyone know tomorrow if it worked!

randee01:
Not sure about my problem having the same names for globals and locals. Need to study up on that. Still confused. I will let everyone know tomorrow if it worked!

If you have a local with the same name as a global, that part of the program will use the local. The rest of the program will use the global. Locals get a random value when created and don't exist outside the block in which they are declared.

That did it. The code works! The marker circles are being drawn around the large circle according to analogRead- not in the right direction yet but getting there!

Sorry but I have yet another problem. How are the old circles erased or written over from the display. Instead of one circle I get a bunch of circles.
I tried this but can't figure out how to make it only apply to circles that must be erased

myGLCD.setColor(0,0,0); // write black color to fill (cover) old circles

myGLCD.fillCircle(xpanPotMarker, ypanPotMarker,10) // determine positions of old circles

Current Code

//Setup Pan Position Circle
int x=160; // x value of main pan circle
int y=240; // y value of main pan circle
int r=100; // radius of main pan circle

float xpanPotMarker; //declare pan marker circle x value

float ypanPotMarker; //declare pan marker circle y value

float panPotMarker = map(panPot,0,1023,1,359); // map panPot from analog Read

myGLCD.setColor(255,0,0); //set color of Pan Position marker circle

xpanPotMarker = x + (r * cos (panPotMarker)); //calculate new x value for pan pot marker around main circle

ypanPotMarker = y + (r * sin (panPotMarker)); //calculate new y value for pan pot marker around main circle

myGLCD.fillCircle(xpanPotMarker, ypanPotMarker,10); // draw small marker circle

WSFA_TFT_PanTilt_20.ino (31 KB)

A picture is attached

You will want to store the position of the last dot you drew and erase it just before you draw the new dot.

This sort of works if (oldxpanPotMarker != xpanPotMarker || oldypanPotMarker != ypanPotMarker) but I don't understand why the name oldpanPotMarker tells the sketch this is the old value. Is the word "old" a part of a library or the arduino code? But it seems to work. The old circles are erased but the current circle blinks. Also there are only a few circles drawn around the main circle - maybe six of them. Think it is related to mapping to -PI to PI or 0 to 2PI? Is there a better way to improve the speed so the circle doesn't blink and make the circle appear at least every 10 degrees. Sorry to ask all these questions. Spent hours trying different things that don't work well - not sure what to do now,

Thanks

float xpanPotMarker; //declare pan marker circle x value
float ypanPotMarker; //declare pan marker circle y value
float panPotMarker = map(panPot,0,1023,0,360); // map panPot from analog Read
//float panPot = panPotMarker;
//deg= rad * 57296 / 1000
//float panPotMarkerA = panPotMarker * 57296/1000 ;
//rad = deg * 1000 / 57296

myGLCD.setColor(255,0,0); //set color of Pan Position marker circle
xpanPotMarker = x + (r * cos (panPotMarker)); //calulate new x value for pan pot marker around main circle
ypanPotMarker = y + (r * sin (panPotMarker)); //calulate new y value for pan pot marker around main circle
myGLCD.fillCircle(xpanPotMarker, ypanPotMarker,10); // draw small marker circle

// Beginning Code to write over unused circles
float oldxpanPotMarker;
float oldypanPotMarker;
myGLCD.setColor(0, 0, 0); // Set color to black
if (oldxpanPotMarker != xpanPotMarker || oldypanPotMarker != ypanPotMarker)

{
delay(200);
myGLCD.fillCircle(xpanPotMarker, ypanPotMarker,10);
}

Why does the word "old" record the last position of the marker circle? Is it part of the arduino code or library? Used as if (oldxpanPotMarker != xpanPotMarker || oldypanPotMarker != ypanPotMarker).
The circle erases the old circles but the current circle blinks. Is there a better way to write it so it doesn't blink. Guessing it's not updating fast enough. Mapping with - PI to PI or 0 to PI causes the display to show only about three or four circle markers around the main circle. How can it be made to display more circles ?
Thanks

//Setup Pan Position Marker
//int x=160; // x value of main pan circle (real code is in void setup)
//int y=240; // y value of main pan circle (real code is in void setup)
//int r=100; // radius of main pan circle (real code is in void setup)
float xpanPotMarker; //declare pan marker circle x value
float ypanPotMarker; //declare pan marker circle y value
float panPotMarker = map(panPot,0,1023,0,360); // map panPot from analog Read

//deg= rad * 57296 / 1000
//float panPotMarkerA = panPotMarker * 57296/1000 ;
//rad = deg * 1000 / 57296

myGLCD.setColor(255,0,0); //set color of Pan Position marker circle
xpanPotMarker = x + (r * cos (panPotMarker)); //calculate new x value for pan pot marker around main circle
ypanPotMarker = y + (r * sin (panPotMarker)); //calculate new y value for pan pot marker around main circle
myGLCD.fillCircle(xpanPotMarker, ypanPotMarker,10); // draw small marker circle

// Code to write over unused circles
float oldxpanPotMarker;
float oldypanPotMarker;
myGLCD.setColor(0, 0, 0); // Set color to black
if (oldxpanPotMarker != xpanPotMarker || oldypanPotMarker != ypanPotMarker)

{
delay(200); //temporarily used to monitor position of circles
myGLCD.fillCircle(xpanPotMarker, ypanPotMarker,10); // writes solid circles around the main pan //circle
}

// Draw Pan Circle ( code is written in void setup - shown here to easily see code for main pan circle
myGLCD.setColor(255,255,255);
//int x=160;
// int y=240;
// int r=100;
// myGLCD.drawCircle(x, y, r);

}
}

randee01:
Why does the word "old" record the last position of the marker circle?

It doesn't. You are using garbage from the data stack. You are lucky you get any usable results. Your code should have global or static variables to store the old values. Your code should look something like:
Calculate the new X and Y position.
If the new X and Y position is different from the old X and Y position {
Erase the old circle at the old position.
Draw the new circle at the new position.
Save the new position as the old position.
}

Sorry,
I'm confused. How do you know what to call the old variable and the new variable? How would the new position be saved as the old position?
Thanks

 static float oldxpanPotMarker;
 static float oldypanPotMarker;

oldxpanPotMarker = xpanPotMarker;
oldypanPotMarker = ypanPotMarker;