Advertisement
Guest User

LightningTrigger

a guest
Jul 15th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.55 KB | None | 0 0
  1. const int triggerOut = 9;               // Output to camera shutter release (to take picture)
  2. const int sensor = A0;                  // Photoresistor
  3. const float lightLevelMultiplier = 1.4; // level above ambient to trigger shutter release (ex. 1.4 = 40% above ambient)
  4. int lightLevel = 0;                     // holds reading from photoresitor
  5. int ambient = 0;                        // holds average of last 'numReadings' samples of lightLevel to determine ambient light level
  6.  
  7. //bellow are variables used for calculating ambient light
  8. const int numReadings = 100;    // number of readings we want to average
  9. int readings[numReadings];      // the readings from the photoresistor input
  10. int readIndex = 0;              // the index of the current reading
  11. long total = 0;                 // the running total
  12.  
  13. void setup() {
  14.   // put your setup code here, to run once:
  15.  
  16. Serial.begin(115200);
  17. pinMode(triggerOut,OUTPUT);
  18. digitalWrite(triggerOut,HIGH);    // Setting shutter release output to HIGH (LOW is "ON")
  19.  
  20. }
  21.  
  22.  
  23. void loop() {
  24.  
  25.   lightLevel = analogRead(sensor);      // reading the current light level
  26.  
  27.   // calculate ambient lighting        
  28.   total = total - readings[readIndex];      // something i stole and do not understand well enough to comment
  29.   readings[readIndex] = analogRead(sensor); //
  30.   total = total + readings[readIndex];      //
  31.   readIndex = readIndex + 1;                //
  32.                                             //
  33.   if (readIndex >= numReadings) {           //
  34.     readIndex = 0;                          //
  35.   }                                         //
  36.                                            
  37.                                              
  38.   ambient = total / numReadings; // average
  39.  
  40.  
  41.  
  42.  
  43. if(lightLevel > ambient * lightLevelMultiplier){  // checks for spike in light level (lightning)
  44.   digitalWrite(triggerOut,LOW);                   // Turn "on" shutter release
  45.   delay(10);                                      // small delay just seemed appropriate
  46.   digitalWrite(triggerOut,HIGH);                  // Turn "off" shutter release
  47.   delay(150);                                     // Delay to filter out multiple triggers for single event
  48.  
  49. }
  50.  
  51. Serial.print("Ambient: ");                    // For debugging
  52. Serial.print(ambient);
  53. Serial.print("  ");
  54. Serial.print("lightLevel: ");
  55. Serial.println(lightLevel);
  56. Serial.print("readings[numReadings]: ");
  57. Serial.print(readings[numReadings]);
  58. Serial.print(" readIndex: ");
  59. Serial.print(readIndex);
  60. Serial.print(" total: ");
  61. Serial.println(total);
  62. Serial.println();
  63.  
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement