Displaying Compass on OLED SSD1306 (128x64)

Hi,
I was planning to display a compass in an OLED. Now, I'm testing on it.
So if I insert a degree in Serial Monitor, I will get the O/P in OLED. i.e., the pointer of the compass will show the angle, which I inserted as I/P. I'm getting it correctly.
But, the issue is, in some angles, the pointer of the compass will be shorter and in some other it will be longer. What would be the reason?
After this I have to interface it with an IMU.

The code is -

#include <Wire.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_GFX.h>
#include <U8glib.h>

#define OLED_WIDTH 128
#define OLED_HEIGHT 64

#define OLED_ADDR 0x3d
#define OLED_RESET 13;
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);

//int arc;
//float rad;
String setAngle;
float angle;
float radian_angle = 0;
float adjacent;
float opposite;
float x1 = 0;
float y1 = 0;
int xcenter = 64;
int ycenter = 32;
//float Lx0 = arc * sin(angle*M_PI / 180.);
//float Lx1 = arc * sin(angle*M_PI / 180.);



//void radar(uint8_t angle)
//{
//float x1 = sin(angle * M_PI / 180);        // needle TIP position
//float y1 = cos(angle * M_PI / 180); // a more accurate calculation would be "cos(DEG_TO_RAD*angle)"
//display.drawLine(xcenter, ycenter, xcenter+x1, ycenter-y1, WHITE);
//}
//********************************************//void setup()//********************************
void setup() {
  Serial.begin(9600);
  display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR);
  Serial.println("Enter the angle");
}

//*********************************************//void loop()//***********************************
void loop() {
  display.clearDisplay();
  if (Serial.available()) {
    angle = Serial.parseInt();
    setAngle = Serial.readStringUntil('\n');
    Serial.print("the angle is:");
    Serial.println(angle);
    radian_angle = angle * 0.0174533;
    Serial.print("the radian is:");
    Serial.println(radian_angle);
    Serial.print("sin:");
    Serial.println(sin(radian_angle));
    Serial.print("cos:");
    Serial.println(cos(radian_angle));
    adjacent = (cos(radian_angle) * (angle));
    opposite = (sin(radian_angle) * (angle));
    Serial.print("adjacent value is:");
    Serial.println(adjacent);
    Serial.print("opposite value is:");
    Serial.println(opposite);
    x1 = 64 + opposite;
    y1 = 32 - adjacent;
    Serial.print("Value of x1:");
    Serial.println(x1);
    Serial.print("Value of y1:");
    Serial.println(y1);

    //Lx0 = arc * sin(angle * M_PI / 180.);
    //Lx1 = arc * sin(angle * M_PI / 180.);

    //display.drawTriangle(63.5, 32, 63, 32, 63.5, 5, WHITE);

    //*************************************************Drawing Circle**********************************************
    display.drawCircle(64, 32, 30, WHITE);
    //*************************************************Angle 'N'*****************************************************
    display.setCursor(64, 5);
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.println("N");
    //*************************************************Angle 'S'*****************************************************
    display.setCursor(63, 54);
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.println("S");
    //**************************************************Angle 'W'***************************************************
    display.setCursor(28, 32);
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.println("W");
    //**************************************************Angle 'E'*****************************************************
    display.setCursor(98, 32);
    display.setTextColor(WHITE);
    display.setTextSize(1);
    display.println("E");

    display.drawLine(64, 32, 64 + opposite, 32 - adjacent, WHITE);

    display.display();
    display.setFont();
    while (1);
  }
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Math error. For a triangle, the sides are given by something like

    adjacent = length*cos(radian_angle);
    opposite = length*sin(radian_angle);

why are you multiplying by angle? So that the bigger the angle the longer the hand?

Thanks for the help @jremington and @killzone_kid . It is working now. If any further doubts, I'll ask .

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