Chris Styles
/
DemoBoard_RangeIndicator
main.cpp@1:7354a0bac81a, 2009-09-22 (annotated)
- Committer:
- chris
- Date:
- Tue Sep 22 11:55:03 2009 +0000
- Revision:
- 1:7354a0bac81a
- Parent:
- 0:c88750da21d0
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
chris | 1:7354a0bac81a | 1 | #include "DemoBoard.h" |
chris | 0:c88750da21d0 | 2 | |
chris | 0:c88750da21d0 | 3 | /* |
chris | 0:c88750da21d0 | 4 | * This project used and ultrasonic range finder to blend and RGB LED from |
chris | 0:c88750da21d0 | 5 | * green to yellow to red as the ranged object gets closer |
chris | 0:c88750da21d0 | 6 | */ |
chris | 0:c88750da21d0 | 7 | |
chris | 0:c88750da21d0 | 8 | int main() { |
chris | 0:c88750da21d0 | 9 | |
chris | 0:c88750da21d0 | 10 | while (1) { |
chris | 0:c88750da21d0 | 11 | |
chris | 0:c88750da21d0 | 12 | // Take a reading from the Ultrasonic Range Finder |
chris | 0:c88750da21d0 | 13 | float d = srf; |
chris | 0:c88750da21d0 | 14 | |
chris | 0:c88750da21d0 | 15 | // Scale and trim the number so it approaches 0.0 at a long range |
chris | 0:c88750da21d0 | 16 | // and approaches 1.0 as the range gets closer |
chris | 0:c88750da21d0 | 17 | float normalised = (2.0/d) - 0.02; |
chris | 0:c88750da21d0 | 18 | |
chris | 0:c88750da21d0 | 19 | // Set the red component to get brighter the closer the range |
chris | 1:7354a0bac81a | 20 | rgb.red(normalised); |
chris | 0:c88750da21d0 | 21 | |
chris | 0:c88750da21d0 | 22 | // Set the red component to get brighter the further the range |
chris | 1:7354a0bac81a | 23 | rgb.green(1.0-normalised); |
chris | 0:c88750da21d0 | 24 | |
chris | 0:c88750da21d0 | 25 | // For completeness, print the measured range |
chris | 0:c88750da21d0 | 26 | pc.printf("Range is %.1f cm\n",(float)srf); |
chris | 0:c88750da21d0 | 27 | wait (0.2); |
chris | 0:c88750da21d0 | 28 | |
chris | 0:c88750da21d0 | 29 | } |
chris | 0:c88750da21d0 | 30 | |
chris | 0:c88750da21d0 | 31 | } |
chris | 0:c88750da21d0 | 32 | |
chris | 0:c88750da21d0 | 33 |