#define buttonPin 2 // the number of the pushbutton pin #define solenoidPin 4 // the number of the solenoid pin #define potPin A0 // Pot should be connected to this analog pin #define shutterMinTime 500 // min shutter millis #define shutterMaxTime 2000 // mas shutter millis int selnoidOpenTime = 2000; unsigned long TimerStart; bool Timer_Started; // is set when Timer is started // variables will change: boolean buttonState; // variable for reading the pushbutton status void setup() { // initialize the LED pin as an output: pin as an output: pinMode(solenoidPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); } void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. If it is, the buttonState is HIGH: if (buttonState == HIGH) { if (!Timer_Started) { int analogReadValue = analogRead(potPin); selnoidOpenTime = map(analogReadValue,0,1024,shutterMinTime,shutterMaxTime); digitalWrite(solenoidPin, HIGH); // Open // turn LED ON TimerStart = millis(); // starting Timer Timer_Started = true; } //end if } //end if // if timer already started if (Timer_Started) { if ( TimerStart + selnoidOpenTime <= millis()) { digitalWrite(solenoidPin, LOW); // Open // turn LED OFF Timer_Started = false; } //end if } //end if }