Skip to content

Instantly share code, notes, and snippets.

@nilclass
Created April 24, 2013 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nilclass/5450937 to your computer and use it in GitHub Desktop.
Save nilclass/5450937 to your computer and use it in GitHub Desktop.
#include <math.h>
int offsetX = 514, offsetY = 504;
float minX = -270, maxX = 264;
float minY = -271, maxY = 278;
void setup() {
Serial.begin(9600);
}
float calcAngle(float _x, float _y) {
float x = _x / maxX, y = _y / maxY;
float c = sqrt(pow(x, 2) + pow(y, 2));
return (acos(x / c) + (y < 0 ? PI : 0))/(2*PI) * 360.0;
}
void loop() {
int x = analogRead(A0) - offsetX;
int y = analogRead(A1) - offsetY;
Serial.println(calcAngle((float)x, (float)y), DEC);
delay(100);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment