Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int triggerOut = 9; // Output to camera shutter release (to take picture)
- const int sensor = A0; // Photoresistor
- const float lightLevelMultiplier = 1.4; // level above ambient to trigger shutter release (ex. 1.4 = 40% above ambient)
- int lightLevel = 0; // holds reading from photoresitor
- int ambient = 0; // holds average of last 'numReadings' samples of lightLevel to determine ambient light level
- //bellow are variables used for calculating ambient light
- const int numReadings = 100; // number of readings we want to average
- int readings[numReadings]; // the readings from the photoresistor input
- int readIndex = 0; // the index of the current reading
- long total = 0; // the running total
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(115200);
- pinMode(triggerOut,OUTPUT);
- digitalWrite(triggerOut,HIGH); // Setting shutter release output to HIGH (LOW is "ON")
- }
- void loop() {
- lightLevel = analogRead(sensor); // reading the current light level
- // calculate ambient lighting
- total = total - readings[readIndex]; // something i stole and do not understand well enough to comment
- readings[readIndex] = analogRead(sensor); //
- total = total + readings[readIndex]; //
- readIndex = readIndex + 1; //
- //
- if (readIndex >= numReadings) { //
- readIndex = 0; //
- } //
- ambient = total / numReadings; // average
- if(lightLevel > ambient * lightLevelMultiplier){ // checks for spike in light level (lightning)
- digitalWrite(triggerOut,LOW); // Turn "on" shutter release
- delay(10); // small delay just seemed appropriate
- digitalWrite(triggerOut,HIGH); // Turn "off" shutter release
- delay(150); // Delay to filter out multiple triggers for single event
- }
- Serial.print("Ambient: "); // For debugging
- Serial.print(ambient);
- Serial.print(" ");
- Serial.print("lightLevel: ");
- Serial.println(lightLevel);
- Serial.print("readings[numReadings]: ");
- Serial.print(readings[numReadings]);
- Serial.print(" readIndex: ");
- Serial.print(readIndex);
- Serial.print(" total: ");
- Serial.println(total);
- Serial.println();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement