I have a pretty simple function set up to display a dial on my 128*64 OLED display. It's just a circle with a line drawn from the center to the edge at a specific angle. I have it displaying the position of a stepper motor, but it's turning the wrong way and I can't figure out why.
void drawDial(uint8_t radius, double angle, uint8_t h, uint8_t k) {
uint8_t x, y;
x = radius * cos(angle) + h; // problem occurs with these two
y = radius * sin(angle) + k; // these are the endpoints of the line
display.drawCircle(h, k, radius, WHITE); // (center x, center y, radius, color)
display.drawLine(h, k, x, y, WHITE); // (x1, y1, x2, y2, color)
}
An angle goes into the function that can range from 0 to 2pi. At the moment it's receiving an angle that changes from 0 to pi, so it should start from x = radius + h and y = 0 + k, this is 3 o'clock, and it does start here. It should rotate counter clockwise as it move toward pi.
for example, r = 20, angle = 0, h = 64, k = 32:
a circle of radius 20 is drawn in the center of the screen with an arrow pointing to 3 o'clock.
x = 20cos(0)+64 = 84
y = 20sin(0) +32 = 32
Here's a graph so it's easier to visualize. Scroll variable a to see what I mean.
but some trouble happens when I move toward pi, r = 20, angle = pi/2, h = 64, k = 32:
x = 20cos(pi/2)+64 = 64
y = 20sin(pi/2) +32 = 52
These (x,y) coordinates should obviously be above the center on top, but instead it's drawn at the bottom. This is causing my dial to rotate in the opposite direction it should.
What's up with this?