Skip to content

Instantly share code, notes, and snippets.

@aprimc
Created October 28, 2012 15:26
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 aprimc/3968902 to your computer and use it in GitHub Desktop.
Save aprimc/3968902 to your computer and use it in GitHub Desktop.
Less Flimsy Arduino Ocelli
/*
Less Flimsy Arduino Ocelli
Get the direction of the source of light from three photoresistors.
Andrej Primc <aprimc@gmail.com> (http://fgh.si)
*/
int getSource(int a, int b, int c, int bins = 24) {
int ab = abs(a - b);
int bc = abs(b - c);
int ca = abs(c - a);
int mul = bins / 6;
int h;
int m;
if (a > b && b >= c) {
h = 0;
m = mul * 2 * bc / (ca + bc);
} else if (b >= a && a > c) {
h = 1;
m = mul * 2 * ab / (bc + ab);
} else if (b > c && c >= a) {
h = 2;
m = mul * 2 * ca / (ab + ca);
} else if (c >= b && b > a) {
h = 3;
m = mul * 2 * bc / (ca + bc);
} else if (c > a && a >= b) {
h = 4;
m = mul * 2 * ab / (bc + ab);
} else if (a >= c && c > b) {
h = 5;
m = mul * 2 * ca / (ab + ca);
} else {
h = 0;
m = 0;
}
return h * mul + m;
}
void setup() {
Serial.begin(9600);
}
void loop() {
int a, b, c, h;
a = analogRead(A0);
b = analogRead(A1);
c = analogRead(A2);
h = getSource(a, b, c);
Serial.print(h);
Serial.println("");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment