Created
October 27, 2012 21:10
-
-
Save aprimc/3966275 to your computer and use it in GitHub Desktop.
Flimsy Arduino Ocelli
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
NOTE: don't use this, use https://gist.github.com/3968902 instead | |
Flimsy Arduino Ocelli | |
Get the direction of the source of light from three photoresistors and | |
present it as a number on a clock dial. | |
Andrej Primc <aprimc@gmail.com> (http://fgh.si) inspired by the bees | |
There are three ocelli facing directions 12, 4 and 8 on a clock dial. If | |
all three ocelli are receiving about the same amount of light, the source | |
is above (0). If ocellus facing 12 receives most light and ocelli facing 4 | |
and 8 about the same, the light source is 12. If ocellus 12 receives more | |
than ocellus 4 and ocellus 4 more than ocellus 8, the light source is 1. | |
And so on. | |
Sensor readings will more often be unequal than equal. Unless we round the | |
readings using some constant K, even positions, where we test for equality, | |
will cover less of our imaginary dial then odd positions, where we test | |
for inequality. | |
The right K is a mystery. If it's too low, we'll get mostly odd positions. | |
If it's too high, we'll get mostly even positions. If it's even higher than | |
that, we'll get mostly "above". | |
*/ | |
const int sensor_pin_a = A0; // facing 12 o'clock, i.e. front | |
const int sensor_pin_b = A1; // facing 4 o'clock | |
const int sensor_pin_c = A2; // facing 8 o'clock | |
int K = 64; // constant that defines "about the same" | |
void setup() { | |
// used by utility flashBits(int) | |
pinMode(13, OUTPUT); | |
} | |
// utility: flash pin 13 with a binary representation of d | |
void flashBits(int d) { | |
boolean flag = false; | |
for (int i = 15; i >=0 ; i--) { | |
if ((1 << i) & d) { | |
// long pulse means 1 | |
flag = true; | |
delay(50); | |
digitalWrite(13, HIGH); | |
delay(400); | |
digitalWrite(13, LOW); | |
delay(50); | |
} else { | |
//short pulse means 0 | |
if ((!flag) && (i != 0)) { | |
// skip leading zeroes | |
continue; | |
} | |
delay(225); | |
digitalWrite(13, HIGH); | |
delay(50); | |
digitalWrite(13, LOW); | |
delay(225); | |
} | |
} | |
delay(500); | |
} | |
void loop() { | |
int a, b, c; | |
int source; | |
// read sensors | |
// A is at 12 o'clock | |
// B is at 4 o'clock | |
// C is at 8 o'clock | |
a = analogRead(sensor_pin_a) / K; | |
b = analogRead(sensor_pin_b) / K; | |
c = analogRead(sensor_pin_c) / K; | |
// get direction | |
// 1 - 12 are positions on a dial, 0 is above (or below) | |
if (a == b) { | |
if (b == c) { | |
source = 0; // above | |
} else if (a > c) { | |
source = 2; | |
} else { | |
source = 8; | |
} | |
} else if (b == c) { | |
if (a > b) { | |
source = 12; | |
} else { | |
source = 6; | |
} | |
} else if (a == c) { | |
if (a > b) { | |
source = 10; | |
} else { | |
source = 4; | |
} | |
} else if (a > b && a > c) { | |
if (b > c) { | |
source = 1; | |
} else { | |
source = 11; | |
} | |
} else if (b > a && b > c) { | |
if (a > c) { | |
source = 3; | |
} else { | |
source = 5; | |
} | |
} else { // c > a && c > b | |
if (a > b) { | |
source = 9; | |
} else { | |
source = 7; | |
} | |
} | |
// flash position on led 13 | |
flashBits(source); | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment