You are on page 1of 1

/*

Simple Solenoid Timer - Mark Qvale


This will turn on an output (D13) for an adjustable amount of time (20ms to 600ms) with a 1
second fire delay to keep the activation a single event.
*/

// pin assignments
const int triggerPin = 2; // the number of the trigger pin
const int firePin = 13; // the number of the fire pin
int adjPin = A3; // the analog adjustment pin

// variables
int buttonState = 0; // variable for reading the triggerPin state
int interval; // variable to set the firing time
unsigned long currentTime; // variable to capture current time (ms)
int adj = 0; // variable to capture fire time adjustment

void setup() {
pinMode(firePin, OUTPUT); // initialize the fire pin as an output:
pinMode(triggerPin, INPUT); // initialize the trigger pin as an input:
}

void loop() {
adj = analogRead(adjPin); // sets adj to integer value
interval = map(adj, 0, 1023, 20, 600); // re-maps values to use in interval

buttonState = digitalRead(triggerPin);
if(buttonState == HIGH)
{
delay(1000); // delay
guarantees a single fire
currentTime = millis();
while(currentTime + interval >= millis()){
digitalWrite(firePin, HIGH); // fires solenoid for time = interval
}
}
digitalWrite(firePin, LOW); // resets firePin to off (0)
}

You might also like