Skip to content

Instantly share code, notes, and snippets.

@nirodg
Created March 30, 2018 21:56
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 nirodg/ac173a5ccc9ac141b958904bbb382c61 to your computer and use it in GitHub Desktop.
Save nirodg/ac173a5ccc9ac141b958904bbb382c61 to your computer and use it in GitHub Desktop.
Potentiometer's degree's position for Arduino
// author: Dorin Brage
// repo: https://github.com/nirodg
long maxRotPtr = 1023; // Max potentiometr value
long totalDegr = 360; // Max degree
long rotationPtr = 0; // Will be fetch from the A0 input
const int sensorInput = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Position: ");
Serial.print(getPositionDegrees(), 0); // 0 (zero) won't display decimals
Serial.println(" º degr ");
delay(500);
}
float getPositionDegrees() {
rotationPtr = analogRead(sensorInput);
return abs((totalDegr * rotationPtr) / maxRotPtr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment