Advertisement
Ocelot

Led camera

Nov 2nd, 2011
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.27 KB | None | 0 0
  1. //
  2. //  www.blinkenlight.net
  3. //
  4. //  Copyright 2011 Udo Klein
  5. //
  6. //  This program is free software: you can redistribute it and/or modify
  7. //  it under the terms of the GNU General Public License as published by
  8. //  the Free Software Foundation, either version 3 of the License, or
  9. //  (at your option) any later version.
  10. //
  11. //  This program is distributed in the hope that it will be useful,
  12. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. //  GNU General Public License for more details.
  15. //
  16. //  You should have received a copy of the GNU General Public License
  17. //  along with this program. If not, see http://www.gnu.org/licenses/
  18.  
  19. // Usage
  20. //
  21. // This sketch uses the Blinkenlight Shield as a light
  22. // sensor. In order to make this work jumper the shield
  23. // such the common cathode of the LEDs it connected
  24. // to +5V.
  25. //
  26. // It will output hexadecimal digits that correspond
  27. // to the amount of light captured by the LEDs.
  28. // 0 = very bright light
  29. // higher numbers = less light
  30. //
  31. //
  32. // Theory of operation
  33. //
  34. // For each LED the following happens:
  35.  
  36. // 1) The PIN is pulled low thus reversing the LED.
  37. //    Thus the LED will act like a capacitor and gets
  38. //    charged.
  39. // 2) We store the current milli second count in
  40. //    start_millis for later use.
  41. // 3) The PIN is put to high Z input and starts to
  42. //    "float" with the voltage of the "LED cap".
  43. // 4) If the LED captures light the "LED cap" will
  44. //    discharge fast, otherwise it discharges slow.
  45. // 5) As the cap discharges the input PIN will
  46. //    float high.
  47. // 6) Once the pin is detected to be high we will
  48. //    compute elapsed_millis by subtracting
  49. //    start_millis from the current milli second
  50. //    count
  51.  
  52. // The loops are coded in such a way that this
  53. // happens "in parallel". They are also coded
  54. // in such a way that each pin gets some time
  55. // to settle.
  56.  
  57. // used to store the start milli second count per pin
  58. uint16_t start_millis[20];
  59. // used to store the last computed milli second count when pin floated to high
  60. uint16_t elapsed_millis[20];
  61.  
  62. uint8_t transform(uint16_t data) {
  63.     // output transformation, used to map uint16_t to 1 hex digit
  64.     // basically a logarithm to the base of 2
  65.     uint8_t i=0;
  66.     while (data) {
  67.         data >>= 1;
  68.         ++i;
  69.     }
  70.     return i;
  71. }
  72.  
  73. boolean pin_is_ok(uint8_t pin) {
  74.     // used to determine which pins are good for light detection
  75.     // pins 0,1 are spoiled by the serial port
  76.     // pin 13 is spoiled by the Arduino's LED
  77.     return (pin != 0) && (pin !=1) && (pin != 13);
  78. }
  79.  
  80. void setup() {
  81.     Serial.begin(115200);
  82.     Serial.println("go");
  83.  
  84.     for (uint8_t pin = 0; pin < 20; ++pin) if (pin_is_ok(pin)) {
  85.         pinMode(pin, OUTPUT);
  86.         digitalWrite(pin, LOW);
  87.         start_millis[pin] = millis();
  88.         elapsed_millis[pin] = 0;
  89.     }
  90.     for (uint8_t pin = 0; pin < 20; ++pin) if (pin_is_ok(pin)) {
  91.         pinMode(pin, INPUT);
  92.     }
  93. }
  94.  
  95. void loop() {
  96.     for (uint8_t pin = 0; pin < 20; ++pin) if (pin_is_ok(pin)) {
  97.         if (digitalRead(pin)) {
  98.             pinMode(pin, OUTPUT);
  99.             elapsed_millis[pin] = millis()-start_millis[pin];
  100.             start_millis[pin] = millis();
  101.             pinMode(pin, INPUT);
  102.         }
  103.     }
  104.     for (uint8_t pin = 0; pin < 20; ++pin) if (pin_is_ok(pin)) {
  105.         Serial.print(transform(elapsed_millis[pin]), HEX);
  106.     }
  107.     Serial.println();
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement