/*
Several changes to the original sketch from post #1 in the forum
see code for comments
Forum: https://forum.arduino.cc/t/potentiometer-change-threshold-value/1155150/6
Wokwi: https://wokwi.com/projects/372327961938093057
*/
#define SHUTTER_PIN 2
#define LIGHTNING_TRIGGER_ANALOG_PIN 0
#define TRIGGER_THRESHHOLD 18
int lightningVal;
void setup()
{
pinMode(SHUTTER_PIN, OUTPUT);
digitalWrite(SHUTTER_PIN, LOW);
Serial.begin(9600); // open serial
// Use mapping here as well to avoid triggering after (re-)start of the sketch
lightningVal = map(analogRead(LIGHTNING_TRIGGER_ANALOG_PIN), 0, 1023, 0, 50);
Serial.print("Starting with lightningVal:\t");
Serial.println(lightningVal);
}
void loop()
{
// Added mapping here from 0..1023 to 0..50 also
int aLightningVal = analogRead(LIGHTNING_TRIGGER_ANALOG_PIN);
int newLightningVal = map(aLightningVal, 0, 1023, 0, 50);
if (abs(newLightningVal - lightningVal) > TRIGGER_THRESHHOLD)
{
// Moved the prints here so that they are readable before the
// delay
Serial.println("Shutter triggered");
// Moved this line from below the if-clause to here so
// that the variable lightningVal is only changed if
// the condition above was true
lightningVal = newLightningVal;
Serial.print("New lightningVal:\t");
Serial.println(lightningVal, DEC);
digitalWrite(SHUTTER_PIN, HIGH);
delay(1000); // May want to adjust this depending on shot type
digitalWrite(SHUTTER_PIN, LOW);
}
}