High speed trigger is slow when LCD screen added.

I have made a project that triggers a flashgun when a light barrier is broken or sound is registered depending on the sensor. The pot is added so i can tune the delay between the sensor being triggered and the flash firing.

All is working well except there is a obvious delay (from the photographs taken of a crossbow bolt hitting various items)and the flash firing. The LCD (16x2) is used to display the delay.

When I remove the LCD from the sketch all is well and I can capture excellent shots of crossbow bolt (200 at velocity of fps) and and a pellet (400 fps) hitting various items.

Question is, do I have to do without the LCD screen and just use the ‘feel’ of the pot position or is there a way of integrating the LCD without slowing down the speed between detection and firing of the flashgun. I am assuming the part of the sketch for the LCD is the problem.

Sketch is attached

Many thanks in anticipation.

Jon

Flash Digital.txt (1.62 KB)

Attached where?

I am assuming the part of the sketch for the LCD is the problem.

Yes, writing to the LCD IS slow. You can minimize that by only writing when the value to be written differs from what was last written.

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C  lcd(0x27,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified module

#define READY_BUTTON 12
#define READY_LED 7 //GREEN
#define FLASH_TRIGGER_PIN 2
#define SENSOR_PIN 11
#define FIRE_LED 4 //BLUE
#define POT_PIN A2
#define STANDBY 0
#define ACTIVE 1
int mode = STANDBY;
int flashDelayMS = 0; 
int NOISE;

void setup() 
{
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH); 
lcd.begin(16, 2);
lcd.clear();

pinMode(SENSOR_PIN, INPUT);
digitalWrite(SENSOR_PIN, LOW);
pinMode(READY_BUTTON, INPUT_PULLUP);
pinMode(READY_LED, OUTPUT);
digitalWrite(READY_LED, LOW);
pinMode(FLASH_TRIGGER_PIN, OUTPUT);
pinMode(FIRE_LED, OUTPUT);
digitalWrite(FIRE_LED, LOW); 
Serial.begin(9600);
delay(250);
}

void loop() 
{
lcd.setCursor(0,0);
lcd.print("Delay");
flashDelayMS = analogRead(POT_PIN)/4.092; // 0-250 ms
lcd.setCursor(7,0);
lcd.print("    ");
lcd.setCursor(7,0);
lcd.print(flashDelayMS);

if(digitalRead(READY_BUTTON)==LOW)
{
mode = ACTIVE;
}
        if(mode == ACTIVE){
        digitalWrite(READY_LED, HIGH);
        lcd.setCursor(4,1);
        lcd.print("            ");
        lcd.setCursor(4,1);
        lcd.print("READY");
}
NOISE = digitalRead(SENSOR_PIN);
if((mode == ACTIVE)&&(NOISE == 1))
{
delay(flashDelayMS);
//Serial.println(flashDelayMS);
digitalWrite(FLASH_TRIGGER_PIN, HIGH); // fire flash
delay(50);
digitalWrite(FLASH_TRIGGER_PIN, LOW);
digitalWrite(FIRE_LED, HIGH);
delay(2000);
digitalWrite(FIRE_LED, LOW); 
digitalWrite(READY_LED, LOW);
lcd.clear();
mode = STANDBY;

}}

Danksy:
is there a way of integrating the LCD without slowing down the speed between detection and firing of the flashgun.

of course there is.. first if you could indent your code properly and don't put more than 1 '}' on a line, it would greatly increase the readability (wow that actually is a word..)
Don't continuously write to the LCD ! create a separate function writing the delay and only call it if the delay-value has been altered. Do the same for the mode. Now your code should work more or less at the same speed it was without the LCD (only a change in the delay slows it down temporarily)

Many thanks for the feedback and advice. I will try an rewrite code as advised.

Thanks again.
Jon